普通模式,也称为传统DAO模式,就是在传统DAO模式下,定义接口和实现类,如 interface EmpDao class EmpDaoImpl implements EmpDao. 在实现类中,用SQLSession对象调用 select insert delete update 等方法实现.目前极为少见.在传统模式下,我们需要知道SqlSession对象 实现CURD和 参数传递的处理
1.mybatis查询的三种方式
SqlSession对象本身的API中就有三个查询方法,分别能够实现如下查询方式
1返回单个对象 selectOne
2返回对象List集合 selectList
3返回对象Map集合 selectMap
创建Emp实体类
@AllArgsConstructor @NoArgsConstructor @Data public class Emp implements Serializable { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; private Integer deptno; }
准备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="EmpMapper"> <!-- 返回单个对象 public Emp findOne(); id 相当于方法名 resultType 相当于返回值类型 sql语句的查询结果用哪个类来进行封装 如果返回值类型是集合,这里写的也是集合中的元素对应的类,不是集合本身作为类型 paramaterType 参数类型 SQL语句就是具体的方法体的实现 --> <select id="findOne" resultType="emp" > select * from emp where empno = 7499 </select> <!-- 返回多个对象List集合 查询全部的员工信息 public List<Emp> findAll() --> <select id="findAll" resultType="emp"> select * from emp </select> <!--返回多个对象的Map集合 把查询出来的数据中的某一列作为键,整条数据封装的对象作为值 public Map<key,Emp> findEmpMap() <empno,Emp> <key,Emp> --> <select id="findEmpMap" resultType="map"> select * from emp </select> </mapper>
SQLMapConfig中导入EmpMapper映射文件
<!--加载mapper映射文件--> <mappers> <mapper resource="com/msb/mapper/DeptMapper.xml"/> <mapper resource="com/msb/mapper/EmpMapper.xml"/> </mappers>
2.mybatis参数传递的三种方式
1 单个基础数据类型作为参数
2 多个基础数据类型的map 集合作为参数
3 引用类型作为参数
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="EmpMapper2"> <!-- 参数为一个基本数据类型 根据员工工号查询员工的全部信息,返回单个员工对象 public Emp findByEmpno(int empno); parameterType 在有参数情况下也是可以省略不写 mybatis 可以根据实际情况自动判断 如果要写parameterType 那么就要写对 在SQL语句上可以使用${} #{} 代表参数的占位 如果参数是单个基本数据类型,{}中名字可以随便写,见名知意 ${} 代表mybatis底层使用Statment语句对象,参数是以字符串拼接的形式设置 #{} 代表mybatis底层使用的preparedStatment语句对象,参数使用?作为占位符处理 #{} 以后常用 --> <select id="findByEmpno" resultType="emp" parameterType="int"> select * from emp where empno = #{empno} </select> <!-- 参数为map集合 查询指定部门号和指定最低薪资的员工信息 20 号部门 且工资在1500以上的员工信息 public List<Emp> findEmpByDeptnoAndSal(int deptno,double sal); < > 最好要进行转译处理,参照HTML转译 w3school在线文档中有转译符号对应规则 Map<String,Object> args=new HashMap<>(); args.put("deptno", 20); args.put("sal", 1500.0); #{}中写的是map集合中,参数的键 --> <select id="findEmpByDeptnoAndSal" resultType="emp" parameterType="map"> select * from emp where deptno = #{deptno} and sal >= #{sal} </select> <!-- 参数为对象 emp >>> deptno sal 参数是我们自定义的类型,那么 #{}中写的是参数的属性名 --> <select id="findEmpByDeptnoAndSal2" resultType="emp" parameterType="emp"> select * from emp where deptno = #{deptno} and sal >= #{sal} </select> </mapper>
3.mybatis实现CURD
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>
测试代码:
/** * @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(); } }