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. test-hellow world!

    //for C #include<stdio.h> int main() { printf("hellow world!"); return 0; } #for pyt ...

  2. 在Chrome 中使用Vimium

    原文连接:https://blog.csdn.net/wuxianjiezh/article/details/91848604 Vimium:像在 Vim 中一样使用 Chrome 安装 使用方法 在 ...

  3. Supermap/Cesium 开发心得----定位

    SuperMap的WebGL是基于开源JS库Cesium做的修改而形成的产品,理论上用起来大同小异,如果在有不一样的地方再看,基本上还是与Cesium的接口名称和结构是一样的. 定位方法有基于Cesi ...

  4. centos安装redis并开启多个redis实例

    1.下载安装包       下载地址 :  http://download.redis.io/releases/,去里面找对应的版本下载        例如  wget http://download ...

  5. JUC-5-CountDownLatch 闭锁

      CountDownLatch 闭锁 同步辅助类 一组操作中,多个线程完成,  闭锁会允许一个或多个线程一直等待.   即 所有线程都完成才继续执行  

  6. C#_.NetCore_Web项目_EXCEL数据导出(ExcelHelper_第一版)

    项目需要引用NPOI的Nuget包:DotNetCore.NPOI-v1.2.2 A-前端触发下载Excel的方法有三种: 1-JS-Url跳转请求-后台需要返回文件流数据: window.Locat ...

  7. JavaWeb学习——Servlet相关的接口和类

    JavaWeb学习——Servlet相关的接口和类 摘要:本文主要学习了Servlet相关的接口和类. Servlet的接口和类 三种方式 实现Servlet有三种方式: 实现javax.servle ...

  8. C lang:Protect array data——Const

    Xx_Introduction Use pointer translate parameter array original data will change data,and use const p ...

  9. Java实现Kafka的生产者和消费者例子

    Kafka的结构与RabbitMQ类似,消息生产者向Kafka服务器发送消息,Kafka接收消息后,再投递给消费者.生产者的消费会被发送到Topic中,Topic中保存着各类数据,每一条数据都使用键. ...

  10. LeetCode刷题191126

    博主渣渣一枚,刷刷leetcode给自己瞅瞅,大神们由更好方法还望不吝赐教.题目及解法来自于力扣(LeetCode),传送门. 今天状态不好,划水第二天. 算法: 题号:20 给定一个只包括 '(', ...