1. 正文

1. set标签 和 foreach标签 trim标签 sql片段

2. mybatis映射文件处理特殊字符.

3. mybatis完成模糊查询。

4. 联表查询

2. 动态sql

2.1 set标签

这个配合if标签一起用,一般用在修改语句。如果传递的参数值为null,那么应该不修改该列的值。

<!--set:可以帮我们生成关键字 set 并且可以去除最后一个逗号--><update id="update">update account<set><if test="name!=null and name!=''">name=#{name},</if><if test="money!=null">money=#{money},</if><if test="isdeleted!=null">isdeleted=#{isdeleted},</if><if test="created!=null">created=#{created},</if><if test="updated!=null">updated=#{updated},</if></set>where id=#{id}</update>

2.2 foreach标签

循环标签.

查询:

<!-- select * from account where id in(2,3,5,7,0)如果你使用的为数组array  如果你使用的为集合 那么就用listcollection:类型item:数组中每个元素赋值的变量名open: 以谁开始close:以谁结束separator:分割符--><select id="findByIds" resultType="com.zjh.entity.Account">select * from account where id in<foreach collection="array" item="id" open="(" close=")" separator=",">#{id}</foreach></select>

删除:

 <delete id="batchDelete"><foreach collection="array" item="id" open="delete from account where  id in(" close=")" separator=",">#{id}</foreach></delete>

添加:

