最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User(); user.setUserName("chenzhou"); user.setPassword("xxxx"); user.setComment("测试插入数据返回主键功能"); System.out.println("插入前主键为:
1.Java代码: 1.1 entity类: User.java public class User { private int userId; private String userName; private String password; private String comment; //这里要添加 setter and getter 略 } 1.2 DAO类: UserDao.java public interface UserDao { public int insertAndGe