mybatis

xiaoxiao2021-02-28  146

MyBatis动态SQL之增、删、改、查操作


MyBatis动态SQL之增删改查操作 概述MyBatis动态SQL支持 动态SQL之查询动态SQL之插入动态SQL之删除动态SQL之更新


概述


在实际的场景中,经常会遇到动态SQL的增、删、改、查问题,这里就必要说明何谓动态SQL,我们举一个实际的例子,比如,在一个web工程中,经常会有一个搜索框,并且在搜索之前通常会进行一个关键词的过滤,比如可以过滤的条件有:姓名、年龄等,这样的话当我们姓名和年龄都不选,则等价于下面的SQL语句

select * from students; -- 不去限制姓名和年龄 1 1

当我们将年龄选择为>20时,相当于下面的SQL语句:

select * from students where age>20; 1 1

当我们同时选择条件姓名为:张三,年龄>20,则相当于下面的SQL语句:

select * from students where age>20 and name='张三'; 1 1

当我们有很多的条件时,此时就需要我们去组合这些条件,并动态的生成一个可执行的SQL语句,这样就不是一个简单的SQL语句能够解决问题,那么我们该怎么办呢?在MyBatis中同样是支持这种动态SQL的写法,具体见下面的内容。

MyBatis动态SQL支持


动态SQL之查询

<?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="studentNameSpace"> <!-- resultMap标签将了工程entity实体类中的对象与数据库中的表对应起来 resultMap中的id属性是一个唯一的名字 子标签中的id属性用来指定主键 result标签用来指定其他的键,其中property属性是指实体中的字段,对应的 column属性表示的数据库中的响应的字段 --> <resultMap type="com.jpzhutech.entity.Student" id="studentMap"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sal" column="sal"/> </resultMap> <!-- 动态查询SQL语句 --> <select id="findAll" parameterType="map" resultMap="studentMap"> select id , name , sal from students <where> <if test="pid!=null" > and id = #{pid} <!-- #{}和之前使用c3p0的时候写的?含义是相同的 --> </if> <if test="pname!=null" > and name = #{pname} </if> <if test="psal!=null" > and sal = #{psal} </if> </where> </select> </mapper> 12345678910111213141516171819202122232425262728293031323334353637 12345678910111213141516171819202122232425262728293031323334353637
动态SQL之插入

<!-- 动态insert --> <!-- 定义两个sql片段,第一个对应字段名,id属性值任意并且唯一 --> <sql id="key"> <trim suffixOverrides=","> <if test="id!=null"> id, </if> <if test="name!=null"> name, </if> <if test="sal!=null"> sal, </if> </trim> </sql> <!-- 定义第二个sql片段,第二个对应?,key属性值任意并且唯一 --> <sql id="value"> <trim suffixOverrides=","> <if test="id!=null"> #{id}, </if> <if test="name!=null"> #{name}, </if> <if test="sal!=null"> #{sal}, </if> </trim> </sql> <!-- <include refid="key"/>和<include refid="value"/>表示引用上面sql片段 --> <insert id="insertStudent" parameterType="com.jpzhutech.entity.Student"> insert into students(<include refid="key"/>) values(<include refid="value"/>); </insert> 12345678910111213141516171819202122232425262728293031323334353637383940 12345678910111213141516171819202122232425262728293031323334353637383940
动态SQL之删除

<!-- 动态删除操作 delete from students where id in(?,?,?);--> <delete id="deleteStudent"> delete from students where id in <!-- foreach用于迭代数组元素 open表示开始符号 close表示结束符号 seprator表示元素间的分割符 items表示迭代的数组 --> <foreach collection="array" open="(" close=")" separator="," item="ids"> #{ids} </foreach> </delete> 12345678910111213 12345678910111213
动态SQL之更新

<!-- 动态更新SQL语句,update table_name set name=? , sal=? where id=?, 其中id不能更新,因为id为主键,这个动态更新该怎么写呢? set标签会自动判断后面是否加, --> <update id="updateStudent" parameterType="map" > update students <set> <if test="pname!=null"> name = #{pname}, </if> <span class="hljs-tag"><<span class="hljs-title">if</span> <span class="hljs-attribute">test</span>=<span class="hljs-value">"psal!=null"</span>></span> sal = #{psal}, <span class="hljs-tag"></<span class="hljs-title">if</span>></span> <span class="hljs-tag"></<span class="hljs-title">set</span>></span> where id = #{pid} <span class="hljs-tag"></<span class="hljs-title">update</span>></span></code><ul class="pre-numbering" style="opacity: 0.149728;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li></ul><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div><ul class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li></ul></pre></div> <script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul></ul>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li></li>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

转载请注明原文地址: https://www.6miu.com/read-31219.html

最新回复(0)