首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,110 阅读
2
类的加载
754 阅读
3
Spring Cloud OAuth2.0
735 阅读
4
SpringBoot自动装配原理
698 阅读
5
集合不安全问题
594 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
相聚有时,后会无期
累计撰写
392
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
页面
统计
关于
搜索到
1
篇与
的结果
2020-12-14
动态SQL
动态SQL==根据不同的条件生成不同的SQL语句==ifchoose (when, otherwise)trim (where, set)foreach搭建环境CREATE TABLE `blog`( `id` VARCHAR(50) NOT NULL COMMENT 'id', `title` VARCHAR(100) NOT NULL COMMENT '标题', `author` VARCHAR(50) NOT NULL COMMENT '作者', `create_time` datetime NOT NULL COMMENT '创建时间', `views` int(11) NOT NULL COMMENT '浏览量' )ENGINE=INNODB DEFAULT CHARSET=utf8;1、导包2、编写配置文件3、创建实体类package com.sw.pojo; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * @Author suaxi * @Date 2020/12/13 16:20 */ @Data public class blog implements Serializable { private String id; private String title; private String author; private Date createTime; private int views; } 4、编写实体类对应的Mapper接口和Mapper.xml文件IF<select id="findBlogIf" parameterType="map" resultType="Blog"> select * from blog where 1=1 <if test="title != null"> and title = #{title} </if> <if test="author != null"> and author = #{author} </if> </select>chose(when,otherwise)<select id="findBlogChoose" 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>trim(where,set) <select id="findBlogIf" 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><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>SQL片段将部分SQl抽取出来,方便复用1、使用SQL标签抽取公共部分<sql id="if-title-author"> <if test="title != null"> title = #{title} </if> <if test="author != null"> and author = #{author} </if> </sql>2、在需要使用的地方用include标签引用、<select id="findBlogIf" parameterType="map" resultType="Blog"> select * from blog <where> <include refid="if-title-author"></include> </where> </select>注:最好基于单表查询,不要存在where标签Foreach<!--select * from bolg where 1=1 and (id=1 or id=2 or id=3)--> <select id="findBlogForeach" parameterType="map" resultType="Blog"> select * from blog <where> <foreach collection="ids" item="id" open="and (" close=")" separator="or"> id = #{id} </foreach> </where> </select>==动态SQL就是在拼接SQL语句,先确保SQL的正确性,再按照SQL格式去排列组合==先在MySQL中写出完整的sql,再去修改成动态sql实现通用
2020年12月14日
109 阅读
0 评论
0 点赞