標籤:ima prefix 動態 username 去除 isp insert XML 建立
前言:
mybatis架構中最具特色的便是sql語句中的自訂,而動態sql的使用又使整個架構更加靈活。
建立User表
/*Table structure for table `user` */DROP TABLE IF EXISTS `user`;CREATE TABLE `user` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `username` varchar(20) NOT NULL, `age` int(3) NOT NULL, `phone` varchar(11) NOT NULL, `email` varchar(50) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;/*Data for the table `user` */insert into `user`(`id`,`name`,`username`,`age`,`phone`,`email`) values (1,‘張三‘,‘zs‘,18,‘15010998046‘,‘[email protected]‘),(2,‘李四‘,‘ls‘,19,‘15019087600‘,‘[email protected]‘),(3,‘王五‘,‘ww‘,20,‘15010898065‘,‘[email protected]‘);/*!40101 SET [email protected]_SQL_MODE */;/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */;/*!40014 SET [email protected]_UNIQUE_CHECKS */;/*!40111 SET [email protected]_SQL_NOTES */;
View Code
if標籤
User實體
View Code
UserMapper
View Code
UserMapper.xml
View Code
UserController
View Code
測試
http://localhost:8080/getUser
where標籤
<select id="getUser" resultType="cn.cnki.ref.pojo.User"> select * from user <where> <if test="id != null">and id=#{id} </if> <if test="age != null">and age=#{age} </if> <if test="name != null">and name=#{name}</if> </where> </select>View Code
trim標籤下的四個屬性:
prefix:在標籤開始添加上該字串
suffixOverrides:在標籤末尾去除上該字串
suffix:在標籤末尾添加上該字串
prefixOverrides:在標籤開始去除上該字串
set標籤
trim標籤
switch\when標籤
foreach標籤
Java SSM架構之MyBatis3(八)MyBatis之動態SQL