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 ...
随机推荐
- (转)Java并发包:AtomicBoolean和AtomicReference
转:https://blog.csdn.net/zxc123e/article/details/52057289 文章译自:http://tutorials.jenkov.com/java-util- ...
- Linux配置Selenium+Chrome+Python
---恢复内容开始--- 最近一个月没有更新博客了.最近都在复习LINUX与PYTHON知识.决定以后在LINUX环境下学习新知识. 包括后期的框架学习与平台知识方面. 直接记录今天下午的学习成果. ...
- appium常见问题02_android内嵌H5页(webview)如何定位
现在大多数app都是由原生页面和内嵌H5(即webview)组成,app原生页面直接定位即可,那内嵌H5页面要如何定位呢. 相信大多数人用appium做自动化时都有遇到这个问题,小编总结了下工作中该问 ...
- XAMPP【phpmyadmin】外网访问被拒绝解决办法
问题场景: 在阿里云搭建一个apache服务器,正常访问XAMPP目录下的页面. 服务器本地是可以正常访问的 但是远程 就不可以访问了: 出现这样的画面: 解决方法 1.按照提示找到httpd-xam ...
- 【转载 | 翻译】Visualizing A Neural Machine Translation Model(神经机器翻译模型NMT的可视化)
转载并翻译Jay Alammar的一篇博文:Visualizing A Neural Machine Translation Model (Mechanics of Seq2seq Models Wi ...
- [Codeforces 729F] Financiers Game
题意: 两个人分别从长度为n的数列的两端开始取数,如果前一 个人取了k个数,后一个人必须取k或k+1个. 第一个人最 开始可以取1个或2个,不能操作时结束. 两个人都希望自 己取到的数字之和尽量大,并 ...
- ORCAL 数据库的约束以及SQL语言的四种类型
oracle数据库约束: 定义:要输入的这个值是一个什么样的值, 或者是哪个范围内的值 作用: 确保完整性, 确保精确性 1, 非空约束(not null) 记录一条信息的时候如果用户名和密码没有被记 ...
- Windows OS PathTooLongException 转摘自http://www.cstruter.com/blog/308
When I told one of my developer friends that I am going to write a post about the PathTooLongExcepti ...
- Educational Codeforces Round 33 D. Credit Card
Credit Card time limit per test2 seconds memory limit per test256 megabytes Recenlty Luba got a cred ...
- spring集成rabbitMq(非springboot)
首先 , pom文件需要加入spring集成rabbitMq的依赖: <dependency> <groupId>org.springframework.amqp</gr ...