一、Mybatis的分页查询

  由于第一二节较为详细讲述了Mybatis的环境搭建,文件配置,SQL编写和Java代码实现,所以接下来的讲述都将只抽取关键代码和mapper文件中的关键sql,详细的流程和案例可参见《Mybatis入门和简单Demo》和《Mybatis的CRUD案例》。

  (1)无条件的分页的mapper文件配置和Java代码实现

<!-- 传入的参数类型为map,此时无需使用map.get("key")去获得实际值,只需填入key值便可 -->
<select id="findByPage" parameterType="map" resultMap="studentMap">
select id,name,age,sex from student
limit #{start},#{end}
</select>
/*
* 无条件分页查询
*/
public List<Student> findByPage(int start,int end)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession(); Map<String,Object> param = new LinkedHashMap<String,Object>();
param.put("start",start);
param.put("end",end); List<Student> stuList;
stuList = sqlSession.selectList(Student.class.getName()+".findByPage", param); System.out.println("添加查询成功"); return stuList;
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}

  (2)有条件的分页的mapper文件配置和Java代码实现

<select id="findByPageAndRequest" parameterType="map" resultMap="studentMap">
select id,name,age,sex from student
where name like #{name}
limit #{start},#{end}
</select>
/*
* 有条件分页查询
*/
public List<Student> findByPageAndRequest(String name,int start,int end)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession();
Map<String,Object> params = new LinkedHashMap<String,Object>();
//当sql的条件有模糊匹配时,参数需前后带上%
params.put("name", "%"+name+"%");
params.put("start", start);
params.put("end", end); List<Student> stuList;
stuList = sqlSession.selectList(Student.class.getName()
+".findByPageAndRequest", params); System.out.println("添加查询成功");
return stuList;
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}

二、Mybatis的动态SQL

  Mybatis除了支持简单的sql外,还支持多种动态sql语句,如条件判断,参数遍历,包含等等语法,下面通过一些例子简单认识下Mybatis对动态sql的支持

  (1)动态条件查询:查询时编写where条件,判断传入的参数不为空才予以拼接,条件写在<if test="xx">标签中

<select id="findAll" parameterType="map" resultMap="studentMap">
select * from student
<where>
<if test="id!=null">
and id = #{id}
</if>
<if test="name!=null">
and name = #{name}
</if>
<if test="age!=null">
and age = #{age}
</if>
<if test="sex!=null">
and sex = #{sex}
</if>
</where>
</select>
/*
* 动态带条件查询
*/
public List<Student> findAll(String id,String name,String age,String sex)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession(); Map<String,Object> stuMap = new HashMap<String,Object>();
stuMap.put("id", id);
stuMap.put("name", name);
stuMap.put("age", age);
stuMap.put("sex", sex); return sqlSession.selectList(Student.class.getName()+".findAll", stuMap);
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}

  (2)动态条件更新:查询时编写where条件,判断传入的参数不为空才予以拼接,其中判断条件中xx=#{xx}后需要带",",set标签会自动判断哪个是最后一个字段,会自动去掉最后一个","号

<!-- set标签会自动判断哪个是最后一个字段,会自动去掉最后一个","号 -->
<update id="update" parameterType="map">
update student
<set>
<if test="name!=null">
name = #{name},
</if>
<if test="age!=null">
age = #{age},
</if>
<if test="sex!=null">
sex = #{sex},
</if>
</set>
where id = #{id}
</update>
/*
* 动态带条件更新
*/
public List<Student> update(String id,String name,String age,String sex)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession(); Map<String,Object> updateMap = new HashMap<String,Object>();
updateMap.put("id", id);
updateMap.put("name", name);
updateMap.put("age", age);
updateMap.put("sex", sex); sqlSession.update(Student.class.getName()+".update",updateMap); sqlSession.commit();
return null;
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}

  (3)动态条件删除:遍历传入的参数,可以为数组,也可以为list结构,判断集合或数组中的字段值与表中某字段值相匹配则删除

<!--
foreach用于遍历数组元素
open表示开始符号
close表示结束符号
separator表示中间分隔符
item表示数组参数,属性值可以任意,但提倡与方法参数相同
-->
<delete id="dynamicDelete">
delete from student where id in
<foreach collection="array" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete> <delete id="dynamicDeleteList">
delete from student where id in
<foreach collection="list" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete>
/*
* 动态带条件删除
*/
public void dynamicDelete(String... ids)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession(); sqlSession.delete(Student.class.getName()+".dynamicDelete",ids); sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
} /*
* 动态带条件List批量删除
*/
public void dynamicDeleteList(List<String> ids)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession(); sqlSession.delete(Student.class.getName()+".dynamicDeleteList",ids); sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}

  (4)动态条件增加:在编写插入语句时,可通过<include refid="xx"/>标签来引入不同的sql片段,而sql片段可事先定义并配置好,通过refid的值来关联不同的片段从而实现对应字段插入对应的值。

