前言:
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 */;
if标签
User实体
UserMapper
UserMapper.xml
UserController
测试
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>
trim标签下的四个属性:
prefix:在标签开始添加上该字符串
suffixOverrides:在标签末尾去除上该字符串
suffix:在标签末尾添加上该字符串
prefixOverrides:在标签开始去除上该字符串
set标签
trim标签
switch\when标签
foreach标签
原文地址:https://www.cnblogs.com/cnki/p/9346333.html
时间: 2024-10-07 22:27:57