14.MyBatis实现CURD操作

 

1.新建一个java Dynamic Web Project

2.引入相关依赖包

3.新建mybatis.cfg.xml

新建一个source folder: config

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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<!-- MyBatis的整体配置 -->
<configuration>

<!-- 数据源环境配置,default默认数据源的id -->
<environments default="mysql_test">

<!-- mysql 测试数据库 -->
<environment id="mysql_test">

<!-- mybatis的事务管理器 -->
<transactionManager type="JDBC"></transactionManager>

<!-- 配置数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>

<!-- mysql 生产数据库 -->
<!-- <environment id="mysql_prod"></environment> -->

<!-- oracle 数据库 -->
<!-- <environment id="oracle"></environment> -->

</environments>

<!-- 配置Mapper.xml文件 -->
<mappers>
<mapper resource="com/course/mapper/UserMapper.xml" />
</mappers>

</configuration>

新建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
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
package com.course.vo;

import java.util.Date;

public class User {

private Integer id;
private String name;
private Integer age;
private String address;
private Date birthday;

public User() {
super();
}

public User(String name, Integer age, String address, Date birthday) {
super();
this.name = name;
this.age = age;
this.address = address;
this.birthday = birthday;
}

public User(Integer id, String name, Integer age, String address, Date birthday) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.birthday = birthday;
}

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", birthday="
+ birthday + "]";
}

}

