MyBatis insert操作に主キーの戻り値を取得する方法

1.SqlServer2005の定義
create table Dic_City (
ID int identity,
City_Code varchar(10) not null,
Provinces_Code varchar(20) not null,
State_Code varchar(10) not null,
City_Name varchar(50) null,
PY_Code varchar(50) null,
PY_Code_Short varchar(10) null,
Ext1 varchar(20) null,
Ext2 varchar(20) null,
Ext3 varchar(20) null,
Ext4 varchar(20) null,
Ext5 varchar(20) null,
constraint PK_DIC_CITY primary key (ID)
)

2.MyBatisXMLの配置

配置方法1
<insert id="insert" parameterType="ja.softsea.model.DicCity" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
SELECT @@IDENTITY
</selectKey>
insert into Dic_City (City_Code, Provinces_Code,
State_Code, City_Name, PY_Code,
PY_Code_Short, Ext1, Ext2,
Ext3, Ext4, Ext5)
values (#{cityCode,jdbcType=VARCHAR}, #{provincesCode,jdbcType=VARCHAR},
#{stateCode,jdbcType=VARCHAR}, #{cityName,jdbcType=VARCHAR}, #{pyCode,jdbcType=VARCHAR},
#{pyCodeShort,jdbcType=VARCHAR}, #{ext1,jdbcType=VARCHAR}, #{ext2,jdbcType=VARCHAR},
#{ext3,jdbcType=VARCHAR}, #{ext4,jdbcType=VARCHAR}, #{ext5,jdbcType=VARCHAR})
</insert>
配置方法2
<insert id="insert" parameterType="cn.softsea.model.DicCity" useGeneratedKeys="true" keyProperty="id">
insert into Dic_City (City_Code, Provinces_Code,
State_Code, City_Name, PY_Code,
PY_Code_Short, Ext1, Ext2,
Ext3, Ext4, Ext5)
values (#{cityCode,jdbcType=VARCHAR}, #{provincesCode,jdbcType=VARCHAR},
#{stateCode,jdbcType=VARCHAR}, #{cityName,jdbcType=VARCHAR}, #{pyCode,jdbcType=VARCHAR},
#{pyCodeShort,jdbcType=VARCHAR}, #{ext1,jdbcType=VARCHAR}, #{ext2,jdbcType=VARCHAR},
#{ext3,jdbcType=VARCHAR}, #{ext4,jdbcType=VARCHAR}, #{ext5,jdbcType=VARCHAR})
</insert>
3.Mapperを呼び出して、主キーを戻る
//挿入オブジェクトを作成
DicCity city = new DicCity();
city.setCityCode(“104003");
city.setCityName(“東京都品川区");
//mapperオブジェクトを取得
DicCityMapper cityMapper = (DicCityMapper) SpringContextUtil.getBean(“dicCityMapper");
int row = cityMapper.insert(city);
System.out.println(“応答行目数:"+row);
//自動採番のID値
System.out.println(“新たに挿入されたID:"+city.getId());

Oracle

Posted by arkgame