Mybatis笔记 - SQL标签方法
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标签方法的更多相关文章
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...
- Mybatis总结一之SQL标签方法
---恢复内容开始--- 定义:mapper.xml映射文件中定义了操作数据库的sql,并且提供了各种标签方法实现动态拼接sql.每个sql是一个statement,映射文件是mybatis的核心. ...
- mybatis 07: sql标签中 "#{}" 和 "${}" 的作用和比较
"#{}"占位符 作用 传参大部分使用"#{}",在数据库底层使用的是:PreparedStatement预编译处理对象 数据库底层被解析为"?&qu ...
- mybatis中sql标签和include标签
1.首先定义一个sql标签,一定要定义唯一id.(name,age是要查询的字段) <sql id="Base_Column_List" >name,age</s ...
- Mybatis 动态sql标签
1.动态SQL片段 通过SQL片段达到代码复用 <!-- 动态条件分页查询 --> <sql id="sql_count"> ...
- mybatis动态SQL标签的用法
动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格 ...
- mybatis中sql标签、where标签、foreach标签用法
<sql id="query_user_where"> <!-- 如果 userQueryVo中传入查询条件,再进行sql拼接--> <!-- tes ...
- MyBatis 动态sql标签trim
https://blog.csdn.net/zhangxing52077/article/details/75041053 序列序号在Mybatis中的使用的使用 将获得序列值获取返回到对象code字 ...
- MyBatis动态Sql 的使用
Mapper.xml提示: 1:mapper包中新建一个文件:mybatis-3-mapper.dtd 2:在web app libraries/mybatis.jar/org.apache.ibat ...
随机推荐
- EditText设置/隐藏光标位置、选中文本和获取/清除焦点(转)
转:http://blog.csdn.net/dajian790626/article/details/8464722 有时候需要让光标显示在EditText的指定位置或者选中某些文本.同样,为了方便 ...
- 左手Mongodb右手Redis 通过python连接mongodb
首先需要安装第三方包pymongo pip install pymongodb """ 通过python连接mongodb数据库 首先需要初始化数据库连接 "& ...
- appium定位学习
前面也介绍过appium的一些定位方法,今天看到一篇博客,里面的方法总结的,就转载过来. 本文转自:https://www.cnblogs.com/Mushishi_xu/p/7685966.html ...
- Nginx反向代理维基百科镜像制作全解析
近日做的Ngnx代理测试,基于Nginx的ngx_http_substitutions_filter_module模块,并利用UA跳转实现PC/移动端不同站点跳转. 1. 关于Nginx的代理详细请转 ...
- Workflow:Workflow 目录
ylbtech-Workflow:Workflow 目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:ht ...
- svn设置文件提交过滤、svn设置classes文件提交
在svn提交文件的时候为了避免一些不必要的文件也提交到资源库 像编译后的.class文件 第一步:在文件中右击打开设置, 第二步:找到全局忽略样式 第三步:修改要过滤的文件 设置过滤通配符 *clas ...
- haskell目录层次
daniel@daniel-mint /usr/lib/ghc/haskell2010-1.1.1.0 $ tree . ├── Control │ └── Monad.hi ├── Data │ ...
- 【JavaScript性能优化】------理解Script标签的加载和执行
1.script标签是如何加载的?当浏览器遇到一个 < script>标签时,浏览器会停下来,运行JavaScript代码,然后再继续解析.翻译页面.同样的事情发生在使用 src 属性加载 ...
- BZOJ 2724蒲公英 (分块) 【内有块大小证明】
题面 luogu传送门 分析 先分块,设块大小为x(之后我们会证明块大小取何值会更优) 步骤1 把所有的数离散化,然后对每个值开一个vector pos[i],pos[i]存储数i出现的位置 我们设查 ...
- 35.Unique Paths(不同的路径)
Level: Medium 题目描述: A robot is located at the top-left corner of a m x n grid (marked 'Start' in ...