mybatis ----xml细节

1.批量插入

方案一:不推荐这个方式

1. mapper接口的add方法返回值将是最一条INSERT语句的操作成功的记录数目(就是0或1),而不是所有INSERT语句的操作成功的总记录数目 
2. 当其中一条不成功时,不会进行整体回滚。

<insert id="add" parameterType="EStudent">
  <foreach collection="list" item="item" index="index" separator=";">
    INSERT INTO TStudent(name,age) VALUES(#{item.name}, #{item.age})
  </foreach>
</insert>

方案二:推荐使用

<insert id="insertStudentAutoKey" parameterType="java.util.List">
    INSERT INTO STUDENT_TBL (STUDENT_NAME,  
                                 STUDENT_SEX,  
                                 STUDENT_BIRTHDAY,  
                                 CLASS_ID)  
                              VALUES   
  <foreach collection="list" item="item" index="index" separator=",">
      ( #{item.studentName},#{item.studentSex},#{item.studentBirthday},#{item.classEntity.classID})
  </foreach>
  </insert>

2.参数

1)基本参数

<!-- 查询学生list,like姓名、=性别,参数entity类型 -->  
<select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">  
    SELECT * from STUDENT_TBL ST  
        WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')  
          AND ST.STUDENT_SEX = #{studentSex}  
</select> 
StudentEntity entity = new StudentEntity();  
entity.setStudentName("");  
entity.setStudentSex("");  
List<StudentEntity> studentList = studentMapper.getStudentListWhereEntity(entity);  
for (StudentEntity entityTemp : studentList) {  
    System.out.println(entityTemp.toString());  
} 

2).Map参数

<!-- 查询学生list,=性别,参数map类型 -->  
<select id="getStudentListWhereMap" parameterType="Map" resultMap="studentResultMap">  
    SELECT * from STUDENT_TBL ST  
     WHERE ST.STUDENT_SEX = #{sex}  
          AND ST.STUDENT_SEX = #{sex}  
</select>  
Map<String, String> map = new HashMap<String, String>();  
map.put("sex", "");  
map.put("name", "");  
List<StudentEntity> studentList = studentMapper.getStudentListWhereMap(map);
for (StudentEntity entityTemp : studentList) {  
    System.out.println(entityTemp.toString());  
}  

3).多参数实现

如果想传入多个参数,则需要在接口的参数上添加@Param注解。给出一个实例: 
接口写法:

public List<StudentEntity> getStudentListWhereParam(@Param(value = "name") String name, @Param(value = "sex") String sex, @Param(value = "birthday") Date birthdar, @Param(value = "classEntity") ClassEntity classEntity);  
<!-- 查询学生list,like姓名、=性别、=生日、=班级,多参数方式 -->  
<select id="getStudentListWhereParam" resultMap="studentResultMap">  
    SELECT * from STUDENT_TBL ST  
    <where>  
        <if test="name!=null and name!='' ">  
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{name}),'%')  
        </if>  
        <if test="sex!= null and sex!= '' ">  
            AND ST.STUDENT_SEX = #{sex}  
        </if>  
        <if test="birthday!=null">  
            AND ST.STUDENT_BIRTHDAY = #{birthday}  
        </if>  
        <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">  
            AND ST.CLASS_ID = #{classEntity.classID}  
        </if>  
    </where>  
</select>

另一种多参数实现

public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

<select id="getXXXBeanList" resultType="XXBean">

  select t.* from tableName where id = #{0} and name = #{1}  

</select>  

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始

3.批量更新 情景一:更新多条记录为多个字段为不同的值 

<update id="updateBatch"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update course
        <set>
            name=${item.name}
        </set>
        where id = ${item.id}
    </foreach>      
</update>

比较普通的写法,是通过循环,依次执行update语句。

方法二:

UPDATE TStudent SET Name = R.name, Age = R.age
from (
  SELECT 'Mary' as name, 12 as age, 42 as id
  union all
  select 'John' as name , 16 as age, 43 as id
) as r 
where ID = R.id

情景二:更新多条记录的同一个字段为同一个值

<update id="updateOrders" parameterType="java.util.List">
     update orders set state = '0' where no in
     <foreach collection="list" item="id" open="(" separator="," close=")">
   #{id}
     </foreach>
 </update>

4.批量删除

delete语句属性配置细节同update

批量删除:

<delete id="batchRemoveUserByPks" parameterType="java.util.List">

  DELETE FROM LD_USER WHERE ID in 

  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">

    #{item}

  </foreach>

</delete>

5.sql元素

Sql元素用来定义一个可以复用的SQL 语句段,供其它语句调用。比如:

<!-- 复用sql语句  查询student表所有字段 -->  
<sql id="selectStudentAll">  
        SELECT ST.STUDENT_ID,  
                   ST.STUDENT_NAME,  
                   ST.STUDENT_SEX,  
                   ST.STUDENT_BIRTHDAY,  
                   ST.CLASS_ID  
              FROM STUDENT_TBL ST  
</sql>  

这样,在select的语句中就可以直接引用使用了,将上面select语句改成:

<!-- 查询学生,根据id -->  
<select id="getStudent" parameterType="String" resultMap="studentResultMap">  
    <include refid="selectStudentAll"/>  
            WHERE ST.STUDENT_ID = #{studentID}   
</select>  

参考:https://www.cnblogs.com/yufeng218/p/6622644.html

        https://www.cnblogs.com/aligege/p/9527310.html

posted @ 2019-10-28 13:12  开文博客园  阅读(141)  评论(0)    收藏  举报