mybatis 通用mapper.xml中 insert 方法不是自增主键怎么返回

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"/>

参考技术A 方式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"/>

通用Mapper

原理是:拦截器

1、假设:使用MyBatis只需要定义Mapper接口,无需编写Mapper.xml文件

  如果实现无需编写Mapper.xml文件,我们必须要实现动态拼接SQL

如何实现动态拼接SQL语句?

思路:编写Mybatis的插件,在执行过程中动态生成SQL语句

2、简介:

  Mybatis通用 Mapper极其方便的使用Mybatis单表的增删改查,支持单表操作,不支持通用的多表联合查询。

3、在Mybatis的配置文件中进行配置

  3.1 分页助手也是拦截器技术,所以注意顺序,分页助手配置在通用mapper的前面

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <plugins>
        <!-- 配置分页助手 -->
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql" />
            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
            <property name="rowBoundsWithCount" value="true" />
        </plugin>
        <!-- 配置通用Mapper -->
        <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
            <!--主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
            <property name="IDENTITY" value="HSQLDB" />
            <!--通用Mapper接口,多个通用接口用逗号隔开 -->
            <property name="mappers" value="com.github.abel533.mapper.Mapper" />
        </plugin>

    </plugins>

</configuration>

  3.2  mapper继承Mapper<?>

public interface NewUserMapper extends Mapper<User>{
    
}

  3.3 service 使用

@Service
public class NewUserService {
    
    @Autowired
    private NewUserMapper newUsermapper;

    public EasyUIResult queryUserList(Integer page, Integer rows) {
        //设置分页参数
        PageHelper.startPage(page,rows);
        //設置查詢條件
        Example example=new Example(User.class);
        example.setOrderByClause("created DESC");
        List<User> users = this.newUsermapper.selectByExample(example);
        PageInfo<User> pageinfo=new PageInfo<User>(users);
        return new EasyUIResult(pageinfo.getTotal(),pageinfo.getList());
        
    }
    
}

 

以上是关于mybatis 通用mapper.xml中 insert 方法不是自增主键怎么返回的主要内容,如果未能解决你的问题,请参考以下文章

通用Mapper

mybatis的小坑 ### The error may exist in com/vector/dao/*Mapper.xml

springboot整合mybatis通用Mapper

MyBatis Mapper.xml文件中 和#的区别

还在用逆向工程?太Low了,试试通用Mapper吧!

mybatis 怎么调用mapper.xml中的方法