=============== MyBatis回填主键 =============== public interface StudentDao { ... // 增加学生返回标识 public int addHasId(Student student); ... } 将生成的主键回填到 ... _____实体对象的id属性中_____ | v insert into t_student(number, name, gender, age) values(#{number}, #{name}, #{gender}, #{age}) ... public class StudentDaoTest { ... // 测试增加学生返回标识 @Test public void testAddHasId() { // SQL会话工厂建造器 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); try { // SQL会话 SqlSession session = new SqlSessionFactoryBuilder() .build(Resources.getResourceAsStream("mybatis-config.xml")) .openSession(); // 数据访问对象 StudentDao dao = session.getMapper(StudentDao.class); // 学生实体 Student student = new Student(0, "1004", "赵云", "男", 20); // 增加学生返回标识 int rows = dao.addHasId(student); // 打印受影响的行数 System.out.println(rows); // 打印新增学生标识 System.out.println(student.getId()); // 提交事务 session.commit(); } catch (IOException e) { e.printStackTrace(); } } ... } 例程:HelloMyBatis