mybatis 通用mapper.xml中 insert 方法不是自增主键怎么返回
方式1:
<insert id="insert" parameterType="cn.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>
调用Mapper返回主键:
//生成新对象用于插入
DicCity city = new DicCity();
city.setCityCode("330100");
city.setCityName("杭州市");
//获取mapper对象
DicCityMapper cityMapper = (DicCityMapper) SpringContextUtil.getBean("dicCityMapper");
int row = cityMapper.insert(city); //insrt不再返回主键,只返回响应行数,这点和ibatis不同了
System.out.println("响应的行数:"+row);
//取得自增的标识列 ID的值
System.out.println("新插入的数据的ID:"+city.getId());
====================
如果使用mybatis.generator来生成DAO层的话,generatorConfig.xml中要使用:
<generatedKeycolumn="id"sqlStatement="MySql"identity="true"/>
<insert id="insert" parameterType="cn.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>
调用Mapper返回主键:
//生成新对象用于插入
DicCity city = new DicCity();
city.setCityCode("330100");
city.setCityName("杭州市");
//获取mapper对象
DicCityMapper cityMapper = (DicCityMapper) SpringContextUtil.getBean("dicCityMapper");
int row = cityMapper.insert(city); //insrt不再返回主键,只返回响应行数,这点和ibatis不同了
System.out.println("响应的行数:"+row);
//取得自增的标识列 ID的值
System.out.println("新插入的数据的ID:"+city.getId());
====================
如果使用mybatis.generator来生成DAO层的话,generatorConfig.xml中要使用:
<generatedKeycolumn="id"sqlStatement="MySql"identity="true"/>