@Test
    public void testadd() throws IOException {
    //1.创建SQLSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSessionFactory();
        //回去sqlsession ,相当于jdbc中的connection
        SqlSession session = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
            Employee employee = new Employee(null,"小花","[email protected]",30000.00,1);
            mapper.addEmployee(employee);
            session.commit();
            System.out.println("这个对象为"+employee);
        } finally {
            session.close();
        }

这个对象为Employee [id=null, lastName=小花, [email protected], salary=30000.0, deptId=1]

一、支持自增ID的数据库 如:mysql、sqlServer

<insert id="addEmployee" parameterType="com.atgui.spring.mybatis.entities.Employee"
   useGeneratedKeys="true" keyProperty="id">
      insert into employees(last_name,email,salary,dept_id) values(#{lastName},#{email},#{salary},#{deptId})
  </insert>

加上这个  employee ID值就有了

这个对象为Employee [id=7, lastName=小花, [email protected], salary=30000.0, deptId=1]

不支持自增的数据库  如:oracle

Mybatis主键生成方式

相关文章:

  • 2021-11-22
  • 2022-12-23
  • 2021-11-13
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2021-11-18
  • 2022-02-01
  • 2021-09-04
  • 2022-12-23
相关资源
相似解决方案