Mabits是一个Java持久化框架,它提供了动态SQL的功能。动态SQL是一种根据不同条件动态生成SQL语句的技术。在Mabits中,动态SQL通常是通过使用一组特殊的标签和代码块来实现的,这些标签和代码块可以根据条件包含或排除不同的部分,从而生成不同的SQL语句。动态SQL可以让开发者构建更灵活、高效的数据库操作语句,因为他们可以根据实际需要创建出更加精确和优化的SQL语句。Mabits的动态SQL还允许开发者使用参数化查询,可以防止SQL注入攻击,并增强程序的安全性
接下来学习经常使用的几个标签
常见场景:非必传参数时使用
int addUser2(UserEntity user);
@Test
void addUser2() {
String username = "zhaoliu";
String password = "123456";
UserEntity user = new UserEntity();
user.setUsername(username);
user.setPwd(password);
int result = userMapper.addUser2(user);
System.out.println("修改行数: "+result);
}
insert into userinfo(username,password
,photo
) values(#{username},#{pwd}
,#{photo}
)
当不传入photo时,执行结果
JDBC Connection [HikariProxyConnection@579590740 wrapping com.mysql.cj.jdbc.ConnectionImpl@75b6dd5b] will not be managed by Spring
==> Preparing: insert into userinfo(username,password ) values(?,? )
==> Parameters: zhaoliu(String), 123456(String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@380e1909]
修改行数: 1
传入photo时,执行结果
JDBC Connection [HikariProxyConnection@645717550 wrapping com.mysql.cj.jdbc.ConnectionImpl@609e57da] will not be managed by Spring
==> Preparing: insert into userinfo(username,password ,photo ) values(?,? ,? )
==> Parameters: zhaoliu2(String), 123456(String), dog.png(String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@24d61e4]
修改行数: 1
//数据库
mysql> select*from userinfo;
+----+----------+----------+---------+---------------------+---------------------+-------+
| id | username | password | photo | createtime | updatetime | state |
+----+----------+----------+---------+---------------------+---------------------+-------+
| 1 | lisi | 123456 | | 2022-12-06 17:10:48 | 2022-12-06 18:10:48 | 1 |
| 3 | zhangsan | 123456 | | 2023-05-18 17:21:49 | 2023-05-18 17:21:49 | 1 |
| 4 | wangwu | 123456 | | 2023-05-18 17:36:28 | 2023-05-18 17:36:28 | 1 |
| 5 | zhaoliu | 123456 | | 2023-05-20 09:22:08 | 2023-05-20 09:22:08 | 1 |
| 6 | zhaoliu2 | 123456 | dog.png | 2023-05-20 09:35:06 | 2023-05-20 09:35:06 | 1 |
+----+----------+----------+---------+---------------------+---------------------+-------+
5 rows in set (0.00 sec)
注意理解:
学习
insert into userinfo(
username,
password,
photo
) values(
#{username},
#{pwd},
#{photo}
)
当遇到这种所有参数都为非必填参数时,如果第一个参数传了值,后续都没传,就会出现(username,)这个问题,此时很明显SQL 语句是错误的,肯定不能成功执行或者其它类似这种问题
用法一:删除前缀后缀
insert into userinfo
username,
password,
photo
values
#{username},
#{pwd},
#{photo}
单元测试
@Transactional
@Test
void addUser3() {
String username = "zhaoliu3";
String password = "123456";
UserEntity user = new UserEntity();
user.setUsername(username);
user.setPwd(password);
int result = userMapper.addUser2(user);
System.out.println("修改行数: "+result);
}
当不传photo时,上一个pwd后的,被去除了
JDBC Connection [HikariProxyConnection@1228603887 wrapping com.mysql.cj.jdbc.ConnectionImpl@36c2d629] will be managed by Spring
==> Preparing: insert into userinfo(username,password ) values(?,? )
==> Parameters: zhaoliu3(String), 123456(String)
<== Updates: 1
用法二,解决多个非必传参数问题
List getListByIdOrTitle(@Param("id")Integer id ,@Param("title")String title);
@Test
void getListByIdOrTitle() {
List list = articleMapper.getListByIdOrTitle(null,null);
System.out.println(list.size());
}
执行结果:
传参都为空时,也就是trim中没有内容,那么prefix中的where也不会生产。就不用再使用
where 1=1 and ...的方式解决传参为空的问题了
能更好的解决上述多个非必传参数问题
过使用
需要注意的是,由于
执行
不传id 传title
将and前缀清除了
注意:
只能去除sql语句前缀的关键字,不能去除后缀关键字
标签也可以用 替换
通过使用
需要注意的是,由于
标签也可以用 替换
示例:修改lisi->lis2,password->456789
mysql> select*from userinfo;
+----+----------+----------+---------+---------------------+---------------------+-------+
| id | username | password | photo | createtime | updatetime | state |
+----+----------+----------+---------+---------------------+---------------------+-------+
| 1 | lisi | 123456 | | 2022-12-06 17:10:48 | 2022-12-06 18:10:48 | 1 |
| 3 | zhangsan | 123456 | | 2023-05-18 17:21:49 | 2023-05-18 17:21:49 | 1 |
| 4 | wangwu | 123456 | | 2023-05-18 17:36:28 | 2023-05-18 17:36:28 | 1 |
| 5 | zhaoliu | 123456 | | 2023-05-20 09:22:08 | 2023-05-20 09:22:08 | 1 |
| 6 | zhaoliu2 | 123456 | dog.png | 2023-05-20 09:35:06 | 2023-05-20 09:35:06 | 1 |
+----+----------+----------+---------+---------------------+---------------------+-------+
代码
//修改
int updateUser(UserEntity user);
update userinfo
username = #{username},
password = #{pwd},
where id = #{id}
@Test
void updateUser() {
UserEntity user = new UserEntity();
user.setUsername("lisi2");
user.setPwd("456789");
user.setId(1);
int result = userMapper.updateUser(user);
System.out.println("修改后: "+result);
}
执行结果
JDBC Connection [HikariProxyConnection@236002428 wrapping com.mysql.cj.jdbc.ConnectionImpl@4d0e54e0] will not be managed by Spring
==> Preparing: update userinfo SET username = ?, password = ? where id = ?
==> Parameters: lisi2(String), 456789(String), 1(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@95eb320]
修改后: 1
mysql> select*from userinfo;
+----+----------+----------+---------+---------------------+---------------------+-------+
| id | username | password | photo | createtime | updatetime | state |
+----+----------+----------+---------+---------------------+---------------------+-------+
| 1 | lisi2 | 456789 | | 2022-12-06 17:10:48 | 2022-12-06 18:10:48 | 1 |
| 3 | zhangsan | 123456 | | 2023-05-18 17:21:49 | 2023-05-18 17:21:49 | 1 |
| 4 | wangwu | 123456 | | 2023-05-18 17:36:28 | 2023-05-18 17:36:28 | 1 |
| 5 | zhaoliu | 123456 | | 2023-05-20 09:22:08 | 2023-05-20 09:22:08 | 1 |
| 6 | zhaoliu2 | 123456 | dog.png | 2023-05-20 09:35:06 | 2023-05-20 09:35:06 | 1 |
+----+----------+----------+---------+---------------------+---------------------+-------+
不传参数pwd时
标签是 MyBatis 中的一个迭代标签,可以对集合对象进行遍历,并生成多条 SQL 语句(如 INSERT
、UPDATE
、DELETE
等)。
使用
标签,我们可以将一个集合对象的元素依次取出,作为 SQL 语句中的参数进行插入、更新或删除操作。常用的语法如下:
其中,各个属性和元素的含义如下:
collection
:指定要遍历的集合对象的属性名。item
:指定在遍历过程中每个元素所对应的变量名。separator
:指定在生成多条 SQL 语句时,不同语句之间的分隔符,默认为英文逗号。open
:指定生成的 SQL 语句的头部。close
:指定生成的 SQL 语句的尾部。示例:删除文章表的数据
mysql> select*from articleinfo;
+----+-------+-----------+---------------------+---------------------+-----+--------+-------+
| id | title | content | createtime | updatetime | uid | rcount | state |
+----+-------+-----------+---------------------+---------------------+-----+--------+-------+
| 1 | Java | Java正文 | 2023-05-15 09:12:59 | 2023-05-15 09:12:59 | 1 | 1 | 1 |
| 2 | mysql | mysql正文 | 2023-05-19 11:14:49 | 2023-05-19 11:14:49 | 1 | 1 | 1 |
+----+-------+-----------+---------------------+---------------------+-----+--------+-------+
2 rows in set (0.00 sec)
此处进行回滚,不真的删除
代码:
//根据id批量删除文章
int delByIdList(List idList);
delete from articleinfo
where id in(
#{id}
)
@Transactional
@Test
void delByIdList() {
List idList = new ArrayList<>();
idList.add(1);
idList.add(2);
idList.add(3);
//只有两条
int result = articleMapper.delByIdList(idList);
System.out.println("删除: "+result);
}
执行单元测试
JDBC Connection [HikariProxyConnection@918738473 wrapping com.mysql.cj.jdbc.ConnectionImpl@55fee662] will be managed by Spring
==> Preparing: delete from articleinfo where id in( ? , ? , ? )
==> Parameters: 1(Integer), 2(Integer), 3(Integer)
<== Updates: 2
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4b960b5b]
删除: 2
上述代码没有使用open,close属性.将()写在了foreach标签外,这里加上
和刚才的写在标签外的方式作用是相同的