动态SQL
2021/8/22 2:06:01
本文主要是介绍动态SQL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
动态SQL
- https://mybatis.org/mybatis-3/zh/dynamic-sql.html
搭建环境
- sql
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;
- 导包
- 编写配置文件
- 编写实体类
@Data @AllArgsConstructor @NoArgsConstructor public class Blog { private int id; private String title; public String author; private Date createTime; private int views; }
- 编写对应的Mapper接口和Mapper.xml文件
If
- BlogMapper.java
public interface BlogMapper { List<Blog> getBlogs(); List<Blog> queryBlogIf(Map map); }
- BlogMapper.xml
<?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.dao.BlogMapper"> <resultMap id="myBlog" type="com.pojo.Blog"> <result property="createTime" column="create_time"/> </resultMap> <select id="getBlogs" resultMap="myBlog"> select * from blog </select> <select id="queryBlogIf" parameterType="map" resultMap="myBlog"> select * from blog where 1=1 <if test="title != null"> and title = #{title} </if> <if test="author!=null"> and author = #{aotuor} </if> </select> </mapper>
- test
@Test public void test03(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); Map map=new HashMap(); map.put("title", "java"); List<Blog> blogs = mapper.queryBlogIf(map); for (Blog blog : blogs) { System.out.println(blog); } }
choose、when、otherwise
<select id="queryBlogChoose" parameterType="map" resultMap="myBlog"> select * from blog <where> <choose> <when test="title != null"> title = #{title} </when> <when test="author != null"> and author= #{author} </when> </choose> </where> </select>
trim、where、set
List<Blog> getBlogs(); List<Blog> queryBlogIf(Map map); List<Blog> queryBlogChoose(Map map); void updateBlog(Map map);
<select 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} </select>
@Test public void test05() { SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); Map map = new HashMap(); map.put("id","1"); map.put("author", "baba"); mapper.updateBlog(map); }
SQL片段
- sql,include
<sql id="id-title-author"> <set> <if test="title!=null"> title = #{title}, </if> <if test="author!=null"> author = #{author}, </if> </set> </sql> <select id="updateBlog" parameterType="map"> update blog <include refid="id-title-author"></include> where id = #{id} </select>
foreach
- xml
<select id="queryBlogForeach" parameterType="map" resultMap="myBlog"> select * from blog <where> <foreach collection="ids" item="id" open="and (" close=")" separator="or"> id = #{id} </foreach> </where> </select>
- test
@Test public void test0() { SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); Map map = new HashMap(); ArrayList<Integer> ids = new ArrayList<>(); ids.add(1); ids.add(2); map.put("ids", ids); List<Blog> blogs = mapper.queryBlogForeach(map); for (Blog blog : blogs) { System.out.println(blog); } }
这篇关于动态SQL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-29RocketMQ底层原理资料详解:新手入门教程
- 2024-11-29RocketMQ源码资料解析与入门教程
- 2024-11-29[开源]6.1K star!这款电视直播源神器真的太赞啦!
- 2024-11-29HTTP压缩入门教程:轻松提升网页加载速度
- 2024-11-29JWT开发入门指南
- 2024-11-28知识管理革命:文档软件的新玩法了解一下!
- 2024-11-28低代码应用课程:新手入门全攻略
- 2024-11-28哪些办公软件适合团队协作,且能够清晰记录每个阶段的工作进展?
- 2024-11-28全栈低代码开发课程:零基础入门到初级实战
- 2024-11-28拖动排序课程:轻松掌握课程拖动排序功能