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>

SSM框架之Mybatis(6)动态SQL的更多相关文章

  1. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  2. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

  3. MyBatis的动态SQL详解-各种标签使用

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  4. 9、SpringBoot+Mybatis整合------动态sql

    开发工具:STS 前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 动态sql中的语法: where标签 if标签 trim标签 set标签 s ...

  5. Java-MyBatis:MyBatis 3 动态 SQL

    ylbtech-Java-MyBatis:MyBatis 3 动态 SQL 1.返回顶部 1. 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架 ...

  6. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  7. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

  8. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  9. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

  10. mybatis.5.动态SQL

    1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...

随机推荐

  1. C#mvc重新定向并在路径中使用html扩展名实现伪静态

    首先修改配置文件,增加下面的两个配置: 接下来,修改MapRoute为路由增加.html后缀 完成后,我们来验证一下刚才的成果: http://localhost:2279/Home/.html 一个 ...

  2. C#线程学习笔记七:Task详细用法

    一.Task类简介: Task类是在.NET Framework 4.0中提供的新功能,主要用于异步操作的控制.它比Thread和ThreadPool提供了更为强大的功能,并且更方便使用. Task和 ...

  3. c++-继承的学习

    继承的基本概念 继承和派生 继承概念 派生类的访问控制(继承三种方式.类三种访问控制.三看原则)综合训练 继承中的构造和析构 类型兼容性原则 继承中的构造和析构 继承中同名成员函数.成员变量处理方法 ...

  4. php使用微信登录

    1.第一步 $hosturl = urlencode('');//异步回调地址 $wechatInfo = WechatInfo::get_wechat(); //查询appid $url = &qu ...

  5. wx-icon和progress

    基本内容 index.wxml <!--index.wxml--> <view class="container"> <!--icon text pr ...

  6. postgreSQL安装教程 Windows

    Windows 上安装 PostgreSQL 这里使用 EnterpriseDB 来下载安装,EnterpriseDB 是全球唯一一家提供基于 PostgreSQL 企业级产品与服务的厂商. 下载地址 ...

  7. FCC---CSS Flexbox: Add Flex Superpowers to the Tweet Embed

    To the right is the tweet embed that will be used as the practical example. Some of the elements wou ...

  8. 搭建mount+nfs远程挂载

    需求背景: 192.168.10.100 源服务器 目录:/root/test 目录属主属组普通用户,权限777 192.168.10.111 目标服务器 目录:/root/test111 目录属主属 ...

  9. QGIS 3.4 3.6 另存栅格图层到GeoPackage出现覆盖问题 解决方案

    转载请声明:博客园 @秋意正寒 升级你的QGIS到3.8或以上 这在3.4.x和3.6.x都存在同样的问题.在老版本QGIS重,如果新建一个GeoPackage,先存入栅格数据就没有这个问题,但是如果 ...

  10. stdc++.6.0.9动态库缺失

    问题 ld: library not found for -lstdc++.6.0.9 clang: error: linker command failed with exit code 1 (us ...