MyBatis_2_动态SQL_参数传递_注解_事务处理

MyBatis框架

动态SQL

if判断
<select id="selectByCondition" resultMap="userMap">
	SELECT * FROM users WHEN
    <!--test属性的表达式为真时,if标签中的文本内容就会拼接到sql语句中-->
	<if test="id!=null">
		 id=#{id}
	</if>
</select>
choose/when/otherwies选择
 <!--单条件动态查询-->
<select id="selectByCondition" resultMap="userMap">
	select * from users where
    <choose> <!--相当于java中的switch-->
    	<when test="id!=null">id=#{id}</when><!--case-->
        <when test="userName!=null and userName != ''">
        	userName = #{userName}
        </when>
        <otherwise>1=1</otherwise>
        <!--恒等式解决语法错误,或者使用where标签-->
    </choose>
</select>
trim/where/set条件
 <!--多条件动态查询-->
<select id="selectByCondition" resultMap="userMap">
	SELECT * FROM users
   	<where>
    <!--智能生成WHERE语句,并解决了where and的sql语句错误,替换恒等式解决方案-->
    	<if test="id!=null">
	        AND id=#{id}
	    </if>
    	<!--test属性中判断条件是否满足-->
		<if test="userName!=null and userName != ''">
        	AND userName = #{userName}
    	</if>
    </where>
</select>

<!--单条件动态查询-->
<update id="updateOne" resultMap="userMap">
	UPDATE users
    <set>
    <!--set标签生成SET语句,并智能去除语句中多余逗号-->
    	<if test="userName!=null and userName!=''">
        	user_name = #{userName},
        </if>
        <if test="userSex!=null and userSex!=''">
        	user_sex = #{userSex},
        </if>
        <if test="userAge!=null and userAge!=''">
        	user_age = #{userAge},
        </if>
    </set>
    where id = #{id};
</update>

<trim prefix="" suffix="" suffixOverrides="" prefixOverrides="">
<!--
prefix:在trim标签内sql语句加上前缀。
suffix:在trim标签内sql语句加上后缀。
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
prefixOverrides:指定去除多余的前缀内容
-->
</trim>
foreach循环
<select id="selectByCondition" resultMap="userMap">
  SELECT * FROM user
  <where>
    <foreach item="id" index="index" collection="list"
        open="id IN (" separator="," close=")" nullable="true">
          #{id}
    <!--item项,index索引,collection传入参数(数组或集合),open开头字符串-->
    <!--close结束字符串,separator循环间隔字符串,-->    
    </foreach>
  </where>
</select>

MyBatis参数传递

MyBatis接口方法中可接受各种参数,框架底层对参数进行不同封装处理

单个参数:

  1. POJO、javaBean类型,属性名和参数占位符一致

  2. Map集合,键名和参数占位符一致

  3. Collection

封装为Map,封装两次分别为arg0、collectin

  1. List

封装为List,封装三次次分别为arg0、collectin、list

  1. Array

封装为Map,封装两次次次分别为arg0、array

  1. 其他类型,直接使用

多参数:

  1. 封装为Map集合

  2. 集合中单个参数将会封装两次,命名为arg和param

即:map.put(“arg0”,参数1);map.put(“param0”,参数1);

  1. 可以使用@Param("xxx")注解中的字符串替换默认的arg键名

即:map.put(“xxx”,参数1);map.put(“param0”,参数1);

MyBatis注解开发

注解开发会比配置文件开发更方便,注解完成简单功能,配置文件完成复杂功能

查询:@Select

添加:@Insert

修改:@Update

删除:@Delete

集合映射:@ResultMap
类型映射:@ResultType

MyBatis事务处理

//openSession方法无参时等同于默认为false,手动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession();
//~~~~~~~~~~~~~~操作数据库的DML语句~~~~~~~~~~~~~~~~
sqlSession.rollback();//回滚事务
//~~~~~~~~~~~~~~操作数据库的DML语句~~~~~~~~~~~~~~~~
sqlSession.commit();//提交事务

//openSession方法参数为true时自动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值