MyBatis06——动态SQL
动态SQL
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
搭建环境
1、搭建数据库
CREATE TABLE `blog` (
`id` varchar(50) NOT NULL COMMENT '博客id',
`title` varchar(100) NOT NULL COMMENT '博客标题',
`author` varchar(30) NOT NULL COMMENT '博客作者',
`create_time` datetime NOT NULL COMMENT '创建时间',
`views` int(30) NOT NULL COMMENT '浏览量'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
2、实体类编写
@Data
public class Blog {
private String id;
private String title;
private String author;
private Date createTime;
private int views;
//set,get....
}
3、编写Mapper接口以及xml文件
public interface BlogMapper {
}
<?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="com.kuang.mapper.BlogMapper">
</mapper>
4、在setting中开启驼峰自动转换
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--注册Mapper.xml-->
<mappers>
<mapper resource="mapper/BlogMapper.xml"/>
</mappers>
5、插入初始数据
编写接口
//新增一个博客
int addBlog(Blog blog);
sql配置文件
<insert id="addBlog" parameterType="blog">
insert into blog (id, title, author, create_time, views)
values (#{id},#{title},#{author},#{createTime},#{views});
</insert>
IF语句
select * from blog where title = #{title} and author = #{author}
-->
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from blog where
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
where语句
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
set语句
<!--注意set是用的逗号隔开-->
<update id="updateBlog" parameterType="map">
update blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author}
</if>
</set>
where id = #{id};
</update>
choose语句
<select id="queryBlogChoose" parameterType="map" resultType="blog">
select * from blog
<where>
<choose>
<when test="title != null">
title = #{title}
</when>
<when test="author != null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
SQL片段
<sql id="if-title-author">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
引用SQL片段
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from blog
<where>
<!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace -->
<include refid="if-title-author"></include>
<!-- 在这里还可以引用其他的 sql 片段 -->
</where>
</select>
Foreach
批量查询
1、编写接口
//批量查询
List<Blog> queryBlogForeach(Map map);
2、编写SQL语句
<select id="queryBlogForeach" parameterType="map" resultType="blog">
select * from blog
<where>
<!--
collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
select * from blog where 1=1 and (id=1 or id=2 or id=3)
-->
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
批量插入
1、编写接口
int addUserForeach(@Param("list") List<Map> list);
2、编写SQL语句
<!-- 批量插入信息-->
<insert id="addUserForeach" parameterType="list">
insert into user
(id,name,pwd)
values
<foreach collection="list" item="user" separator=",">
(
#{user.id},
#{user.name},
#{user.pwd}
)
</foreach>
</insert>
MyBatis06——动态SQL的更多相关文章
- Mybatis-06 动态Sql
Mybatis-06 动态Sql 多对一处理 多个学生,对应一个老师 对于学生这边而言,关联多个学生,关联一个老师 [多对一] 对于老师而言,集合,一个老师又很多学生 [一对多] 1.创建数据库 2. ...
- 值得注意的ibatis动态sql语法格式
一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...
- Mysql - 游标/动态sql/事务
游标这个在我目前的项目里面用的还不多, 但是其功能还是很强大的. 动态sql以前都没用过, 是跟着富士康(不是张全蛋的富土康哦)过来的同事学的. 还是挺好用的. 我的数据库方面, 跟他学了不少. 在此 ...
- MyBatis4:动态SQL
什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...
- 分享公司DAO层动态SQL的一些封装
主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...
- MySQL存储过程动态SQL语句的生成
用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...
- 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】
一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...
- 自定义函数执行动态sql语句
--函数中不能调用动态SQL,使用用存储过程吧.如果还要对函数做其他操作,换成存储过程不方便,可以考虑把其他操作一起封装在存储过程里面.如: create proc [dbo].[FUN_YSCL ...
- mybatis入门基础(五)----动态SQL
一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...
- mybatis 动态sql表达式相关应用
一.mybatis 表达式简介 对于mybatis3 ,提供了一种动态sql的方式.通过动态sql我们可以直接在mybatis 的xm映射文件中直接通过条件判断的方式进行查询添加的拼接.mybatis ...
随机推荐
- SpringMVC中资源路径映射本地文件图片
SpringMVC中资源路径映射本地文件图片 import org.springframework.context.annotation.Configuration; import org.sprin ...
- 【笔记】 springCloud-configServer配置中心
当然第一步还是得要了解啦! 介绍 做项目, 那么就少不了配置微服务架构中,配置文件众多,各个服务的配置文件也有可能不一样, Spring为我们提供了相应的配置中心组件--Spring Cloud co ...
- vulnhub - Aragog - writeup
信息收集 目标开放了80.22端口. root@Lockly temp/tmp » arp-scan -I eth1 -l Interface: eth1, type: EN10MB, MAC: 00 ...
- 聊一聊如何整合Microsoft.Extensions.DependencyInjection和Castle.Core(完结篇)
前言 书接上回,上回我们了解了 castle 代理的一些缺点,本文将开始操作整合 Microsoft.Extension.Dependency和Castle,以让默认的容器可以支持拦截器 我们将以进阶 ...
- 聊一聊如何整合Microsoft.Extensions.DependencyInjection和Castle.Core(三)
前言 今天的第三篇,感觉没啥人看呀,难道没有兄弟跟我有同样的整合需求吗???手动 , 本文会简短一些,介绍下 CastleCore 作为代理库的一些缺点甚至是硬伤 异步支持 先上代码 /// < ...
- Spring源码学习笔记6——Spring bean的实例化
一丶前言 前面我们了解到读取xml or 根据扫描路径生成BeanDefinition并注册到BeanFactory,相当于我们具备了生火做饭的原材料:BeanDefinition,接下来就是Spri ...
- 文心一言 VS 讯飞星火 VS chatgpt (23)-- 算法导论4.2 5题
五.V.Pan 发现一种方法,可以用 132 464 次乘法操作完成 68 x 68 的矩阵相乘,发现另一种方法,可以用 143 640 次乘法操作完成 70 x 70 的矩阵相乘,还发现一种方法,可 ...
- .NET技术分享日活动-202110
2021年10月15日下午,个人组织举办了山东地区的第三次.NET技术分享日活动.围绕.NET.低代码Low Code.云原生 Cloud Native.大数据.算法等方向进行创新技术的实践分享. 本 ...
- 如何更好的分析潜在人脉?聊聊华为云图引擎GES的Cypher子查询
摘要:本文以华为云图引擎 GES 为例,来介绍如何使用图查询语言 Cypher 表达一些需要做数据局部遍历的场景. 本文分享自华为云社区<使用 Cypher 子查询进行图探索 -- 以华为云图引 ...
- 云数据库 GaussDB(for Influx) 解密第十一期:让智能电网中时序数据处理更高效
摘要:GaussDB(for Influx)是一款基于计算存储分离架构,完全兼容 InfluxDB 生态的云原生时序数据库. 本文分享自华为云社区<云数据库 GaussDB(for Influx ...