mybatis获取插入的语句主键(自增主键)

时间:2021-02-10 06:26:26
    <insert id="insertUser" parameterType="User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into users(name, age, gmt_create, money) VALUE ('xiao', 24, '1991-06-26', 10000)
</insert>
     SqlSessionFactory sessionFactory = MybatisUtils.getFactory();
SqlSession session = sessionFactory.openSession();
String statement = "test.UserMapper.insertUser";
Date now = new Date();
User user = new User("xiao", 18, now, 1000);
session.insert(statement, user);
System.out.println(user.getId());
session.commit();
session.close();

如果注释after那段,输出的为0。