1. 不自动生成主键
-
public interface ELogDetailMapper {
-
-
int insertBatch(List<ELogDetail> list);
-
-
}
-
<insert id="insertBatch" parameterType="cas.iie.po.ELogDetail">
-
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Integer">
-
SELECT LAST_INSERT_ID()
-
</selectKey>
-
insert into e_log_detail (id, time, fro_ip,
-
to_ip,
-
fro_port, to_port,
-
protocol_code,
-
up_package_num, down_package_num,
-
total_package_num,
-
up_flow, down_flow, total_flow,
-
c_rst, c_reason,
-
c_plugin,
-
ip_type,
-
transport_protocol, application_protocol,
-
app_code,
-
sub_app_code,
-
action_code,
-
class_type_code, state_code,
-
last_update_time
-
)
-
values
-
<foreach collection="list" item="item" index="index"
-
separator=", ">
-
(#{item.id,jdbcType=INTEGER},
-
#{item.time,jdbcType=TIMESTAMP},
-
#{item.froIp,jdbcType=CHAR},
-
#{item.toIp,jdbcType=CHAR},
-
#{item.froPort,jdbcType=CHAR},
-
#{item.toPort,jdbcType=CHAR},
-
#{item.protocolCode,jdbcType=CHAR},
-
#{item.upPackageNum,jdbcType=INTEGER},
-
#{item.downPackageNum,jdbcType=INTEGER},
-
#{item.totalPackageNum,jdbcType=INTEGER},
-
#{item.upFlow,jdbcType=INTEGER},
-
#{item.downFlow,jdbcType=INTEGER},
-
#{item.totalFlow,jdbcType=INTEGER},
-
#{item.cRst,jdbcType=INTEGER},
-
#{item.cReason,jdbcType=INTEGER},
-
#{item.cPlugin,jdbcType=INTEGER},
-
#{item.ipType,jdbcType=CHAR},
-
#{item.transportProtocol,jdbcType=CHAR},
-
#{item.applicationProtocol,jdbcType=CHAR},
-
#{item.appCode,jdbcType=CHAR},
-
#{item.subAppCode,jdbcType=CHAR},
-
#{item.actionCode,jdbcType=CHAR},
-
#{item.classTypeCode,jdbcType=CHAR},
-
#{item.stateCode,jdbcType=CHAR},
-
#{item.lastUpdateTime,jdbcType=TIMESTAMP}
-
)
-
</foreach>
-
</insert>
2. 自动生成主键, 速度太慢 和一条一条插入速度是一样的
-
public interface InsertBatch {
-
-
int insert(Object record);
-
}
-
public interface ELogDetailMapper extends InsertBatch{
-
-
-
int insert(ELogDetail record);
-
-
-
}
-
<insert id="insert" useGeneratedKeys="true" keyProperty="id"
-
parameterType="cas.iie.po.ELogDetail">
-
insert into e_log_detail (id, time, fro_ip,
-
to_ip,
-
fro_port, to_port, protocol_code,
-
up_package_num, down_package_num,
-
total_package_num,
-
up_flow, down_flow, total_flow,
-
c_rst, c_reason,
-
c_plugin,
-
ip_type, transport_protocol, application_protocol,
-
app_code,
-
sub_app_code, action_code,
-
class_type_code, state_code,
-
last_update_time
-
)
-
values (#{id,jdbcType=INTEGER},
-
#{time,jdbcType=TIMESTAMP},
-
#{froIp,jdbcType=CHAR},
-
#{toIp,jdbcType=CHAR}, #{froPort,jdbcType=CHAR},
-
#{toPort,jdbcType=CHAR},
-
#{protocolCode,jdbcType=CHAR},
-
#{upPackageNum,jdbcType=INTEGER}, #{downPackageNum,jdbcType=INTEGER},
-
#{totalPackageNum,jdbcType=INTEGER},
-
#{upFlow,jdbcType=INTEGER},
-
#{downFlow,jdbcType=INTEGER}, #{totalFlow,jdbcType=INTEGER},
-
#{cRst,jdbcType=INTEGER}, #{cReason,jdbcType=INTEGER},
-
#{cPlugin,jdbcType=INTEGER},
-
#{ipType,jdbcType=CHAR},
-
#{transportProtocol,jdbcType=CHAR},
-
#{applicationProtocol,jdbcType=CHAR},
-
#{appCode,jdbcType=CHAR},
-
#{subAppCode,jdbcType=CHAR}, #{actionCode,jdbcType=CHAR},
-
#{classTypeCode,jdbcType=CHAR}, #{stateCode,jdbcType=CHAR},
-
#{lastUpdateTime,jdbcType=TIMESTAMP}
-
)
-
</insert>
-
public class MyBatisUtil {
-
-
private final static SqlSessionFactory sqlSessionFactory;
-
-
static {
-
sqlSessionFactory = (SqlSessionFactory) ApplicationContextHelper.getBean("sessionFactory");
-
}
-
-
public final static SqlSessionFactory getSqlSessionFactory() {
-
return sqlSessionFactory;
-
}
-
-
-
public final static<E> int insertBatch(Class<?> mapper,List<E> list){
-
SqlSession session=sqlSessionFactory.openSession(ExecutorType.BATCH,true);
-
InsertBatch map = (InsertBatch) session.getMapper(mapper);
-
-
for (E e : list) {
-
map.insert(e);
-
}
-
session.commit();
-
session.close();
-
-
return 1;
-
}
-
-
-
}
阅读(4548) | 评论(0) | 转发(0) |