<!-- 可通过<include refid="xx"/>标签来引入不同的sql片段,如<include refid="key"/>表示参数对应的表字段
<include refid="value"/> 表示字段对应的值-->
<insert id="dynamicInsert" parameterType="dynamicstudent">
insert into student(<include refid="key"/>) values(<include refid="value"/>)
</insert> <!-- SQL片段对应字段名 -->
<sql id="key">
<if test="id!=null">
id,
</if>
<if test="name!=null">
name,
</if>
<if test="age!=null">
age,
</if>
<if test="sex!=null">
sex
</if>
</sql> <!-- SQL片段对应占位符? -->
<sql id="value">
<if test="id!=null">
#{id},
</if>
<if test="name!=null">
#{name},
</if>
<if test="age!=null">
#{age},
</if>
<if test="sex!=null">
#{sex}
</if>
</sql>
/*
* 动态插入数据
*/
public void dynamicInsert(Student stu)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession(); sqlSession.insert(Student.class.getName()+".dynamicInsert", stu); sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}

Mybatis分页查询与动态SQL的更多相关文章

  1. MyBatis学习总结(三)——多表关联查询与动态SQL

    在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...

  2. Mybatis之关联查询及动态SQL

    前言 实际开发项目中,很少是针对单表操作,基本都会联查多表进行操作,尤其是出一些报表的内容.此时,就可以使用Mybatis的关联查询还有动态SQL.前几篇文章已经介绍过了怎么调用及相关内容,因此这里只 ...

  3. MyBatis学习总结_11_MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  4. SSM框架之Mybatis(6)动态SQL

    Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...

  5. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  6. Spring mybatis源码篇章-动态SQL节点源码深入

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...

  7. MyBatis中的条件查询(动态sql)

    本文将介绍使用MyBatis框架,编写DAO层接口类和接口类对应的sql映射文件,使用动态sql查询满足条件的用户集合. 首先,需要创建一个实体类User,供封装数据使用: package com.x ...

  8. 持久层之 MyBatis: 第二篇 :动态SQL And多表查询

    MyBatis入门到精通 完整CRUD UserDaoImpl 编写UserDao对应的UserDaoMapper.xml 添加UserDao的测试用例 编写UserDao的测试用例 解决数据库字段名 ...

  9. Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider(8)

    1)动态语言注解(2)@Provider使用思路(3)@SelectProvider小试牛刀(4)@SelectProvider初露锋芒(5)@SelectProvider过关斩将(6)@Insert ...

随机推荐

  1. mybatis由浅入深day02_5resultMap总结

    5 resultMap总结 resultType: 作用: 将查询结果按照sql列名pojo属性名一致性映射到pojo中. 场合: 常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示 ...

  2. Java集合----Map集合

    Map Map 用于保存具有映射关系的数据,因此 Map 集合里保存着两组值,一组值用于保存 Map 里的 Key,另外一组用于保存 Map 里的 Value Map 中的 key 和 value 都 ...

  3. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  4. Javascript 字符串替换

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  5. 【RF库Collections测试】Set To Dictionary

    Name:Set To DictionarySource:Collections <test library>Arguments:[ dictionary | *key_value_pai ...

  6. Python 对目录做遍历

    os.walk() 用于遍历目录里的文件,或者说迭代目录里的文件 ??? 什么意思?比如 Linux 中 find /data -type f 表示找出 /data 目录下的所有文件,它会先找出 /d ...

  7. Python 收集主机信息

    写一个 python 脚本,收集以下信息 ( CentOS 6 ) : IP地址 <ip>    主机名 <hostname>    操作系统版本 <osver>  ...

  8. JS-比较函数中嵌套函数,可以排序【对象数组】

    function createCompareFun(propertyName){ return function(object1,object2){ var value1 = object1[prop ...

  9. 9.Node.js 包管理器npm

    npm 是 Node.js  官方提供的包管理工具, 用于 Node.js包的发布.传播.依赖控制 安装 express ==> 流行的基于Node.js的Web开发框架,可以快速地搭建一个完整 ...

  10. 【BZOJ4149】[AMPPZ2014]Global Warming 单调栈+RMQ+二分

    [BZOJ4149][AMPPZ2014]Global Warming Description 给定一个序列a[1],a[2],...,a[n].请从中选出一段连续子序列,使得该区间最小值唯一.最大值 ...