【重温SSM框架系列】13 - Mybatisd的动态SQL及PageHelper分页查询
动态SQL及分页查询
- 动态SQL
-
- if
- where
- foreach
- include
- PageHelper分页查询
-
- 1. 导入PageHelper依赖
- 2. 在mybatis核心配置文件中配置PageHelper插件
- 获取其他分页参数
大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。
如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!!
Good better best, never let it rest, until good is better, and better best.近期会重新温习一下SSM的相关知识,相应的博客会更新至专栏【SSM框架】中,欢迎大家关注!
SSM专栏:https://blog.csdn.net/weixin_43598687/category_11652306.html
动态SQL
动态 SQL 是 MyBatis 的强大特性之一,可以通过根据不同条件拼接 SQL 语句。
if
根据实体类的不同取值,使用不同的SQL语句来进行查询。比如在id如果不为空时可以根据id查询,如果username 不同空时将username也作为查询条件。
<select id="findByIdUsername"parameterType="user" resultType="user"> SELECT * FROM user WHERE <if test="id != 0"> id=#{id} </if> <if test="username!=null"> AND username=#{username} </if></select>
where
上面的SQL存在一些问题:
- 当id和username都为空时,SQL语句就会变成:
SELECT * FROM userWHERE
- 当id为空,username不为空时,SQL语句就会变成:
SELECT * FROM userWHEREAND username=#{username}
这样就会使得查询失败。
为了解决这个问题,可以使用代替where,如下:
<select id="findByIdUsername"parameterType="user" resultType="user"> SELECT * FROM user <where> <if test="id != 0"> id=#{id}</if> <if test="username!=null"> AND username=#{username}</if> </where></select>
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。
foreach
当循环执行sql的拼接操作,例如:SELECT * FROM USER WHERE id IN (1,2,3,4,5)。可以使用进行处理。
<select id="findByIds" parameterType="list" resultType="user">select * from user<where><foreach collection="array" open="id in(" close=")" item="id" separator=",">#{id}</foreach></where></select>
foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符。
可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
include
当Sql语句有许多重复的地方时,可以使用sql
将其中可将重复的sql提取出来,使用时用include 引用即可,最终达到sql重用的目的。如:
<sql id="selectAll"select * from User</sql><select id="findById" parameterType="int" resultType="user"><include refid="selectAll"></include> where id=#{id}</select><select id="findByIds" parameterType="list" resultType="user"><include refid="selectAll"></include> <where><foreach collection="array" open="id in(" close=")" item="id" separator=",">#{id}</foreach></where></select>
PageHelper分页查询
分页助手PageHelper是将分页的复杂操作进行封装,通过以下简单的步骤即可获得分页的相关数据。
1. 导入PageHelper依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.5</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.1</version> </dependency>
2. 在mybatis核心配置文件中配置PageHelper插件
<plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </plugin> </plugins>
测试
之前我们已经有了查询全部的实现:
@Test public void findAll() throws Exception { //加载核心配置文件 InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得sqlSession工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //获得sqlSession对象 SqlSession session = sqlSessionFactory.openSession(); //执行sql语句 List<User> userList = session.selectList("userMapper.findAll"); Iterator<User> iterator = userList.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } session.close(); }
在使用PageHelper进行分页时,只需要先指定当前页和每页显示条数即可。
获取其他分页参数
PageInfo<User> pageInfo= new PageInfo<User>(userList);System.out.println("总条数:"+pageInfo.getTotal());System.out.println("总页数:"+pageInfo.getPages());System.out.println("当前页:"+pageInfo.getPageNum());System.out.println("每页显示长度:"+pageInfo.getPageSize());System.out.println("是否第一页:"+pageInfo.isIsFirstPage());System.out.println("是否最后一页:"+pageInfo.isIsLastPage());
SSM专栏:https://blog.csdn.net/weixin_43598687/category_11652306.html