我最近开始学习使用 MyBatis,但我遇到了一种情况。我无法使用 MyBatis 在 sql 中插入 Reason 对象列表。我有下面的 ReasonMapper.xml 文件,但无法理解如何编写插入查询以将原因对象作为批处理插入。
<?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="aero.sita.gsl.ee.services.referencedata.generated.mapper.ReasonMapper">
<resultMap id="BaseResultMap" type="aero.sita.gsl.ee.services.referencedata.generated.model.Reason">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
<result column="REASON_ID" jdbcType="CHAR" property="reasonId" />
<result column="IS_NOTE_REQUIRED" jdbcType="BIT" property="isNoteRequired" />
<result column="IS_SYSTEM_RESERVED" jdbcType="BIT" property="isSystemReserved" />
<result column="EFFECTIVE_FROM" jdbcType="NVARCHAR" property="effectiveFrom" />
<result column="EFFECTIVE_TO" jdbcType="NVARCHAR" property="effectiveTo" />
<result column="CREATED_ON" jdbcType="TIMESTAMP" property="createdOn" />
<result column="CREATED_BY" jdbcType="NVARCHAR" property="createdBy" />
<result column="LAST_UPDATED_ON" jdbcType="TIMESTAMP" property="lastUpdatedOn" />
<result column="LAST_UPDATED_BY" jdbcType="NVARCHAR" property="lastUpdatedBy" />
<result column="REASON_TYPE" jdbcType="NVARCHAR" property="reasonType" />
<result column="REASON_NAME" jdbcType="VARCHAR" property="reasonName" />
</resultMap>
<sql id="Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
REASON_ID, IS_NOTE_REQUIRED, IS_SYSTEM_RESERVED, EFFECTIVE_FROM, EFFECTIVE_TO, CREATED_ON,
CREATED_BY, LAST_UPDATED_ON, LAST_UPDATED_BY, REASON_TYPE, REASON_NAME
</sql>
<select id="selectByExample" parameterType="aero.sita.gsl.ee.services.referencedata.generated.model.ReasonExample" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from REASON
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<delete id="deleteByExample" parameterType="aero.sita.gsl.ee.services.referencedata.generated.model.ReasonExample">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
delete from REASON
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="aero.sita.gsl.ee.services.referencedata.generated.model.Reason">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
insert into REASON (REASON_ID, IS_NOTE_REQUIRED, IS_SYSTEM_RESERVED,
EFFECTIVE_FROM, EFFECTIVE_TO, CREATED_ON,
CREATED_BY, LAST_UPDATED_ON, LAST_UPDATED_BY,
REASON_TYPE, REASON_NAME)
values (#{reasonId,jdbcType=CHAR}, #{isNoteRequired,jdbcType=BIT}, #{isSystemReserved,jdbcType=BIT},
#{effectiveFrom,jdbcType=NVARCHAR}, #{effectiveTo,jdbcType=NVARCHAR}, #{createdOn,jdbcType=TIMESTAMP},
#{createdBy,jdbcType=NVARCHAR}, #{lastUpdatedOn,jdbcType=TIMESTAMP}, #{lastUpdatedBy,jdbcType=NVARCHAR},
#{reasonType,jdbcType=NVARCHAR}, #{reasonName,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="aero.sita.gsl.ee.services.referencedata.generated.model.Reason">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
insert into REASON
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="reasonId != null">
REASON_ID,
</if>
<if test="isNoteRequired != null">
IS_NOTE_REQUIRED,
</if>
<if test="isSystemReserved != null">
IS_SYSTEM_RESERVED,
</if>
<if test="effectiveFrom != null">
EFFECTIVE_FROM,
</if>
<if test="effectiveTo != null">
EFFECTIVE_TO,
</if>
<if test="createdOn != null">
CREATED_ON,
</if>
<if test="createdBy != null">
CREATED_BY,
</if>
<if test="lastUpdatedOn != null">
LAST_UPDATED_ON,
</if>
<if test="lastUpdatedBy != null">
LAST_UPDATED_BY,
</if>
<if test="reasonType != null">
REASON_TYPE,
</if>
<if test="reasonName != null">
REASON_NAME,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="reasonId != null">
#{reasonId,jdbcType=CHAR},
</if>
<if test="isNoteRequired != null">
#{isNoteRequired,jdbcType=BIT},
</if>
<if test="isSystemReserved != null">
#{isSystemReserved,jdbcType=BIT},
</if>
<if test="effectiveFrom != null">
#{effectiveFrom,jdbcType=NVARCHAR},
</if>
<if test="effectiveTo != null">
#{effectiveTo,jdbcType=NVARCHAR},
</if>
<if test="createdOn != null">
#{createdOn,jdbcType=TIMESTAMP},
</if>
<if test="createdBy != null">
#{createdBy,jdbcType=NVARCHAR},
</if>
<if test="lastUpdatedOn != null">
#{lastUpdatedOn,jdbcType=TIMESTAMP},
</if>
<if test="lastUpdatedBy != null">
#{lastUpdatedBy,jdbcType=NVARCHAR},
</if>
<if test="reasonType != null">
#{reasonType,jdbcType=NVARCHAR},
</if>
<if test="reasonName != null">
#{reasonName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="aero.sita.gsl.ee.services.referencedata.generated.model.ReasonExample" resultType="java.lang.Long">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
select count(*) from REASON
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
update REASON
<set>
<if test="record.reasonId != null">
REASON_ID = #{record.reasonId,jdbcType=CHAR},
</if>
<if test="record.isNoteRequired != null">
IS_NOTE_REQUIRED = #{record.isNoteRequired,jdbcType=BIT},
</if>
<if test="record.isSystemReserved != null">
IS_SYSTEM_RESERVED = #{record.isSystemReserved,jdbcType=BIT},
</if>
<if test="record.effectiveFrom != null">
EFFECTIVE_FROM = #{record.effectiveFrom,jdbcType=NVARCHAR},
</if>
<if test="record.effectiveTo != null">
EFFECTIVE_TO = #{record.effectiveTo,jdbcType=NVARCHAR},
</if>
<if test="record.createdOn != null">
CREATED_ON = #{record.createdOn,jdbcType=TIMESTAMP},
</if>
<if test="record.createdBy != null">
CREATED_BY = #{record.createdBy,jdbcType=NVARCHAR},
</if>
<if test="record.lastUpdatedOn != null">
LAST_UPDATED_ON = #{record.lastUpdatedOn,jdbcType=TIMESTAMP},
</if>
<if test="record.lastUpdatedBy != null">
LAST_UPDATED_BY = #{record.lastUpdatedBy,jdbcType=NVARCHAR},
</if>
<if test="record.reasonType != null">
REASON_TYPE = #{record.reasonType,jdbcType=NVARCHAR},
</if>
<if test="record.reasonName != null">
REASON_NAME = #{record.reasonName,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sun Apr 05 18:38:46 IST 2020.
-->
update REASON
set REASON_ID = #{record.reasonId,jdbcType=CHAR},
IS_NOTE_REQUIRED = #{record.isNoteRequired,jdbcType=BIT},
IS_SYSTEM_RESERVED = #{record.isSystemReserved,jdbcType=BIT},
EFFECTIVE_FROM = #{record.effectiveFrom,jdbcType=NVARCHAR},
EFFECTIVE_TO = #{record.effectiveTo,jdbcType=NVARCHAR},
CREATED_ON = #{record.createdOn,jdbcType=TIMESTAMP},
CREATED_BY = #{record.createdBy,jdbcType=NVARCHAR},
LAST_UPDATED_ON = #{record.lastUpdatedOn,jdbcType=TIMESTAMP},
LAST_UPDATED_BY = #{record.lastUpdatedBy,jdbcType=NVARCHAR},
REASON_TYPE = #{record.reasonType,jdbcType=NVARCHAR},
REASON_NAME = #{record.reasonName,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
</mapper>
我的 ReasonMapper.java 类中有以下方法:
package aero.sita.gsl.ee.services.referencedata.generated.mapper;
import aero.sita.gsl.ee.services.referencedata.generated.model.Reason;
import aero.sita.gsl.ee.services.referencedata.generated.model.ReasonExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface ReasonMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table REASON
*
* @mbg.generated Sun Apr 05 18:38:46 IST 2020
*/
long countByExample(ReasonExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table REASON
*
* @mbg.generated Sun Apr 05 18:38:46 IST 2020
*/
int deleteByExample(ReasonExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table REASON
*
* @mbg.generated Sun Apr 05 18:38:46 IST 2020
*/
int insert(Reason record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table REASON
*
* @mbg.generated Sun Apr 05 18:38:46 IST 2020
*/
int insertSelective(Reason record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table REASON
*
* @mbg.generated Sun Apr 05 18:38:46 IST 2020
*/
List<Reason> selectByExample(ReasonExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table REASON
*
* @mbg.generated Sun Apr 05 18:38:46 IST 2020
*/
int updateByExampleSelective(@Param("record") Reason record, @Param("example") ReasonExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table REASON
*
* @mbg.generated Sun Apr 05 18:38:46 IST 2020
*/
int updateByExample(@Param("record") Reason record, @Param("example") ReasonExample example);
}
我正在寻找以下方法来插入一批原因对象,但现在能够理解如何将其映射到 xml 文件中。
int insertSelective(List<Reason> record);
我在配置文件中做了以下更改:
public class ReferenceDataConfiguration {
/** The datasource url. */
@Value("${gsl.ee.datasource.url}")
private String datasourceUrl;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(datasourceUrl);
return dataSource;
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("aero.sita.gsl.ee.services.referencedata");
return sessionFactory.getObject();
}
@Bean
@Primary
public SqlSessionTemplate batchSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory(), ExecutorType.BATCH);
}
我在 Dao 类上调用它如下:
for(Reason reasonInsert : reason){
num = reasonMapper.insertSelective(reasonInsert);
}
但是以上两个更改都没有做任何批量插入。