Mpper.xml映射文件中定义了操作数据库的sql,并且提供了各种标签方法实现动态拼接sql。每个sql是一个statement,映射文件是mybatis的核心。

一、内容标签

1、NamePlace

NamePlace命名空间作用就是对sql进行分类化管理。若使用Dao开发方式,映射文件的nameplace可以任意命名;但如果采用的是Mapper接口代理的方式开发,Mapper的映射文件中namespace必须为接口的全名。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Mapper.EmpMapper">
//CURD操作标签
        //if片段
</mapper>

2、CRUD标签

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Mapper.EmpMapper">
<!-- 查询 -->
<select id="" parameterType="" resultType=""></select> <!-- 添加 -->
<insert id="" parameterType=""></insert> <!-- 删除 -->
<delete id="" parameterType=""></delete> <!-- 更新 -->
<update id="" parameterType=""></update>
</mapper>

3、标签调用方法

(1)selectOne与selectList方法

selectOne表示查询出一条结果集进行映射,使用selectOne查询多条记录会抛出异常。selectList表示查询出一个列表(多条记录)进行映射,对于使用selectOne可以实现的查询,使用selectList必然也可以实现(list中只有一个对象)。

(3)代理对象内部调用

动态代理对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定,如果mapper方法返回单个pojo对象(非集合对象),代理对象内部通过selectOne查询数据库。如果mapper方法返回集合对象,代理对象内部通过selectList查询数据库。

二、动态SQL标签

1、if标签

//进行空字符串校验
<select id="findUserList" parameterType="user" resultType="user">
select * from user where 1=1
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</select>

2、where标签

//<where/>可以自动处理第一个and
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>

3、sql片段

Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:

//建立sql片段
<sql id="query_user_where">
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</sql> //使用include引用sql片段
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<include refid="query_user_where"/>
</where>
</select> //引用其它mapper.xml的sql片段
<include refid="namespace.sql片段"/>

三、foreach标签

1、通过pojo类传递list

向sql传递数组或List,mybatis使用foreach解析,foreach参数定义如下:collection指定输入 对象中集合属性,  item每个遍历生成对象中,open开始遍历时拼接的串,close结束遍历时拼接的串,separator:遍历的两个对象中需要拼接的串。

(1)sql语句

SELECT * FROM USERS WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16)
SELECT * FROM USERS WHERE username LIKE '%张%' id IN (10,89,16)

(2)vo类

public class QueryVo{
private User user;
        private UserCustom userCustom;
//传递多个用户id
private List<Integer> ids;
set()/get() ...
}

(3)映射文件

<select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom">
     SELECT * FROM USER
     <where>
        <!-- 使用实现下边的sql拼接: AND (id=1 OR id=10 OR id=16) -->

<if test="ids!=null and ids.size>0">

<foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">

id=#{user_id}

</foreach>

</if>
     </where>
</select>

<!-- 使用实现下边的sql拼接: and id IN(1,10,16)—>

<foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">

#{user_id}

</foreach>

(4)测试代码

List<Integer> ids = new ArrayList<Integer>();
ids.add(1);//查询id为1的用户
ids.add(10); //查询id为10的用户
queryVo.setIds(ids);
List<User> list = userMapper.findUserList(queryVo);

2、传递单个list

(1)Mapper映射文件

<select id="selectUserByList" parameterType="java.util.List" resultType="user">
select * from user
<where>
<!-- 传递List,List中是pojo -->
<if test="list!=null">
<foreach collection="list" item="item" open="and id in( "separator="," close=")">
#{item.id}
</foreach>
</if>
</where>
</select>

(2)Mapper接口

public List<User> selectUserByList(List userlist);

(3)测试程序

//构造查询条件List
List<User> userlist = new ArrayList<User>();
User user = new User();
user.setId(1);
userlist.add(user); user = new User();
user.setId(2);
userlist.add(user);
//传递userlist列表查询用户列表
List<User>list = userMapper.selectUserByList(userlist);

3、传递pojo类数组

(1)Mapper映射文件

参数含义:index为数组的下标,item为数组每个元素的名称,名称随意定义,open循环开始,close循环结束,separator中间分隔输出。

<select id="selectUserByArray" parameterType="Object[]" resultType="user">
select * from user
<where>
<!-- 传递pojo类数组 -->
<if test="array!=null">
<foreach collection="array" index="index" item="item"
open="and id in("separator=","close=")">
#{item.id}
</foreach>
</if>
</where>
</select>

(2)Mapper接口

public List<User> selectUserByArray(Object[] userlist)

(3)测试程序

