Chinaunix首页 | 论坛 | 博客
  • 博客访问: 166059
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2015-04-13 11:22:11

1. 不自动生成主键


点击(此处)折叠或打开

  1. public interface ELogDetailMapper {
  2.     
  3.     int insertBatch(List<ELogDetail> list);

  4. }

点击(此处)折叠或打开

  1. <insert id="insertBatch" parameterType="cas.iie.po.ELogDetail">
  2.         <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Integer">
  3.             SELECT LAST_INSERT_ID()
  4.         </selectKey>
  5.         insert into e_log_detail (id, time, fro_ip,
  6.         to_ip,
  7.         fro_port, to_port,
  8.         protocol_code,
  9.         up_package_num, down_package_num,
  10.         total_package_num,
  11.         up_flow, down_flow, total_flow,
  12.         c_rst, c_reason,
  13.         c_plugin,
  14.         ip_type,
  15.         transport_protocol, application_protocol,
  16.         app_code,
  17.         sub_app_code,
  18.         action_code,
  19.         class_type_code, state_code,
  20.         last_update_time
  21.         )
  22.         values
  23.         <foreach collection="list" item="item" index="index"
  24.             separator=", ">
  25.             (#{item.id,jdbcType=INTEGER},
  26.             #{item.time,jdbcType=TIMESTAMP},
  27.             #{item.froIp,jdbcType=CHAR},
  28.             #{item.toIp,jdbcType=CHAR},
  29.             #{item.froPort,jdbcType=CHAR},
  30.             #{item.toPort,jdbcType=CHAR},
  31.             #{item.protocolCode,jdbcType=CHAR},
  32.             #{item.upPackageNum,jdbcType=INTEGER},
  33.             #{item.downPackageNum,jdbcType=INTEGER},
  34.             #{item.totalPackageNum,jdbcType=INTEGER},
  35.             #{item.upFlow,jdbcType=INTEGER},
  36.             #{item.downFlow,jdbcType=INTEGER},
  37.             #{item.totalFlow,jdbcType=INTEGER},
  38.             #{item.cRst,jdbcType=INTEGER},
  39.             #{item.cReason,jdbcType=INTEGER},
  40.             #{item.cPlugin,jdbcType=INTEGER},
  41.             #{item.ipType,jdbcType=CHAR},
  42.             #{item.transportProtocol,jdbcType=CHAR},
  43.             #{item.applicationProtocol,jdbcType=CHAR},
  44.             #{item.appCode,jdbcType=CHAR},
  45.             #{item.subAppCode,jdbcType=CHAR},
  46.             #{item.actionCode,jdbcType=CHAR},
  47.             #{item.classTypeCode,jdbcType=CHAR},
  48.             #{item.stateCode,jdbcType=CHAR},
  49.             #{item.lastUpdateTime,jdbcType=TIMESTAMP}
  50.             )
  51.         </foreach>
  52.     </insert>


2. 自动生成主键,  速度太慢 和一条一条插入速度是一样的

点击(此处)折叠或打开

  1. public interface InsertBatch {

  2.     int insert(Object record);
  3. }

点击(此处)折叠或打开

  1. public interface ELogDetailMapper extends InsertBatch{


  2.     int insert(ELogDetail record);
  3.     

  4. }


点击(此处)折叠或打开

  1. <insert id="insert" useGeneratedKeys="true" keyProperty="id"
  2.         parameterType="cas.iie.po.ELogDetail">
  3.         insert into e_log_detail (id, time, fro_ip,
  4.         to_ip,
  5.         fro_port, to_port, protocol_code,
  6.         up_package_num, down_package_num,
  7.         total_package_num,
  8.         up_flow, down_flow, total_flow,
  9.         c_rst, c_reason,
  10.         c_plugin,
  11.         ip_type, transport_protocol, application_protocol,
  12.         app_code,
  13.         sub_app_code, action_code,
  14.         class_type_code, state_code,
  15.         last_update_time
  16.         )
  17.         values (#{id,jdbcType=INTEGER},
  18.         #{time,jdbcType=TIMESTAMP},
  19.         #{froIp,jdbcType=CHAR},
  20.         #{toIp,jdbcType=CHAR}, #{froPort,jdbcType=CHAR},
  21.         #{toPort,jdbcType=CHAR},
  22.         #{protocolCode,jdbcType=CHAR},
  23.         #{upPackageNum,jdbcType=INTEGER}, #{downPackageNum,jdbcType=INTEGER},
  24.         #{totalPackageNum,jdbcType=INTEGER},
  25.         #{upFlow,jdbcType=INTEGER},
  26.         #{downFlow,jdbcType=INTEGER}, #{totalFlow,jdbcType=INTEGER},
  27.         #{cRst,jdbcType=INTEGER}, #{cReason,jdbcType=INTEGER},
  28.         #{cPlugin,jdbcType=INTEGER},
  29.         #{ipType,jdbcType=CHAR},
  30.         #{transportProtocol,jdbcType=CHAR},
  31.         #{applicationProtocol,jdbcType=CHAR},
  32.         #{appCode,jdbcType=CHAR},
  33.         #{subAppCode,jdbcType=CHAR}, #{actionCode,jdbcType=CHAR},
  34.         #{classTypeCode,jdbcType=CHAR}, #{stateCode,jdbcType=CHAR},
  35.         #{lastUpdateTime,jdbcType=TIMESTAMP}
  36.         )
  37.     </insert>


点击(此处)折叠或打开

  1. public class MyBatisUtil {

  2.     private final static SqlSessionFactory sqlSessionFactory;

  3.     static {
  4.         sqlSessionFactory = (SqlSessionFactory) ApplicationContextHelper.getBean("sessionFactory");
  5.     }

  6.     public final static SqlSessionFactory getSqlSessionFactory() {
  7.         return sqlSessionFactory;
  8.     }
  9.     
  10.     
  11.     public final static<E> int insertBatch(Class<?> mapper,List<E> list){
  12.         SqlSession session=sqlSessionFactory.openSession(ExecutorType.BATCH,true);
  13.         InsertBatch map = (InsertBatch) session.getMapper(mapper);
  14.         
  15.         for (E e : list) {
  16.             map.insert(e);
  17.         }
  18.         session.commit();
  19.         session.close();
  20.         
  21.         return 1;
  22.     }
  23.     
  24.     
  25. }




阅读(4583) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~