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 ...
随机推荐
- pytest框架学习-前置和后置setup和teardown
前置和后置 (1)setup和teardown,方法级 写在类中 方法级,每个用例都会执行setup和teardown. 相当于setup_method和teardown_method (2)setu ...
- Python——第一章:for循环
字符串是可迭代的for循环: for 任意变量名 in 字符串|列表|元祖|字典|集合(可迭代的东西): 代码 for循环把可迭代的东西中的每一项内容拿出来. 挨个的赋值给变量. 每一次赋值都要执行一 ...
- 在ubuntu下将virtualbox虚拟机的磁盘重设大小的方法
1.VBoxManage modifyhd /home/beyond/xxx.vdi --resize 20480 {20480(单位:M)是你要扩容之后的总大小,/home/beyond 是你存放 ...
- Terraform 的开源替代:OpenTofu 宣布 GA!
OpenTofu 社区于1月10日宣布 OpenTofu 项目 GA,这是 OpenTofu 的首个稳定版本(https://github.com/opentofu/opentofu/releases ...
- Cesium案例解析(七)——Layers在线地图服务
目录 1. 概述 2. 案例 2.1. Blue Marble 2.2. ArcGIS地形 2.3. Cesium地形 2.4. Natural Earth II 2.5. Earth at Nigh ...
- 遥居前列!华为云GaussDB再获行业权威验证
摘要:北京国家金融科技认证中心正式公布了2022年通过"分布式数据库金融标准验证"的数据库产品名单.华为云GaussDB金融级分布式数据库以突出的技术优势通过验证,跃然榜上,且测试 ...
- KubeEdge边缘计算在顺丰科技工业物联网中的实践
摘要:顺丰物联网平台负责人胡典钢为大家带来了 " 边缘计算在工业物联网中的应用实践与思考 " ,阐述了工业物联网的发展背景.整体架构设计以及边缘计算在此过程中承担的重要角色,并梳理 ...
- Go 1.18 新特性:多模块工作区模式
摘要:在 Go 1.18 推出多模块工作区模式--Multi-Module Workspaces,用以支持模块的多个工作空间,我们来看看到底有什么特别. 本文分享自华为云社区<一起看看 Go 1 ...
- 传统数据库改造难?华为云GaussDB“五心”解决
摘要:快来看看华为云GaussDB奉上的"五心"诚意吧~ 本文分享自华为云社区<传统数据库改造难?华为云GaussDB"五心"解决>,作者: Gau ...
- nodejs升级到最新LTS版本方法汇总:linux/mac/window—npm/yum/ssh
nodejs不同版本的差异还是蛮多的,比如obj?.a 在nodejs12是不支持的,必须得升级到14才可以.但是centos yum 默认安装的,或者系统集成的nodejs版本都是很老的.项目上传到 ...