//构造查询条件List
Object[] userlist = new Object[2];
User user = new User();
user.setId(1);
userlist[0]=user; user = new User();
user.setId(2);
userlist[1]=user; //传递user对象查询用户列表
List<User>list = userMapper.selectUserByArray(userlist);

4、传递字符串类数组

(1)Mapper映射文件

<select id="selectUserByArray" parameterType="Object[]" resultType="user">
select * from user
<where>
<!-- 传递字符串数组 -->
<if test="array!=null">
<foreach collection="array"index="index"item="item"
open="and id in("separator=","close=")">
#{item}
</foreach>
</if>
</where>
</select>

如果数组中是简单类型则写为#{item},不用再通过ognl获取对象属性值了。

(2)Mapper接口

public List<User> selectUserByArray(Object[] userlist)

(3)测试程序

//构造查询条件List
Object[] userlist = new Object[2];
userlist[0]=”1”;
userlist[1]=”2”;
//传递user对象查询用户列表
List<User>list = userMapper.selectUserByArray(userlist);

Mybatis笔记 - SQL标签方法的更多相关文章

  1. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  2. Mybatis总结一之SQL标签方法

    ---恢复内容开始--- 定义:mapper.xml映射文件中定义了操作数据库的sql,并且提供了各种标签方法实现动态拼接sql.每个sql是一个statement,映射文件是mybatis的核心. ...

  3. mybatis 07: sql标签中 "#{}" 和 "${}" 的作用和比较

    "#{}"占位符 作用 传参大部分使用"#{}",在数据库底层使用的是:PreparedStatement预编译处理对象 数据库底层被解析为"?&qu ...

  4. mybatis中sql标签和include标签

    1.首先定义一个sql标签,一定要定义唯一id.(name,age是要查询的字段) <sql id="Base_Column_List" >name,age</s ...

  5. Mybatis 动态sql标签

    1.动态SQL片段 通过SQL片段达到代码复用         <!-- 动态条件分页查询 -->          <sql id="sql_count"> ...

  6. mybatis动态SQL标签的用法

    动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格 ...

  7. mybatis中sql标签、where标签、foreach标签用法

    <sql id="query_user_where"> <!-- 如果 userQueryVo中传入查询条件,再进行sql拼接--> <!-- tes ...

  8. MyBatis 动态sql标签trim

    https://blog.csdn.net/zhangxing52077/article/details/75041053 序列序号在Mybatis中的使用的使用 将获得序列值获取返回到对象code字 ...

  9. MyBatis动态Sql 的使用

    Mapper.xml提示: 1:mapper包中新建一个文件:mybatis-3-mapper.dtd 2:在web app libraries/mybatis.jar/org.apache.ibat ...

随机推荐

  1. JPA project Change Event Handler问题解决

    eclipse使用的是有经常会出现JPA project Change Event Handler(watering)很卡 网上的解决办法是 [Help > Installation Detai ...

  2. 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 构建 RESTful Flask API 定义资源路由 格式 ...

  3. Iconv作用以及安装问题解决

    当我们在使用Window操作系统的时候,可能使用最多的文本格式就是txt了,但是当我们将Window平台下的txt文本文档拷贝到Linux平台下查看时,发现原来的中文全部变成了乱码.没错, 引起这个结 ...

  4. 利用Graphziv帮助理解复杂的类层次关系

    最近在学习osg三维视景仿真平台,学习的过程中涉及到许多的类与类之间的继承和包含关系.在复杂点的例子中,许多的类和节点组合在一起,很容易让人迷失方向.在编译源代码的时候,无意间发现了Graphviz这 ...

  5. Cocos2d-x之MessageBox

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 在cocos2d-x中使用的对话框为MessageBox,因为cocos2d-x已经包装好了:但是在一般的游戏设置中,我们不会使用coco ...

  6. leetcode.双指针.680验证回文字符串-Java

    1. 具体题目 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...

  7. C++中类的静态成员变量

    1,成员变量的回顾: 1,通过对象名能够访问 public 成员变量: 2,每个对象的成员变量都是专属的: 3,成员变量不能在对象之间共享: 1,在做程序设计中,成员变量一般是私有的.至少不是公有的: ...

  8. python系列——文件操作的代码

    import numpy as np import os,sys #获取当前文件夹,并根据文件名 def path(fileName): p=sys.path[0]+'\\'+fileName ret ...

  9. Codefores 507D The Maths Lecture( 数位DP )

    D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. Python之复数、分数、大型数组数学运算(complex、cmath、numpy、fractions)

    一.复数的数学运算 复数可以用使用函数 complex(real, imag) 或者是带有后缀j的浮点数来指定 a=complex(2,4) print(a) # (2+4j) b=2-5j # 获取 ...