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. HTML最全标签

    一.HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(DTD).   标签:a 说明:标明超链接的起始或目的位置.   标签:acronym 说明:标明缩写词.   ...

  2. ucenter 整合同步登录的内部实现原理

    1.用户登录discuz,通过logging.php文件中的函数uc_user_login对post过来的数据进行验证,也就是对username和password进行验证. 2.如果验证成功,将调用位 ...

  3. CET-6 分频周计划生词筛选(Week 2)

    点我阅读 Week 2 2016.09.04/05 p58 ongoing / forward p59 prosperity p60 rear p61 rival + segregation + se ...

  4. HTTP面试题目

    转自:http://m.blog.csdn.net/zhangliangzi/article/details/51336564 1.说一下什么是Http协议? 对器客户端和 服务器端之间数据传输的格式 ...

  5. 详解JavaScript数组过滤相同元素的5种方法

    详解JavaScript数组过滤相同元素的5种方法:https://www.jb51.net/article/114490.htm

  6. springCloud的使用05-----路由网关(zuul)

    zuul的主要功能是路由转发和过滤,比如让所有/api-a/*的请求都转发到服务器a上,所有/api-b/*的请求都转发到服务器b上,zuul默认和ribbon结合实现了负载均衡的功能. 1 zuul ...

  7. go 学习之函数

    个人把go函数理解分三种: 1.普通函数 普通函数声明: func name(parameter-list) (result-list) { body} package main import &qu ...

  8. python socket的长连接和短连接

    前言 socket中意为插座,属于进程间通信的一种方式.socket库隐藏了底层,让我们更好的专注于逻辑.如果短连接和长连接两概率没搞明白,会被坑的爬不起来. 短连接 一次完整的传输过程,发送方输出流 ...

  9. swap的几点理解

    一.什么是swap space(交换分区)? 在Linux系统中,当物理内存满了才使用Swap空间.当系统需要更多的内存资源,并且物理内存已经满了,此时,内存中那些不活跃的pages被移动(move) ...

  10. C++在windows平台下不存在strptime函数,可以绕过该函数

    https://blog.csdn.net/u011077672/article/details/50524469?utm_source=blogxgwz1