MyBatis(四)

1.映射文件中区别#{}和${}的用法

在Mybatis的mapper文件【sql映射文件】中,参数传递有2种方式,一种是#{},一种是${}

#{} 实现的是sql语句的预处理,之后执行的sql中用?代替,类似于jdbc中的preparedStatement()方法。使用的时候,不需要关注参参数的类型,mybatis会自动类型转换,并且防止sql注入。

¥{}实现的sql语句的拼接操作,不做数据类型的转换,需要自行判断数据类型,不防止sql注入,类似于的jdbc中statement()。

总结#{}占位符,${}用于sql拼接。

#{}:例子

<insert id="insertStu" parameterType="com.student.bean.StudentBean">
        insert into t_student values (null,#{stu_name},#{stu_pass},#{stu_age},#{stu_address});
</insert>

${}演示

<insert id="insertStu" parameterType="com.student.bean.StudentBean">
        insert into t_student values (null,'${stu_name}','${stu_pass}',${stu_age},'${stu_address}');
</insert>

2.动态SQL

如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。

利用动态 SQL 这一特性可以彻底摆脱这种痛苦。

常用的动态SQL

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

1.foreach元素:

格式:

<foreach collection="list【表示需要被遍历的数据结合】" 
item="从collection对应的集合中得到的每一个数据对象【java对象】" 
separator="数据对象【java对象】的分隔符">
	    	每一个具体的数据对象
</foreach>

foreach元素,实现批量添加

<insert id="insertPersonForeach" parameterType="java.util.List">
    insert into t_person values
    <foreach collection="list" item="person" separator=",">
        (null,#{person.pername},#{person.perage},#{person.peraddress})
    </foreach>
</insert>

 foreach元素,实现批量删除

<delete id="deleteList" parameterType="java.util.List">
	delete from t_person where per_id in
		<!-- collection:循环的集合/数组 -->
		<!-- item:来自集合/数组中的每一个元素对象 -->
		<!-- separator:分割符 -->
		<!-- open:开始符号 -->
		<!-- close:结束符号 -->
	<foreach collection="array" item="id" open="(" separator="," close=")">
		   #{id}
	</foreach>
</delete>

 2.if元素

<select id="selectPersonByIf" parameterType="hashMap" resultMap="personMap">
    select * from t_person where 1=1
    <if test="name !=null and name !=''">
        and  per_name like #{name}
    </if>
</select>

如果有就根据用户名模糊查询

 

如果没有就查询所有

3.choose (when, otherwise)

 【每次只匹配一个条件】

需求:查询用户信息,如果输入了用户名,根据用户名进行模糊查找,返回

                如果输入了年龄,根据年龄进行匹配查找,返回

                如果输入了地址,根据地址进行模糊查找,返回

如果查询条件都为空,那么就查询所有。

有点类似于Switch语句

例子:

<resultMap id="personMap" type="com.wangxing.mybatis.bean.Person">
    <id column="per_id" property="perid"></id>
    <result column="per_name" property="pername"></result>
    <result column="per_age" property="perage"></result>
    <result column="per_address" property="peraddress"></result>
</resultMap>
<select id="selectPersonByChoose" parameterType="hashMap" resultMap="personMap">
    select * from t_person where 1=1
    <choose>
        <when test="name !=null and name !=''">and  per_name like #{name}</when>
        <when test="age !=0 and age !=null">and  per_age=#{age}</when>
        <when test="address !=null and address !=''">and  per_address like #{address}</when>
        <otherwise></otherwise>
    </choose>
</select>

4.where元素 == sql中where

例子:

<select id="strictSelectStudent"  parameterType="java.util.HashMap" resultMap="stumap">
   select * from t_student
   <where>

            <if test="name!=null and name!=''">
                and stu_name like '${name}%'
            </if>

            <if test="age != null and age != 0">
                and stu_age = #{age}
            </if>
            <if test="address != null and address != ''">
                and stu_address = #{address}
            </if>
    </where>
</select>

5. set元素==sql中set

<update id="updatePerson" parameterType="hashMap">
    update  t_person
    <set>
        <if test="name !=null and name !=''">
            per_name = #{name},
        </if>
        <if test="age !=null and age !=0">
            per_age = #{age},
        </if>
        <if test="address !=null and address !=''">
            per_address = #{address},
        </if>
    </set>
    <where>per_id = #{id}</where>
</update>

 

                                                                                                                        无奈源于不够强大

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值