mybatis

1,翻页与集合

2,批量

foreach元素的属性主要有 item,index,collection,open,separator,close。

item表示集合中每一个元素进行迭代时的别名.

index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置.

open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符.

close表示以什么结束

<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where 
  <foreach collection="listTag" index="index" item="tag" open="("
    separator="," close=")">
   #{tag} in n.tags
  </foreach>

==select * from t_news n where ( ? in n.tags , ? in n.tags )


批量插入

mysql 

<insert id="batchInsertStudent" parameterType="java.util.List">
    INSERT INTO STUDENT (id,name,sex,tel,address)
    VALUES 
    <foreach collection="list" item="item" index="index" separator="," >
        (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})
    </foreach>
</insert>

oracle是这样的

<insert id="add" parameterType="EStudent">
  <selectKey keyProperty="id" resultType="_long" order="BEFORE">
    select CAST(RANDOM * 100000 as INTEGER) a FROM SYSTEM.SYSDUMMY1
  </selectKey>
  insert into TStudent(id, name, age) values(#{id}, #{name}, #{age}) //注意这个id是上面selectkey出来的, BEFORE表示是在insert语句前还是后执行selectKey操作,type表示类型 id表示这个数据会放到EStudent这个类的id属性下.
</insert>

或者下面这样的

<select id="batchSave" parameterType="java.util.List">
                INSERT INTO TABLE_NAME(ID,NAME) 
                <foreach collection="list"  item="itm" separator="union all">
                        (SELECT #{itm.id},#{itm.name} FROM DUAL) //就是这里不一样.
                </foreach>
        </select>
如果上面mysql的insert标签报错.用下面的肯定不会报错
<select id="batchSave" parameterType="java.util.List">
                INSERT INTO TABLE_NAME(ID,NAME) VALUES
                <foreach collection="list"  item="itm" separator=",">
                        (#{itm.id},#{itm.name})
                </foreach>
        </select>
上面标签这么多,如果在最外层写了![CDATA[<foreach> ]],那foreach不会执行.xml解析器直接把它当做字符串了.

过程;

SessionFactoryUtil.getSqlSessionFactory().openSession()
-->session.insert --> session.commit --> close session

1,上面的如果一条记录报错,会回滚么?

2,返回值设多少?

where 和 if 标签

抄的 where 标签,防止出现 select * from 什么条件都不符合也有一个 where. 顺便有一个if 标签的使用

  1. <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">     
  2.     SELECT * from STUDENT_TBL ST      
  3.     <where>     
  4.         <if test="studentName!=null and studentName!='' ">     
  5.             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
  6.         </if>     
  7.         <if test="studentSex!= null and studentSex!= '' ">     
  8.             AND ST.STUDENT_SEX = #{studentSex}      
  9.         </if>     
  10.         <if test="studentBirthday!=null">     
  11.             AND ST.STUDENT_BIRTHDAY = #{studentBirthday}      
  12.         </if>     
  13.         <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">     
  14.             AND ST.CLASS_ID = #{classEntity.classID}      
  15.         </if>     
  16.     </where>     
  17. </select>     

set 标签类似 where 用在 update xxx set 中

  1. update id="updateStudent" parameterType="StudentEntity">     
  2.     UPDATE STUDENT_TBL      
  3.     <set>     
  4.         <if test="studentName!=null and studentName!='' ">     
  5.             STUDENT_TBL.STUDENT_NAME = #{studentName},      
  6.         </if>     
  7.         <if test="studentSex!=null and studentSex!='' ">     
  8.             STUDENT_TBL.STUDENT_SEX = #{studentSex},      
  9.         </if>     
  10.         <if test="studentBirthday!=null ">     
  11.             STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
  12.         </if>     
  13.         <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
  14.             STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
  15.         </if>     
  16.     </set>     
  17.     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
  18. </update>     

还有 choose 标签 相当于 拼接语句的时候用 switch case

  1. update id="updateStudent" parameterType="StudentEntity">     
  2.     UPDATE STUDENT_TBL      
  3.     <set>     
  4.         <if test="studentName!=null and studentName!='' ">     
  5.             STUDENT_TBL.STUDENT_NAME = #{studentName},      
  6.         </if>     
  7.         <if test="studentSex!=null and studentSex!='' ">     
  8.             STUDENT_TBL.STUDENT_SEX = #{studentSex},      
  9.         </if>     
  10.         <if test="studentBirthday!=null ">     
  11.             STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
  12.         </if>     
  13.         <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
  14.             STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
  15.         </if>     
  16.     </set>     
  17.     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
  18. </update>     

上面代码都是有问题的, XML中的字符串最好都写![CDATA[里面]];不然><#%都可能报错


3,优化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值