• 简易基本MyBatis语句书写模板-续更中


    1.trim标签替换where标签(查询操作)

    <select id="queryBlogIf" parameterType="map" resultType="blog">
    	select * from blog
    		<trim prefix="WHERE" prefixOverrides="AND |OR ">
    		<if test="title!= null and title!= """>
    			title = #{title}
    		</if>
    		<if test="author != null and author != """>
    			and author = #{author}
    		</if>
    	</trim>
    </select>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.trim标签助力insert标签(插入操作)

     <insert id="add"parameterType="cloud.xlh.my_system.pojo.Database">
     		INSERT INTO t_database
    	 <trim prefix="(" suffix=")" suffixOverrides=",">
    	 	<if test="dbName != null and dbName != """>
    	 		db_name,
    	 	</if>
    	 	<if test="createTime != null">
    	 		create_time 
    	 	</if>
    	 </trim>
    	 <trim prefix="values(" suffix=")" suffixOverrides=",">
    	 	<if test="dbName != null and dbName != """>
    	 		#{dbName,jdbcType=VARCHAR},
    	 	</if>
    	 	<if test="createTime != null">
    	 		#{createTime,jdbcType=Date}
    	 	</if>
    	 </trim>
    </insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.trim标签助力set标签(更新操作)

     <update
     id="update" parameterType="cloud.xlh.my_system.pojo.Database">
     		UPDATE t_database
    	 <trim prefix="SET"  suffixOverrides=",">
    	 	<if test="dbName != null and dbName != """>
    	 		db_name = #{dbName,jdbcType=VARCHAR},
    	 	</if>
    	 	<if test="createTime != null">
    	 		create_time = #{createTime,jdbcType=Date}
    	 	</if>
    	 </trim>
    </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4.Choose语句

    有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose
    标签可以解决此类问题,类似于 Java 的 switch 语句
    注意:适用于入参中只能一个有值的场景

    <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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5.Foreach语句

    将数据库中前三个数据的id修改为1,2,3;
    需求:我们需要查询 blog 表中 id 分别为1,2,3的博客信息

    <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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    6.Bind元素

    bind 元素允许你在 OGNL 表达式以外创建一个变量,并将其绑定到当前的上下文。比如:

    <select id="selectBlogsLike" resultType="Blog">
      <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
      SELECT * FROM BLOG
      WHERE title LIKE #{pattern}
    </select>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    原理参考:
    NO.1
    NO.2

  • 相关阅读:
    【愚公系列】2022年08月 微信小程序-slider滑动选择器详解
    从刘老师的进化的力量到有感,疫情阶段如何弯道超车
    【Java刷题进阶】基础入门篇
    JavaScript实现在HTML中的粒子文字特效
    Linux查询服务器配置(CPU、内存RAM等)命令
    【Java】使用HttpClient进行简单的post请求
    别名获取方式记录
    2023ES SHOW深圳电子元器件及物料采购展览会!|深圳比创达电子EMC
    2022 ICPC Gran Premio de Mexico 1ra Fecha(一)
    Navicat连接mysql 8.0.35 2059错误解决办法
  • 原文地址:https://blog.csdn.net/m0_48333563/article/details/125467674