使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法。 Mapper接口开发方式是基于入门程序的基础上,对 控制程序 进行分层开发,程序员只需要 编写mapper接口 和 Mappe.xml 配置文件即可,程序员编写mapper接口需要遵循一些开发规范,mybatis可以自动生成mapper接口实现类代理对象。
一、SQL配置文件
1、EmpMapper.xml
使用Mapper动态代理的方式开发时,映射文件的namespace必须为 Mapper接口文件 的全名。
1 <?xml version="1.0" encoding="UTF-8" ?>
2 /p>
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5
6
7
8 select * from emp where empno = #{empno}
9
10
11
12
13 select * from emp where ename like '%${value}%'
14
15
16
17
18
19 select LAST_INSERT_ID()
20
21 insert into emp(ename,job) values(#{ename},#{job})
22
23
24
25
26 delete from emp where empno=#{empno}
27
28
29
30
31 update emp set ename=#{ename},job=#{job}
32 where empno=#{empno}
33
34
2、pojo类
1 package po;
2 //导入相关类
3 public class Emp {
4 private int empno;
5 private String ename;
6 private String job;
7 private int mgr;
8 private Date hiredate;
9 private double sal;
10 private double comm;
11 private int deptno;
12
13 @Override
14 public String toString() {
15 return "编号:"+empno+"姓名:"+ename+"工作:"+job+"\n";
16 }
17 set()/get()方法 ...
18 }
3、SqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 /p>
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5
6
7
8
9
10
11
12
13
14
16
17
18
19
20
21
22
23
24
25
26
二、Dao层
1、开发规范
程序员编写mapper接口需要遵循一些开发规范,mybatis可以自动生成mapper接口实现类代理对象。相关的开发规范如下:
① 在mapper.xml中namespace等于mapper接口地址
② 接口中的方法名和mapper.xml中statement的id一致
③ 接口中的方法输入参数类型和mapper.xml中statement的parameterType指定的类型一致
④ 接口中的方法返回值类型和mapper.xml中statement的resultType指定的类型一致
总结:以上开发规范主要是对下边的代码进行统一生成:
1 //在方法体内通过SqlSessionFactory创建SqlSession
2 SqlSession sqlSession = sqlSessionFactory.openSession();
3 //业务操作语句
4 Emp emp = sqlSession.selectOne("test.findEmpById",empno);
2、Mapper接口
1 package Mapper;
2 //导入相关类
3 public interface EmpMapper {
4 public Emp findEmpById(int empno);
5 public List findEmpByEmpname(String tname);
6 public int insertEmp(Emp emp);
7 public int deleteEmpById(int empno);
8 public int updateEmp(Emp emp);
9 }
三、测试程序
1、Junit单元测试
1 package Test;
2 //导入相关类
3 public class Mybatis_Mapper {
4
5 //使用 单例模式 管理会话工厂
6 private SqlSessionFactory sqlSessionFactory;
7 @Before
8 public void createSqlSessionFactory() throws IOException {
9 // Mybatis配置文件
10 String resource = "SqlMapConfig.xml";
11 // 得到配置文件流
12 InputStream inputStream = Resources.getResourceAsStream(resource);
13 // 创建会化工厂,传入Mybatis配置文件信息
14 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
15 }
16
17 // 根据 id查询部门信息
18 @Test
19 public void testFindEmpById() {
20 SqlSession sqlSession = sqlSessionFactory.openSession();
21 //创建EmpMapper对象,mybatis自动生成mapper代理对象
22 EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
23 Emp emp = null;
24
25 try {
26 //调用empMapper的方法
27 emp = empMapper.findEmpById(7900);
28 }finally {
29 sqlSession.close();
30 }
31
32 System.out.println(emp);
33 }
34
35 // 根据员工姓名模糊查询员工信息
36 @Test
37 public void testFindEmpByEmpname() {
38 SqlSession sqlSession = sqlSessionFactory.openSession();
39 EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
40 List emp = null;
41
42 try {
43 //调用empMapper的方法
44 emp = empMapper.findEmpByEmpname("th");
45 }
46 finally {
47 sqlSession.close();
48 }
49
50 for(Emp e:emp) {
51 System.out.println(e);
52 }
53 }
54
55 // 添加员工信息
56 @Test
57 public void testInsert() {
58 SqlSession sqlSession = sqlSessionFactory.openSession();
59 EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
60 Emp emp = new Emp();
61 emp.setEname("admin");
62 emp.setJob("admin");
63
64 int flag = 0;
65 try {
66 flag = empMapper.insertEmp(emp);
67 }finally {
68 sqlSession.commit();
69 sqlSession.close();
70 }
71
72 if(flag == 1) {
73 System.out.println("自增主键值:"+emp.getEmpno());
74 }
75 }
76
77 // 更新员工信息
78 @Test
79 public void testUpdate() {
80 SqlSession sqlSession = sqlSessionFactory.openSession();
81 EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
82 Emp emp = new Emp();
83 emp.setEmpno(7950);
84 emp.setEname("test7950");
85 emp.setJob("test7950");
86
87 int flag = 0;
88 try {
89 flag = empMapper.updateEmp(emp);
90 }finally {
91 sqlSession.commit();
92 sqlSession.close();
93 }
94
95 if(flag == 1) {
96 System.out.println("更新成功!");
97 }
98 }
99
100 // 根据id删除员工信息
101 @Test
102 public void testDelete() {
103 SqlSession sqlSession = sqlSessionFactory.openSession();
104 EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
105
106 int flag = 0;
107 try {
108 flag = empMapper.deleteEmpById(7950);
109 }finally {
110 sqlSession.commit();
111 sqlSession.close();
112 }
113
114 if(flag == 1) {
115 System.out.println("删除成功!");
116 }
117 }
118 }
2、测试结果
(1)根据 id查询部门信息
(2)根据员工姓名模糊查询员工信息
(3)添加员工信息
(4)更新员工信息
(5)根据id删除员工信息
-