MyBatis批量新增、修改、删除、查询

1.批量新增接口

<span style="font-size:18px;">public interface StoredRecordVarianceDataMapper {

	  /**
     * 插入差异数据
     * @param rdList 差异
     * @return 差异数据
     */
    int  insertCheckData(List<RecordVarianceData> rdList);
}</span><span style="font-size:14px;">
</span>

2. 批量 新增xml

<strong><span style="font-size:18px;"><?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="cn.my.da.mapper.data.StoredRecordVarianceDataMapper" >


    <resultMap id="BaseList" type="cn.my.model.data.RecordVarianceData" >
     <result column="CID" property="id" jdbcType="INTEGER" />
    <result column="GROUP_CENTRE_RUNNING_NUM" property="gropCenterRuningNum" jdbcType="DECIMAL" />
    <result column="PRO_CENTRE_RUNNING_NUM" property="proCenterRuningNum" jdbcType="DECIMAL" />
    <result column="ROWINFO" property="rowInfo" jdbcType="VARCHAR" />
    <result column="CHECKTIME" property="checkTime" jdbcType="VARCHAR" />
    </resultMap>
  
  <insert id="insertCheckData" parameterType="cn.my.model.data.RecordVarianceData">
  insert into OSD_CHECK_DATA_INFO(GROUP_CENTRE_RUNNING_NUM,PRO_CENTRE_RUNNING_NUM,ROWINFO,CHECKTIME)
  		<foreach item="item" index="index" collection="list" separator=",">
			( #{item.gropCenterRuningNum,jdbcType=DECIMAL},#{item.proCenterRuningNum,jdbcType=DECIMAL},#{item.rowInfo,jdbcType=VARCHAR},#{item.checkTime,jdbcType=VARCHAR})
		</foreach>
  </insert>
</mapper></span></strong>
3.批量修改接口及xml

<strong><span style="font-size:10px;">实例1:
public void batchUpdateStudent(List<Integer> ls){
	SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
	session.insert("mybatisdemo.domain.Student.batchUpdateStudent",ls);
	session.commit();
	session.close();
}
<update id="batchUpdateStudent" parameterType="java.util.List">
	UPDATE STUDENT SET name = "5566" WHERE id IN
	<foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
		#{item}
	</foreach>
</update>

实例2:
public void batchUpdateStudentWithMap(Map<String,Object> map){
	SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
	session.insert("mybatisdemo.domain.Student.batchUpdateStudentWithMap",map);
	session.commit();
	session.close();
}
<update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
	UPDATE STUDENT SET name = #{name} WHERE id IN 
	<foreach collection="idList" index="index" item="item" open="(" separator="," close=")"> 
		#{item} 
	</foreach>
</update></span></strong>



4.批量删除接口及xml

<strong><span style="font-size:18px;">public void batchDelete(List<Integer> ls){
	SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
	session.delete("mybatisdemo.domain.Student.batchDelete",ls);
	session.commit();
	session.close();
}
<delete id="batchDelete" parameterType="java.util.List">
	DELETE FROM OSD_CHECK_DATA_INFO WHERE CID IN
	<foreach collection="list" index="index" item="item" open="(" separator="," close=")"> 
		#{item} 
	</foreach>
</delete></span></strong>


5.批量查询接口及xml

 <strong><span style="font-size:18px;">/**
     * 批量查询
     * @param rdList 差异
     * @return 差异数据
     */
    List<Osdcheckdatainfo> batchQueryCheckData(Map<String,Object> map);
<select id="selectBySomePoiIds" resultType="Osdcheckdatainfo" parameterType="java.util.Map">  
    SELECT 
<include refid="Base_Column_List" />
  FROM 表名   
   WHERE poi_id in       
 <foreach collection="poiIds" item="poiId" index="index" open="(" close=")" separator=",">    
        #{poiId}     
   </foreach>    
      AND pass_uid = #{passUid}    
      <if test="status != null">    
        AND status = #{status,jdbcType=BIGINT}      
     </if>  
</select></span></strong>



你可能感兴趣的:(mybatis,批量增删改查)