动态SQL
2021/10/30 2:10:40
本文主要是介绍动态SQL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一·简介
动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 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;
插入数据:
<insert id="addBlog" parameterType="blog"> insert into mybatis.blog(id, title, author, create_time, views) values (#{id},#{title},#{author},#{createTime},#{views}) </insert>
@Test public void addBlog(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); Blog blog = new Blog(); blog.setId(IDutils.getId()); blog.setTitle("Mybatis"); blog.setAuthor("狂神说"); blog.setCreateTime(new Date()); blog.setViews(9999); mapper.addBlog(blog); blog.setId(IDutils.getId()); blog.setTitle("Java"); mapper.addBlog(blog); blog.setId(IDutils.getId()); blog.setTitle("Spring"); mapper.addBlog(blog); blog.setId(IDutils.getId()); blog.setTitle("微服务"); mapper.addBlog(blog); sqlSession.close(); }
三·if标签
<select id="getBlog" parameterType="map" resultType="blog"> select * from mybatis.blog <where> <include refid="title-author"></include> </where> </select> <sql id="title-author"> <if test="title!=null"> title=#{title} </if> <if test="author!=null"> and author=#{author} </if> </sql>
where标签:会智能的添加where,并且去掉and和or
sql标签,就是将一段sql语句封装起来,后面用include标签引用,实现sql的复用。
四·choose,when,otherwise标签
<select id="getBlog2" parameterType="map" resultType="blog"> select * from mybatis.blog <where> <choose> <when test="title!=null"> title=#{title} </when> <when test="author!=null"> and author=#{author} </when> <otherwise> views=9999 </otherwise> </choose> </where> </select>
类似于java中的switch case,只选择一个,从上往下,如果有满足条件的就加上,只选择一个,如果都不满足就选择otherwise中的内容。
五·trim(set,where)
可通过trim自定义标签,实现不同的功能。prefixOverrided前缀覆盖,suffixOverrides后缀覆盖
where的原型:
<trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>
set的原型:
<trim prefix="SET" suffixOverrides=","> ... </trim>
set的使用:
<update id="updateBlogById" parameterType="map"> update mybatis.blog <set> <if test="title!=null"> title=#{title}, </if> <if test="author!=null"> author=#{author} </if> </set> where id=#{id} </update>
六·foreach
<!--查询1,2,3号博客用where(or)拼接--> <select id="getBlogs" parameterType="map" resultType="blog"> select * from mybatis.blog <where> <foreach collection="ids" item="id" open="(" separator="or" close=")"> id=#{id} </foreach> </where> </select>
传递来的map里的ids如果为空,则不会加上where标签,用id去遍历ids,开始"(",用","隔开,
结尾为")"。
<!--用where in拼接--> <select id="getBlogs2" parameterType="map" resultType="blog"> select * from mybatis.blog <where> <foreach collection="ids" item="id" open="id in(" separator="," close=")"> #{id} </foreach> </where> </select>
这篇关于动态SQL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南