insert into account(name,isdeleted) values('ldh',0),('ldh2',0),('ldh4',0)<insert id="saveBatch">insert into account(name,isdeleted) values<foreach collection="list" item="acc" separator=",">(#{acc.name},#{acc.isdeleted})</foreach></insert>

2.3 sql片段

在执行查询语句时不建议大家使用select *, 建议大家把查询的列写出。

3. mybatis映射文件处理特殊字符.

   <!--第一种:转义标签 &nbsp; &lt;  第二种: <![CDATA[sql]]>--><select id="findByMaxMin" resultType="com.zjh.entity.Account"><![CDATA[select * from account where id >#{min} and id <#{max}]]></select>

4. mybatis完成模糊查询。

select * from 表名 where 列名 like '%a%'

(1)使用字符串函数 完成拼接 concat

<select id="findByLike" resultType="com.zjh.entity.Account">select * from account where name like concat('%',#{name},'%')
</select>

(2) 使用${}

<select id="findByLike" resultType="com.zjh.entity.Account">select * from account where name like '%${name}%'
</select>

通过观察: 发现使用${}实际上是字符串拼接,它不能防止sql注入, 而#{}它是预编译,它可以防止sql注入问题,#{}实际使用的PreparedStatement.

总结:

动态sql标签: if where (choose when otherwise) set foreach sql 处理特殊字符: <![CDATA[sql]]> 转义符 模糊查询: concat('',#{},'')   ${}

5. 联表查询

  1. 多对一 : 从多的一方来查询一的一方。

班级表:

|1-n

学生表:

根据学生id查询学生信息并携带班级信息。

select * from tb_stu s join tb_class c on s.class_id=c.cid where stu_id=1

实体类:

package com.zjh.entity;
​
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
​
/*** @author: jh* @create: 2022/6/2*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {private int id;private String name;private int age;private String sex;private Integer classId;
​private Clazz clazz;//学生所属的班级
}
//如何把联表查询体现到实体类上。
package com.zjh.entity;
​
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
​
/*** @author: jh* @create: 2022/6/2*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Clazz {private Integer cid;private String cname;
​
}

xml:

<resultMap id="baseMap" type="com.zjh.entity.Student"><id column="stu_id" property="id"/><result column="stu_name" property="name"/><result column="stu_age" property="age"/><result column="sex" property="sex"/><result column="class_id" property="classId"/><!--association: 表示一的一方property: 它表示属性名javaType: 该属性名对应的数据类型--><association property="clazz" javaType="com.zjh.entity.Clazz"><id column="cid" property="cid"/><result column="cname" property="cname"/></association></resultMap><select id="findStudentById" resultMap="baseMap">select * from tb_stu s join tb_class c on s.class_id=c.cid where stu_id=#{id}</select>

上面你要是绝的没有懂,那么这里可以使用的一个笨的方式: 但是不推荐。

返回类型就用map封装

//根据学生编号查询学员信息以及班级信息public Map findById(Integer id);<!--key:value--><select id="findById" resultType="java.util.Map">select <include refid="aa"/> from tb_stu s join tb_class c on s.class_id=c.id where s.id=#{id}</select>

动态SQL 模糊查询 联表查询相关推荐

  1. SQL--高级查询--联表查询

    --统计函数: --count总数.max最大.min最小.avg平均.sum求和 select * from Student --求出 学生表中的数据总条数 --count:求数据总条数 selec ...

  2. 修改 连接层_Mybatis连接池_动态sql语句_多表查询实现

    Mybatis连接池 Mybatis中的连接池Mybatis连接池提供了3种方式的配置:配置的位置:主配置文件SqlMapConfig.xml中的dataSource标签,type属性就是表示采用何种 ...

  3. 联表查询和嵌套查询—读懂数据库仓储

    数据仓储不像java.C++.JS等开发语言,而是数据库仓储更多用于对接产品工作,查询数据.分析数据.得出产品未来发展方向,与产品经理相关联,而联表查询和嵌套查询作为数据库基础的查询方法,学习使用,方 ...

  4. 什么是联表查询?(深度刨析,建议收藏)

    MySQL十六;联表查询 -- =================联表查询 join========================== -- 查询 参加了考试的同学(学号,姓名,科目编号,分数) S ...

  5. MyBatis——占位符,转义字符,多元素查询(模糊查询),动态sql(多条件中多查询,多条件中单查询)

    占位符:${},#{} ${}:充当占位符时,无法防止sql注入,纯纯的外面给啥,他就往上放啥 #{}:充当-,可以防止sql注入 实体类: package com.itjh.pojo;public ...

  6. mysql 子查询模糊匹配_sql中的查询(模糊查询,子查询,联表查询)

    1.查出每个部门不等于经理的工资 select from emp where sal <>all(select min (sal) from emp where job='manager' ...

  7. java sql 联表查询系统_Spring Hibernate JPA 联表查询 复杂查询(转)

    关系类型Owning-SideInverse-Side one-to-one @OneToOne @OneToOne(mappedBy="othersideName") one-t ...

  8. SQL语句详解(四)——SQL联表查询

    今天我们继续给大家介绍MySQL相关知识,本文主要内容是SQL联表查询. 一.SQL联表查询简介 在前文SQL语句详解(三)--SQL子查询中,我们给大家介绍了SQL子查询的相关知识和使用示例.实际上 ...

  9. 动态sql模糊查询和分页

    文章目录 动态sql 模糊查询 查询返回结果集的处理 分页 动态sql BookMapper xml bookservice 测试: MapperSql.test 运行: 模糊查询 BookMappe ...

最新文章

  1. 从哲学角度分析,框架的[无用之用]效果
  2. An error has occurred while drawing:java.lang.IllegalStateException: The display list is not valid.
  3. the vmware authorization service is not running
  4. yum安装 vs 源码编译安装
  5. [转]苦逼男和女神之间的经典对话,亲身经历过的有木有啊,必须转。。。
  6. Oracle技术之串行隔离对延迟段和INTERVAL分区的支持
  7. java游戏代码_Java与Kotlin系列文章之性能问题详解
  8. 计算机网络基础系列(三)网络应用
  9. iOS开发GCD(3)-数据安全
  10. Apache Shiro(六)-基于URL配置权限
  11. java成员方法tostring_Java 工具类-toString
  12. #Linux Shell 脚本编程(10)—文本过滤(合并与分割—sort、uniq、join、cut、paste、split)
  13. 间歇性孤独症,我喜欢的,是你刚好在我身边
  14. 【数理统计】卡方检验
  15. 拒绝反爬虫!教你搞定爬虫验证码
  16. win7免费升级win10(正版)!!!
  17. windows如何安装SVN
  18. 采用Matlab解决最小曼哈顿图问题
  19. js中clearInterval的重新执行/重新开始
  20. 插画素材哪里找?5个超级实用的插画素材库推荐

热门文章

  1. 黑龙江第三方软件测试机构 CMA/CNAS双资质
  2. 信息学奥赛一本通1258:数字金字塔
  3. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)...
  4. 线性代数导论32——基变换和图像压缩
  5. 嵌入式主板的应用领域
  6. 升级到OPENWRT 19.07后LUCI报错
  7. 常见的邮箱的pop和imap以及smtp地址整理
  8. 设计测试用例的六种方法
  9. 使用全局阈值进行灰度图像二值化
  10. 《薛兆丰的经济学课》课程总结3--生命有限