一.MyBatis
中的动态SQL
1.if
标签
根据条件,构建查询条件
<if test="属性判断">
and 属性=#{属性}
</if>
举例:
<select id="select" resultType="studentInfo">
select * from studentinfo where 1=1//where1=1是为了保证sql完整性
<if test="id!=null">
and id=#{id}
</if>
<if test="age!=null">
and age=#{age}
</if>
<if test="name!=null">
and name=#{name}
</if>
<if test="score!=null">
and score=#{score}
</if>
</select>
2.where
标签
上述例子中 有一句where 1=1
,也可以不用去拼接,但此时需要用<where></where>
把<if></if>
包裹起来。
3.foreach
标签
<delete id="deleteId" parameterType="list">
delete from studentinfo
<where>
<foreach collection="array" item="id" open="id in (" close=")" separator=",">
#{id}
</foreach>
</where>
</delete>
<foreach>
标签 parameterType
不可以是Array/array
collection
:遍历的对象,数组->array
,集合->list
,map
->对应的key
item
:给遍历的对象取个名字,通过它来取值
open
:语句的开始
close
:语句的结束
4.trim
标签
去除多余的AND
,逗号,或者拼接SQL
<select id="selectMany" parameterType="studentInfo" resultType="studentInfo">
select * from studentinfo
<trim prefix="WHERE" prefixOverrides="AND">
<if test="id!=null">
and id=#{id}
</if>
<if test="age!=null">
and age=#{age}
</if>
<if test="name!=null">
and name=#{name}
</if>
<if test="score!=null">
and score=#{score}
</if>
</trim>
</select>
prefix
:给SQL
语句(给子标签中的语句)的前面拼接
prefixOverrides
:去除SQL
语句(给子标签中的语句)前面的指定字符
5.set
标签
如果子标签有内容,自动添加set
,并删除最后一个逗号
<update id="updateByUser" parameterType="UserInfo">
update userinfo
<set>
<if test="username != null">
username = #{username},
</if>
<if test="name != null">
name = #{name},
</if>
where id = #{id}
</set>
</update>
6.choose
标签
没有语句和when匹配时,执行 <otherwise> </otherwise>
标签里面的,满足一个条件即可
<select id="select1" resultType="studentInfo">
select * from studentinfo
<where>
<choose>
<when test="id != null" >
and id=#{id}
</when>
<when test="age != null" >
and age=#{age}
</when>
<otherwise>
</otherwise>
</choose>
</where>
</select>
二.简化SQL
1.定义代码片段
<sql id="default">
id,name,password
</sql>//抽取重复的语句代码片段
2.引用代码片段
<include refid="与id的值对应"></include>
三.分页插件
PageHelper
:第三方分页助手
开发步骤:(1).导入PageHelper
依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.2</version>
</dependency>
(2)在mybatis
配置文件中配置pagehelper
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 指定方言 -->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
使用
在要用的地方:PageHelper.startpage(第几页,每页条数)
@Test
public void selectAll() {
PageHelper.startPage(1, 2);
List<UserInfo> userInfos = mapper.selectAll();
userInfos.forEach(u -> System.out.println(u));
}
其他信息
PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);
System.out.println("是否是第一页" + pageInfo.isIsFirstPage());
System.out.println("是否有下一页: " + pageInfo.isHasNextPage());
System.out.println("是否是最后一页:" + pageInfo.isIsLastPage());
System.out.println("是否有上一页:" + pageInfo.isHasPreviousPage());
System.out.println("当前是第几页:" + pageInfo.getPageNum());
System.out.println("页大小:" + pageInfo.getPageSize());
System.out.println("共多少条数据:" + pageInfo.getTotal());
System.out.println("总共多少页: " + pageInfo.getPages());
System.out.println("下一页: " + pageInfo.getNextPage());
System.out.println("上一页: " + pageInfo.getPrePage());