mybatis在MySQL中插入数据并返回主键

1、使用xml方式

     方式一:


        
            select last_insert_id()
        
        insert into t_user(username,password,birthday,sex,address)
        values (#{username},#{password},#{birthday},#{sex},#{address})

    方式二:


        insert into t_user(username,password,birthday,sex,address)
        values (#{username},#{password},#{birthday},#{sex},#{address})

2、使用注解方式

    方式一:

@InsertProvider(type = UserProvider.class,method = "insertUser")
@SelectKey(keyColumn = "id",keyProperty = "id",before = false,resultType = Integer.class,statement = "select last_insert_id()")
Integer addUser(User user);

   方式二:

@InsertProvider(type = UserProvider.class,method = "insertUser")
@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
Integer addUser(User user);

 

你可能感兴趣的:(MySQL数据库,mybatis框架)