增删改
Mybatis为我们提供了<insert>、<update>、<delete>标签来对应增删改操作
在接口中写增删改的抽象方法
- void addEmp(Employee e);
- void updateEmp(Employee e);
- void deleteEmp(Employee e);
在映射文件中写sql语句
- <insert id="addEmp" parameterType="com.figsprite.bean.Employee">
- insert into tb_employee(last_name,email,gender)
- values(#{lastName},#{email},#{gender})
- </insert>
- <update id="updateEmp" parameterType="com.figsprite.bean.Employee">
- update tb_empolyee
- set last_name=#{lastName},email=#{email},gender=#{genser}
- where id=#{id}
- </update>
- <delete id="deleteEmp" parameterType="com.figsprite.bean.Employee">
- delete from tb_employee where id=#{id}
- </delete>
顾名思义,parameterType就是参数类型,而我们通过#{字段名}的方式来传递对象中的属性,resultType允许使用String、Boolean等基本类型包作为返回值
测试
- @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();
- }finally {
- openSession.close();
- }
- }
- @Test
- public void test4() throws IOException{
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- SqlSession openSession = sqlSessionFactory.openSession();
- Employee e = new Employee();
- e.setId(1);
- e.setGender("0");
- e.setEmail("qwq");
- e.setLastName("Jerry");
- try {
- EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
- mapper.updateEmp(e);
- openSession.commit();
- }finally {
- openSession.close();
- }
- }
这里我们要自动提交数据,我们也可以选择SqlSession openSession = sqlSessionFactory.openSession(true); 这样就不用手动提交了。
- @Test
- public void test5 ()throws IOException{
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- SqlSession openSession = sqlSessionFactory.openSession();
- Employee e = new Employee();
- e.setId(1);
- try{
- EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
- mapper.deleteEmp(e);
- openSession.commit();
- }finally {
- openSession.close();
- }
- }
insert获取自增主键值
我们在上面的例子中可以看到,我们插入时候并没有指定id,因为我们id是通过自增方式,JDBC里面提供了这样的方法
ResultSet getGeneratedKeys()
MyBatis也支持,当然它的底层,其实就是这个函数,我们仅需在<insert>标签里添加属性即可
useGeneratedKeys="true"
使用自增主键获取主键值策略,接着用keyProperty这个属性,指定对应主键属性,也就是Mybatis获取到逐渐使以后将它赋给JavaBean的对应属性
- <insert id="addEmp" parameterType="com.figsprite.bean.Employee"
- 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