其他和之前相同:user.xml
INSERT INTO USER
(`username`,
`birthday`,
`sex`,
`address`)
VALUES (#{username},
#{birthday},
#{sex},
#{address});
自增返回:
INSERT INTO USER
(`username`,
`birthday`,
`sex`,
`address`
)
VALUES (#{username},
#{birthday},
#{sex},
#{address}
);
Mysql的uuid返回主键,在user类以及表中添加uuid1属性,
SELECT UUID()
INSERT INTO USER
(`username`,
`birthday`,
`sex`,
`address`,
`uuid1`)
VALUES (#{username},
#{birthday},
#{sex},
#{address},
#{uuid1});
test
@Test
public void testInsertUser() throws IOException {
// 创建SqlSession对象
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
User user = new User();
user.setUsername("张鸡儿");
user.setAddress("榆林土龙");
user.setBirthday(new Date());
user.setSex("1");
// 执行插入
sqlSession.insert("user.insertUser", user);
System.out.println(user);
// 提交事务
sqlSession.commit();
// 释放资源
sqlSession.close();
}