使用MyBatis编写Dao的两种语法

在MyBatis中,我们有两种Dao的写法,一种叫传统Dao写法,一种叫Mapper代理接口。下面看看如何实现。

1 传统Dao写法

1.1 编写CustomerDao接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Dao接口
*/
public interface CustomerDao {

/**
* 查询所有用户
*/
public List<Customer> findAll();

/**
* 添加
*/
public void save(Customer customer);

/**
* 修改
*/
public void update(Customer customer);

/**
* 查询一个
*/
public Customer findById(Integer id);

/**
* 条件查询
*/
public List<Customer> findByName(String name);

/**
* 删除
*/
public void delete(Integer id);
}

1.2 编写CustomerDao实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Dao实现类
*/
public class CustomerDaoImpl implements CustomerDao{
@Override
public List<Customer> findAll() {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSession();
return sqlSession.selectList("com.yiidian.dao.CustomerDao.findAll");
} catch (Exception e) {
e.printStackTrace();
} finally{
sqlSession.close();
}
return null;
}

@Override
public void save(Customer customer) {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSession();
sqlSession.insert("com.yiidian.dao.CustomerDao.save", customer);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally{
sqlSession.close();
}
}

@Override
public void update(Customer customer) {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSession();
sqlSession.update("com.yiidian.dao.CustomerDao.update", customer);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally{
sqlSession.close();
}
}

@Override
public Customer findById(Integer id) {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSession();
return sqlSession.selectOne("com.yiidian.dao.CustomerDao.findById",id);
} catch (Exception e) {
e.printStackTrace();
} finally{
sqlSession.close();
}
return null;
}

@Override
public List<Customer> findByName(String name) {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSession();
return sqlSession.selectList("com.yiidian.dao.CustomerDao.findByName",name);
} catch (Exception e) {
e.printStackTrace();
} finally{
sqlSession.close();
}
return null;
}

@Override
public void delete(Integer id) {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSession();
sqlSession.delete("com.yiidian.dao.CustomerDao.delete", id);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally{
sqlSession.close();
}
}
}

传统方式的重点在于Dao实现类,在Dao实现类中,手动调用SqlSession提供的方法直接执行映射文件的SQL语句。

1.3 编写CustomerDao.xml映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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">
<!--
namespace: 用于指定该映射文件需要映射的Dao接口
-->
<mapper namespace="com.yiidian.dao.CustomerDao">

<!--查询所有-->
<select id="findAll" resultType="com.yiidian.domain.Customer">
select * from t_customer
</select>

<!--1.添加方法-->
<insert id="save" parameterType="com.yiidian.domain.Customer">
INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
</insert>

<!--2.修改方法-->
<update id="update" parameterType="com.yiidian.domain.Customer">
UPDATE t_customer SET
NAME = #{name},
gender = #{gender},
telephone = #{telephone}
WHERE id = #{id}
</update>

<!--查询一个-->
<select id="findById" parameterType="integer" resultType="com.yiidian.domain.Customer">
select * from t_customer where id = #{id}
</select>

<!--条件查询-->
<select id="findByName" parameterType="string" resultType="com.yiidian.domain.Customer">
select * from t_customer where name like #{name}
</select>

<!--删除-->
<delete id="delete" parameterType="integer">
delete from t_customer where id = #{id}
</delete>
</mapper>

1.4 编写测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* MyBatis测试类 - 传统Dao写法
*/
public class TestCustomerDao {

/**
* 添加
*/
@Test
public void testSave(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.创建传统Dao实现类对象
CustomerDao customerDao = new CustomerDaoImpl();

//3.调用save方法
Customer customer = new Customer();
customer.setName("小苍");
customer.setGender("女");
customer.setTelephone("15755556666");
customerDao.save(customer);


//4.关闭连接
session.close();
}

/**
* 修改
*/
@Test
public void testUpdate(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.创建传统Dao实现类对象
CustomerDao customerDao = new CustomerDaoImpl();

//3.调用update方法
Customer customer = new Customer();
customer.setId(5);
customer.setName("小泽");
customer.setGender("女");
customer.setTelephone("15755556666");
customerDao.update(customer);

session.commit();

//4.关闭连接
session.close();
}

/**
* 查询所有
*/
@Test
public void testFindAll(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.创建传统Dao实现类对象
CustomerDao customerDao = new CustomerDaoImpl();

//3.调用findAll方法
List<Customer> list = customerDao.findAll();

for(Customer cust:list){
System.out.println(cust);
}

//4.关闭连接
session.close();
}

/**
* 查询一个
*/
@Test
public void testFindById(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.创建传统Dao实现类对象
CustomerDao customerDao = new CustomerDaoImpl();

//3.调用findById方法
Customer customer = customerDao.findById(3);

System.out.println(customer);

//4.关闭连接
session.close();
}

/**
* 条件查询
*/
@Test
public void testFindByName(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.创建传统Dao实现类对象
CustomerDao customerDao = new CustomerDaoImpl();

//3.调用findByName方法
List<Customer> list = customerDao.findByName("%小%");

for(Customer cust:list){
System.out.println(cust);
}

//4.关闭连接
session.close();
}

/**
* 删除
*/
@Test
public void testDelete(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.创建传统Dao实现类对象
CustomerDao customerDao = new CustomerDaoImpl();

//3.调用findByName方法
customerDao.delete(5);

// 提交事务
session.commit();

//4.关闭连接
session.close();
}
}

