时间:2021-07-01 10:21:17 帮助过:2人阅读
<insert id="create" parameterType="User" useGeneratedKeys="true" keyProperty="id"> INSERT INTO t_user(<include refid="all_property"/>) VALUES (#{createTime}, #{mobile}, #{email},#{QQ}) </insert>
IUserMapper.java
void create(User user);
Test 代码
@Test public void testCreate() { SqlSession session = DBUtil.getSqlSession(); IUserMapper userMapper = session.getMapper(IUserMapper.class); User user = userMapper.find("18910000012"); // user.setId(0L); user.setMobile("18900121033"); userMapper.create(user); System.out.println(user.getId()); session.commit(); DBUtil.close(session); }
结果(返回的 ID):15
其他情况如何获取id还有待实验,这种传入pojo的方式很简单,不需要 <selectKey />(我也没通过这个标签得到过id)
mybatis insert into mysql with auto_increment key
标签: