Mybatis(6)动态SQL

1、动态SQL

出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了

1.1、if标签

我们根据实体类的不同取值,使用不同的 SQL 语句来进行查询。比如在 id 如果不为空时可以根据 id 查询,如果 username 不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。

1.1.1、持久层接口

/**
* 通过用户信息查询用户列表
* @param user
* @return
*/
List<User> findByUser(User user);

1.1.2、持久层映射配置

注意此处的1=1必须写,否则当if条件全为false时会出现逻辑错误

<select id="findByUser" resultMap="userMap" parameterType="user">
select * from user where 1=1
<if test="userName != null">
and username = #{userName}
</if>
<if test="userSex != null">
and sex = #{userSex}
</if>
</select>

1.1.3、测试类

@Test
public void testFindByUser(){
//通过模糊查询查找用户 User user = new User();
user.setUserName("wf");
user.setUserSex("女"); List<User> users = uesrdao.findByUser(user); for(User u:users){
System.out.println(u);
}
}

1.2、where标签

为了简化 where 1=1出现了where

1.2.1、持久层接口

同if

1.2.2、持久层映射配置

<select id="findByUser" resultMap="userMap" parameterType="user">
select * from user
<where>
<if test="userName != null">
and username = #{userName}
</if>
<if test="userSex != null">
and sex = #{userSex}
</if>
</where>
</select>

1.3、foreach标签

需求:

传入多个 id 查询用户信息,用下边两个 sql 实现:

SELECT * FROM USERS WHERE username LIKE ‘%张%’ AND (id =10 OR id =89 OR id=16)

SELECT * FROM USERS WHERE username LIKE ‘%张%’ AND id IN (10,89,16)

这样我们在进行范围查询时,就要将一个集合中的值,作为参数动态添加进来。

这样我们将如何进行参数的传递?

1.3.1、 在 QueryVo 中加入一个 List集合用于封装参数

package domain;

import java.util.List;

public class QueryVo {
private User user;
private List<Integer> ids; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public List<Integer> getIds() {
return ids;
} public void setIds(List<Integer> ids) {
this.ids = ids;
}
}

1.3.2、持久层接口

/**
* 通过集合查询id
* @param vo
* @return
*/
List<User> findByIds(QueryVo vo);

1.3.3、持久层映射配置

SQL 语句:

select 字段 from user where id in (?)

foreach标签用于遍历集合,它的属性:

collection:代表要遍历的集合元素,注意编写时不要写#{}

open:代表语句的开始部分

close:代表结束部分

​ item:代表遍历集合的每个元素,生成的变量名

sperator:代表分隔符

<select id="findByIds" resultMap="userMap" parameterType="queryvo">
select * from USER
<where>
<if test="ids != null and ids.size()>0">
<foreach collection="ids" open="id in(" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>

1.3.4、测试类

@Test
public void testFindByIds(){
//通过模糊查询查找用户 QueryVo vo = new QueryVo();
List<Integer> ids =new ArrayList<Integer>();
ids.add(41);
ids.add(43);
ids.add(52);
vo.setIds(ids);
List<User> users = uesrdao.findByIds(vo); for(User u:users){
System.out.println(u);
} }

2、Mybatis中简化编写sql语句

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

注:该配置在持久层配置中编写

2.1、定义代码片段

<!-- 抽取重复的语句代码片段 -->
<sql id="completeSql" >
select * from USER
</sql>

2.2、引用代码片段

<select id="findAll" resultMap="userMap">
<include refid="completeSql"/>
<!--select * from user-->
</select> <select id="findById" parameterType="int" resultMap="userMap">
<include refid="completeSql"/>
<!--select * from user-->
where id=#{id}
</select>

最新文章

  1. C#获取硬盘序列号的问题求助
  2. Edittext默认无焦点
  3. GLEW OpenGL Access violation when using glGenVertexArrays
  4. eclipse编辑struts.xml 代码提示
  5. Linux查看BIOS信息
  6. Linux 关闭防火墙命令
  7. 2015英特尔&#174; 实感™ (Intel&#174; RealSense™) 动手开发实验课
  8. Liunx常用的特殊环境变量
  9. jquery 之效果
  10. https原理及tomcat配置https方法
  11. JS获取table表格任意单元格值
  12. ORACLE获取字符串中数字部分
  13. 运行Myeclipse时,如何删除IVM窗口
  14. 5.LNMP(Linux + Nginx + MySQL + PHP)环境安装
  15. 【python】Numpy中stack(),hstack(),vstack()函数详解
  16. MATLAB:增加噪声,同时多次叠加噪声图和原图以及求平均图像(imnoise,imadd函数)
  17. saltstack自动化运维系列⑤之saltstack的配置管理详解
  18. python中的center
  19. 安装VS2013
  20. 定义一个复数(z=x+iy)类Complex,包含: 两个属性:实部x和虚部y 默认构造函数 Complex(),设置x=0,y=0 构造函数:Complex(int i,int j) 显示复数的方法:showComp()将其显示为如: 5+8i或5-8i 的形式。 求两个复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)public Complex addComp(Compl

热门文章

  1. 一些实用的 Laravel 小技巧
  2. bsoj5988 [Achen模拟赛]期望 题解
  3. vscode 同步扩展插件
  4. 生命周期感知 Lifecycle
  5. SQL数据库基础
  6. ConcurrentHashMap源码走读
  7. IPFS学习-分布式哈希表DHT
  8. JS---课程介绍 + JavaScript分三个部分
  9. LINE 项目总结
  10. FlowPortal 6.00c 使用xFormDesigner复制粘贴中文总是乱码