3_sqlSession完成DML所有操作
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312133115517-743352210.gif)
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312133115517-743352210.gif)
Mapper映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="EmpMapper3">
<!--
增删方法的返回值类型都是int
resultType就无需指定了
insert update delete 标签中没有resultType
但是仍然可以有paramaterType
-->
<!-- 增加方法
public int addEmp(Emp emp);
-->
<insert id="addEmp" parameterType="emp">
insert into emp values(#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
</insert>
<!--修改
根据工号修改员工姓名
public int updateEmp(Emp emp);
-->
<update id="updateEmp" parameterType="emp">
update emp set ename = #{ename} where empno=#{empno}
</update>
<!-- 删除
删除大于给定工号的员工信息
public int deleteEmp(int empno)
-->
<delete id="deleteEmp" parameterType="int">
delete from emp where empno >= #{empno}
</delete>
</mapper>
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312133115517-743352210.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312133115517-743352210.gif)
测试代码
package com.msb.test;
import com.msb.pojo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
public class Test4 {
private SqlSession sqlSession;
@Before
public void init(){
SqlSessionFactoryBuilder ssfb =new SqlSessionFactoryBuilder();
InputStream resourceAsStream = null;
try {
resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory factory=ssfb.build(resourceAsStream) ;
sqlSession=factory.openSession(true);
}
@Test
public void testInsert(){
Emp emp =new Emp(null,"按住啦Baby","SALESMAN",7839,new Date(),3100.0, 200.0,10 );
int rows = sqlSession.insert("addEmp", emp);
System.out.println(rows);
// 手动提交事务
//sqlSession.commit();
/*增删改 要提交事务
* sqlSession.commit();手动提交事务
* sqlSession=factory.openSession(true); 设置事务自动提交
* */
}
@Test
public void testUpdate(){
Emp emp =new Emp( );
emp.setEname("晓明");
emp.setEmpno(7937);
int rows = sqlSession.update("updateEmp", emp);
System.out.println(rows);
}
@Test
public void testDelete(){
int rows = sqlSession.delete("deleteEmp", 7936);
System.out.println(rows);
}
@After
public void release(){
// 关闭SQLSession
sqlSession.close();
}
}
![](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312133115517-743352210.gif)
![点击并拖拽以移动](https://img2023.cnblogs.com/blog/2531222/202303/2531222-20230312133115517-743352210.gif)