Mybatis 实现 DAO 层的开发
# 100.Mybatis 实现 DAO 层的开发
通过之前的博客,我们对 Mybatis 的基本使用有了一定的认知,接下来我们实战下,用 Mybatis 实现 DAO 层的开发
# 自定义实现类
之前我们都是使用代理对象的方式,来完成数据库的 CRUD,接下来我们继续讲解不通过代理对象,而是自己实现 IUserDao 接口的类里完成 CRUD。
创建 UserDaoImpl 类,并实现 findAll 方法:
package com.peterjxl.dao.impl;
import com.peterjxl.dao.IUserDao;
import com.peterjxl.domain.QueryVo;
import com.peterjxl.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class UserDaoImpl implements IUserDao {
private SqlSessionFactory factory;
public UserDaoImpl(SqlSessionFactory factory){
this.factory = factory;
}
@Override
public List<User> findAll() {
SqlSession session = factory.openSession();
List<User> users = session.selectList("com.peterjxl.dao.IUserDao.findAll");
session.close();
return users;
}
@Override
public void saveUser(User user) {
}
@Override
public void updateUser(User user) {
}
@Override
public void deleteUser(Integer userId) {
}
@Override
public User findById(Integer userId) {
return null;
}
@Override
public List<User> findByName(String name) {
return null;
}
@Override
public int findTotal() {
return 0;
}
@Override
public List<User> findUserByVo(QueryVo vo) {
return null;
}
}
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
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
# 新增测试类
为了和之前的代码区分开,这里新建了一个测试类 MybatisTestImpl;
这里我们 init 方法和 destory 方法就简化了,只需要 InputStream 对象和一个 IUserDao 对象:
package com.peterjxl.test;
import com.peterjxl.dao.IUserDao;
import com.peterjxl.dao.impl.UserDaoImpl;
import com.peterjxl.domain.User;
import org.apache.ibatis.io.Resources;
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.List;
public class MybatisTestImpl {
private InputStream in;
private IUserDao userDao;
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
userDao = new UserDaoImpl(factory);
}
@After
public void destory() throws IOException {
in.close();
}
@Test
public void testFindAll(){
List<User> users = userDao.findAll();
for(User user : users){
System.out.println(user);
}
}
}
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
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
然后我们测试 testFindAll,可以看到是能正常运行的,该案例其实我们之前也讲过。这里重新引出这一个案例,是为了再次告诉读者,使用自定义实现类的话,有很多重复的代码,例如获取 session,资源的释放等。
# 完善自定义实现类
这里我们继续完善实现类的其他方法:其实代码都差不多,QueryVo 的方式我们就不演示了:
package com.peterjxl.dao.impl;
import com.peterjxl.dao.IUserDao;
import com.peterjxl.domain.QueryVo;
import com.peterjxl.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class UserDaoImpl implements IUserDao {
private SqlSessionFactory factory;
public UserDaoImpl(SqlSessionFactory factory){
this.factory = factory;
}
@Override
public List<User> findAll() {
SqlSession session = factory.openSession();
List<User> users = session.selectList("com.peterjxl.dao.IUserDao.findAll");
session.close();
return users;
}
@Override
public void saveUser(User user) {
SqlSession session = factory.openSession();
session.insert("com.peterjxl.dao.IUserDao.saveUser", user);
session.commit();
session.close();
}
@Override
public void updateUser(User user) {
SqlSession session = factory.openSession();
session.update("com.peterjxl.dao.IUserDao.updateUser", user);
session.commit();
session.close();
}
@Override
public void deleteUser(Integer userId) {
SqlSession session = factory.openSession();
session.delete("com.peterjxl.dao.IUserDao.deleteUser", userId);
session.commit();
session.close();
}
@Override
public User findById(Integer userId) {
SqlSession session = factory.openSession();
User u = session.selectOne("com.peterjxl.dao.IUserDao.findById", userId);
session.close();
return u;
}
@Override
public List<User> findByName(String name) {
SqlSession session = factory.openSession();
List<User> users = session.selectList("com.peterjxl.dao.IUserDao.findByName", name);
session.close();
return users;
}
@Override
public int findTotal() {
SqlSession session = factory.openSession();
int n = session.selectOne("com.peterjxl.dao.IUserDao.findTotal");
session.close();
return n;
}
@Override
public List<User> findUserByVo(QueryVo vo) {
return null;
}
}
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
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
# 测试其他方法
package com.peterjxl.test;
import com.peterjxl.dao.IUserDao;
import com.peterjxl.dao.impl.UserDaoImpl;
import com.peterjxl.domain.User;
import org.apache.ibatis.io.Resources;
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.List;
public class MybatisTestImpl {
private InputStream in;
private IUserDao userDao;
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
userDao = new UserDaoImpl(factory);
}
@After
public void destory() throws IOException {
in.close();
}
@Test
public void testFindAll(){
List<User> users = userDao.findAll();
for(User user : users){
System.out.println(user);
}
}
@Test
public void testSave() throws IOException {
User user = new User();
user.setUserName("dao impl user");
user.setUserAddress("广州市番禺区");
user.setUserSex("男");
user.setUserBirthday(new Date());
System.out.println("保存操作之前:: " + user);
userDao.saveUser(user);
System.out.println("保存操作之前:: " + user);
}
@Test
public void testUpdate(){
User user = new User();
user.setUserId(41); //张三的数据
user.setUserName("mybatis update impl user");
user.setUserAddress("广州市番禺区");
user.setUserSex("男");
user.setUserBirthday(new Date());
userDao.updateUser(user);
}
@Test
public void testDelete(){
userDao.deleteUser(55);
}
@Test
public void testFindOne(){
User user = userDao.findById(41);
System.out.println(user);
}
@Test
public void testFindByName(){
List<User> users = userDao.findByName("mybatis%");
for(User user : users){
System.out.println(user);
}
}
@Test
public void testFindTotal(){
int total = userDao.findTotal();
System.out.println("total: " + total);
}
}
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
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
# 源码
所有代码已上传到了 GitHub (opens new window) 和 Gitee (opens new window) 上,并且创建了分支 demo10,读者可以通过切换分支来查看本文的示例代码。
上次更新: 2024/10/1 21:14:36