新建Mapper.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
<?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只是用来标识该mapper,与接口名及其实现类名没有关系 -->
<mapper namespace="com.course.dao.abc.UserDao">
<insert id="addUserXml" parameterType="com.course.vo.User">
insert into user(name, age, address, birthday)
values(#{name}, #{age}, #{address}, #{birthday})
</insert>

<update id="updateUser" parameterType="com.course.vo.User">
update user set name=#{name}, age=#{age}, address=#{address}, birthday=#{birthday}
where id=#{id}
</update>

<delete id="deleteUser" parameterType="int">
delete from user
where id=#{val}
</delete>

<select id="getUserById" parameterType="int" resultType="com.course.vo.User">
select * from user where id=#{val}
</select>

<select id="getAllUser" resultType="com.course.vo.User">
select * from user
</select>
</mapper>

新建接口UserDao

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.course.dao;

import java.util.List;

import com.course.vo.User;

public interface UserDao {

public void addUser(User user);

public void updateUser(User user);

public void deleteUser(Integer id);

public User getUserById(Integer id);

public List<User> getAllUser();

}

新建接口的实现类

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
package com.course.dao.impl;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.course.dao.UserDao;
import com.course.vo.User;

public class UserDaoImpl implements UserDao {

@Override
public void addUser(User user) {
// 1.得到mybtais.cfg.xml的配置文件
InputStream inputStream = this.getClass().getResourceAsStream("/mybatis.cfg.xml");
// 2.得到SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 3.sqlSessionFactoryBuilder去构造SqlSessionFactory
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
// 4.从SqlSessionFactory得到sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5.进行CRUD
/**
* 参数1: 对应的是UserMapper.xml的命名空间值+<insert>节点的id属性值
* 参数2: UserMapper.xml里面<insert id="参数1">的parameterType属性对应的类型的对象
*/
sqlSession.insert("com.course.dao.abc.UserDao.addUserXml", user);
// 6.提交事务
sqlSession.commit();
// 7.关闭session
sqlSession.close();

}

@Override
public void updateUser(User user) {
// 1.得到mybtais.cfg.xml的配置文件
InputStream inputStream = this.getClass().getResourceAsStream("/mybatis.cfg.xml");
// 2.得到SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 3.sqlSessionFactoryBuilder去构造SqlSessionFactory
SqlSessionFactory sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
// 4.从SqlSessionFactory得到sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5.进行CRUD
sqlSession.update("com.course.dao.abc.UserDao.updateUser", user);
// 6.提交事务
sqlSession.commit();
// 7.关闭sqlSession
sqlSession.close();

}

@Override
public void deleteUser(Integer id) {
// 1.得到mybtais.cfg.xml的配置文件
InputStream inputStream = this.getClass().getResourceAsStream("/mybatis.cfg.xml");
// 2.得到SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 3.sqlSessionFactoryBuilder去构造SqlSessionFactory
SqlSessionFactory sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
// 4.从SqlSessionFactory得到sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5.进行CRUD
sqlSession.delete("com.course.dao.abc.UserDao.deleteUser", id);
// 6.提交事务
sqlSession.commit();
// 7.关闭sqlSession
sqlSession.close();

}

@Override
public User getUserById(Integer id) {
// 1.得到mybtais.cfg.xml的配置文件
InputStream inputStream = this.getClass().getResourceAsStream("/mybatis.cfg.xml");
// 2.得到SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 3.sqlSessionFactoryBuilder去构造SqlSessionFactory
SqlSessionFactory sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
// 4.从SqlSessionFactory得到sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5.进行CRUD
User user = sqlSession.selectOne("com.course.dao.abc.UserDao.getUserById", id);
// 6.提交事务
sqlSession.commit();
// 7.关闭sqlSession
sqlSession.close();

return user;
}

@Override
public List<User> getAllUser() {
// 1.得到mybtais.cfg.xml的配置文件
InputStream inputStream = this.getClass().getResourceAsStream("/mybatis.cfg.xml");
// 2.得到SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 3.sqlSessionFactoryBuilder去构造SqlSessionFactory
SqlSessionFactory sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
// 4.从SqlSessionFactory得到sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5.进行CRUD
List<User> userList = sqlSession.selectList("com.course.dao.abc.UserDao.getAllUser");
// 6.提交事务
sqlSession.commit();
// 7.关闭sqlSession
sqlSession.close();

return userList;
}
}

新建测试类

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
package com.course.test;

import java.util.List;

import com.course.dao.UserDao;
import com.course.dao.impl.UserDaoImpl;
import com.course.vo.User;

public class MainTest {

public static void main(String[] args) {

UserDao userDao = new UserDaoImpl();

// User user1 = new User("user1", 10, "address1", new Date());
// User user2 = new User("user2", 10, "address2", new Date());
// User user3 = new User("user3", 10, "address3", new Date());
// userDao.addUser(user1);
// userDao.addUser(user2);
// userDao.addUser(user3);

// User user2 = new User(2, "user2", 10, "address2", new Date());
// userDao.updateUser(user2);

// userDao.deleteUser(10);

// User user2 = userDao.getUserById(2);
// System.out.println(user2.toString());

List<User> userList = userDao.getAllUser();
for (User user : userList) {
System.out.println(user.toString());
}
}
}
1
2
3
4
5
6
7
8
9
10
User [id=1, name=user1, age=10, address=address1, birthday=Fri Aug 13 12:35:22 CST 2021]
User [id=2, name=user2, age=10, address=address2, birthday=Fri Aug 13 16:51:11 CST 2021]
User [id=4, name=user1, age=10, address=address1, birthday=Fri Aug 13 16:55:22 CST 2021]
User [id=5, name=user2, age=10, address=address2, birthday=Fri Aug 13 16:55:22 CST 2021]
User [id=7, name=user1, age=10, address=address1, birthday=Fri Aug 13 16:57:01 CST 2021]
User [id=8, name=user2, age=10, address=address2, birthday=Fri Aug 13 16:57:01 CST 2021]
User [id=9, name=user3, age=10, address=address3, birthday=Fri Aug 13 16:57:01 CST 2021]
User [id=11, name=user11, age=10, address=address11, birthday=Fri Aug 13 16:58:27 CST 2021]
User [id=12, name=user12, age=10, address=address2, birthday=Mon Aug 16 15:54:29 CST 2021]
User [id=14, name=user3, age=10, address=address3, birthday=Mon Aug 16 15:53:30 CST 2021]

直接执行sql操作的是mapper.xml,Dao接口及其实现类只是使用mybatis的方法调用mapper.xml执行sql操作,Dao接口及其实现类与mapper.xml没有直接的关系。