Java SSM框架之MyBatis3(八)MyBatis之動態SQL
阿新 • • 發佈:2018-07-21
one uniq div mapper ODB when rop mail con
View Code
前言:
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) NOTView CodeNULL, `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 SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
if標簽
User實體
UserMapper
View CodeUserMapper.xml
View CodeUserController
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