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

    <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。

posted on 2015-06-14 20:08  颓废的悠然  阅读(457)  评论(0编辑  收藏  举报

导航