MyBatis映射文件1(增删改、insert获取自增主键值)

时间:2023-03-10 08:29:57
MyBatis映射文件1(增删改、insert获取自增主键值)

增删改

Mybatis为我们提供了<insert>、<update>、<delete>标签来对应增删改操作

在接口中写增删改的抽象方法

  1. void addEmp(Employee e);  
  2. void updateEmp(Employee e);  
  3. void deleteEmp(Employee e);  

在映射文件中写sql语句

  1. <insert id="addEmp" parameterType="com.figsprite.bean.Employee">  
  2.     insert into tb_employee(last_name,email,gender)  
  3.     values(#{lastName},#{email},#{gender})  
  4. </insert>  
  5. <update id="updateEmp" parameterType="com.figsprite.bean.Employee">  
  6.     update tb_empolyee  
  7.       set last_name=#{lastName},email=#{email},gender=#{genser}  
  8.       where id=#{id}  
  9. </update>  
  10. <delete id="deleteEmp" parameterType="com.figsprite.bean.Employee">  
  11.     delete from tb_employee where id=#{id}  
  12. </delete>

顾名思义,parameterType就是参数类型,而我们通过#{字段名}的方式来传递对象中的属性,resultType允许使用String、Boolean等基本类型包作为返回值

测试

  1. @Test  
  2.     public void test3() throws IOException{  
  3.         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
  4.         SqlSession openSession = sqlSessionFactory.openSession();  
  5.         Employee e = new Employee();  
  6.         e.setGender("1");  
  7.         e.setLastName("Hello");  
  8.         e.setEmail("qwewqeqw");  
  9.         e.toString();  
  10.         try {  
  11.             EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);  
  12.             mapper.addEmp(e);  
  13.             openSession.commit();  
  14.         }finally {  
  15.             openSession.close();  
  16.         }  
  17.     }  
  1. @Test  
  2.     public void test4() throws IOException{  
  3.         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
  4.         SqlSession openSession = sqlSessionFactory.openSession();  
  5.         Employee e = new Employee();  
  6.         e.setId(1);  
  7.         e.setGender("0");  
  8.         e.setEmail("qwq");  
  9.         e.setLastName("Jerry");  
  10.         try {  
  11.             EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);  
  12.             mapper.updateEmp(e);  
  13.             openSession.commit();  
  14.         }finally {  
  15.             openSession.close();  
  16.         }  
  17.     }  

这里我们要自动提交数据,我们也可以选择SqlSession openSession = sqlSessionFactory.openSession(true); 这样就不用手动提交了。

  1. @Test  
  2. public void test5 ()throws IOException{  
  3.     SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
  4.     SqlSession openSession = sqlSessionFactory.openSession();  
  5.     Employee e = new Employee();  
  6.     e.setId(1);  
  7.     try{  
  8.         EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);  
  9.         mapper.deleteEmp(e);  
  10.         openSession.commit();  
  11.     }finally {  
  12.         openSession.close();  
  13.     }  

insert获取自增主键值

我们在上面的例子中可以看到,我们插入时候并没有指定id,因为我们id是通过自增方式,JDBC里面提供了这样的方法

ResultSet getGeneratedKeys()

MyBatis也支持,当然它的底层,其实就是这个函数,我们仅需在<insert>标签里添加属性即可

useGeneratedKeys="true"

使用自增主键获取主键值策略,接着用keyProperty这个属性,指定对应主键属性,也就是Mybatis获取到逐渐使以后将它赋给JavaBean的对应属性

  1. <insert id="addEmp" parameterType="com.figsprite.bean.Employee"  
  2.     useGeneratedKeys="true" keyProperty="id">  

让我们做个测试

@Test

public void test3() throws IOException{

SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

SqlSession openSession = sqlSessionFactory.openSession();

Employee e = new Employee();

e.setGender("1");

e.setLastName("Hello");

e.setEmail("qwewqeqw");

e.toString();

try {

EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);

mapper.addEmp(e);

openSession.commit();

System.out.println(e.getId());

}finally {

openSession.close();

}

}

如果我们没有写useGenerateKeys和KeyProperty,那么上述代码打印出来的将会是null