2 Mapper代理接口

2.1 编写CustomerDao接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Dao接口
*/
public interface CustomerDao {

/**
* 查询所有用户
*/
public List<Customer> findAll();

/**
* 添加
*/
public void save(Customer customer);

/**
* 修改
*/
public void update(Customer customer);

/**
* 查询一个
*/
public Customer findById(Integer id);

/**
* 条件查询
*/
public List<Customer> findByName(String name);

/**
* 删除
*/
public void delete(Integer id);
}

2.2 编写CustomerDao.xml映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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">
<!--
namespace: 用于指定该映射文件需要映射的Dao接口
-->
<mapper namespace="com.yiidian.dao.CustomerDao">

<!--查询所有-->
<select id="findAll" resultType="com.yiidian.domain.Customer">
select * from t_customer
</select>

<!--1.添加方法-->
<insert id="save" parameterType="com.yiidian.domain.Customer">
INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
</insert>

<!--2.修改方法-->
<update id="update" parameterType="com.yiidian.domain.Customer">
UPDATE t_customer SET
NAME = #{name},
gender = #{gender},
telephone = #{telephone}
WHERE id = #{id}
</update>

<!--查询一个-->
<select id="findById" parameterType="integer" resultType="com.yiidian.domain.Customer">
select * from t_customer where id = #{id}
</select>

<!--条件查询-->
<select id="findByName" parameterType="string" resultType="com.yiidian.domain.Customer">
select * from t_customer where name like #{name}
</select>

<!--删除-->
<delete id="delete" parameterType="integer">
delete from t_customer where id = #{id}
</delete>
</mapper>

2.3 编写测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* MyBatis测试类 - Mapper代理接口
*/
public class TestCustomerDao2 {

/**
* 添加
*/
@Test
public void testSave(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.生成Dao代理对象
CustomerDao customerDao = session.getMapper(CustomerDao.class);

//3.调用save方法
Customer customer = new Customer();
customer.setName("小苍");
customer.setGender("女");
customer.setTelephone("15755556666");
customerDao.save(customer);


//4.关闭连接
session.close();
}

/**
* 修改
*/
@Test
public void testUpdate(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.生成Dao代理对象
CustomerDao customerDao = session.getMapper(CustomerDao.class);

//3.调用update方法
Customer customer = new Customer();
customer.setId(5);
customer.setName("小泽");
customer.setGender("女");
customer.setTelephone("15755556666");
customerDao.update(customer);

session.commit();

//4.关闭连接
session.close();
}

/**
* 查询所有
*/
@Test
public void testFindAll(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.生成Dao代理对象
CustomerDao customerDao = session.getMapper(CustomerDao.class);

//3.调用findAll方法
List<Customer> list = customerDao.findAll();

for(Customer cust:list){
System.out.println(cust);
}

//4.关闭连接
session.close();
}

/**
* 查询一个
*/
@Test
public void testFindById(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.生成Dao代理对象
CustomerDao customerDao = session.getMapper(CustomerDao.class);

//3.调用findById方法
Customer customer = customerDao.findById(5);

System.out.println(customer);

//4.关闭连接
session.close();
}

/**
* 条件查询
*/
@Test
public void testFindByName(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.生成Dao代理对象
CustomerDao customerDao = session.getMapper(CustomerDao.class);

//3.调用findByName方法
List<Customer> list = customerDao.findByName("%小%");

for(Customer cust:list){
System.out.println(cust);
}

//4.关闭连接
session.close();
}

/**
* 删除
*/
@Test
public void testDelete(){
//1.获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();

//2.生成Dao代理对象
CustomerDao customerDao = session.getMapper(CustomerDao.class);

//3.调用findByName方法
customerDao.delete(5);

// 提交事务
session.commit();

//4.关闭连接
session.close();
}
}