说明:MyCAT自增长主键和返回生成主键ID的实现 1) mysql本身对非自增长主键,使用last_insert_id()是不会返回结果的,只会返回0:这里做一个简单的测试 创建测试表 -------------------------------------- --创建测试表 ------------------------------------- USE test; CREATE TABLE IF NOT EXISTS t_auto_increment ( id INT NOT NULL
有时候插入记录之后需要使用到插入记录的主键,通常是再查询一次来获取主键,但是MyBatis插入记录时可以设置成返回主键id,简化操作,方法大致有两种. 对应实体类: public class User { private int userId; private String userName; private int userAge; } 对应DAO类: public interface UserMapper { int save(User user); } 方法一.使用useGenerated
先说一下没有注解的 先给出实体类: public class City { private int city_id; private String city_name; public int getCity_id() { return city_id; } public void setCity_id(int city_id) { this.city_id = city_id; } public String getCity_name() { return city_name; } public
一个自增长ID的对象被save或者persist后,会返回其主键ID: Department department = new Department(); department.setName("xxxxxxx"); session.persist(department); System.out.println("id = " + department2.getId()); Department department2 = new Department(); dep