Mybatis框架入门程序增删改查用户

​ 本篇文章接着上一篇最后的程序进行了增加和改写,实现了利用MyBatis框架对数据的增删改查。本次测试方法有所不同,没有在main方法下执行而是利用了@Test注解,自己导一下库,然后可以通过右键Run as ->JUnit Test来进行测试。然后就是一直有个小问题,就是日志一直不显示在控制台,我查了也不好使,将来再进行更改。

​ 数据库测试前数据库数据如图:

image-20191004190847850

image-20191004190954556

JUnit信息:

image-20191004191036443

数据库数据:

image-20191004191154155

接下来附上源代码:

首先还是项目结构:

image-20191004191304564

Customer.java

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
package com.dm.po;
/*
*
* 客户持久类
*/
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username + ", jobs="
+ jobs + ", phone=" + phone + "]";
}


}

CustomerMapper.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
<?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为命名空间 -->
<mapper namespace="com.dm.po.Customer">
<!-- 根据编号获取客户信息 parameterType为插入类型 ,resultType为返回类型 -->
<select id="findCustomerByID" parameterType="Integer"
resultType="com.dm.po.Customer">
<!-- #{id}相当于? -->
select * from customer where id = #{id}
</select>
<!-- 根据客户模糊查询 -->
<select id="findCustomerByName" parameterType="String"
resultType="com.dm.po.Customer">
<!-- concat字符串连接 -->
select *from customer where username like concat('%',#{username},'%')

</select>
<!--添加顾客 -->
<insert id="addCustomer" parameterType="com.dm.po.Customer">
insert into customer
(username,jobs,phone)values(#{username},#{jobs},#{phone})
</insert>
<!--更新用户信息 -->
<update id="updateCustomer" parameterType="com.dm.po.Customer">
update customer set
username =#{username},jobs=#{jobs},phone=#{phone} where id=#{id}

</update>
<!-- 删除顾客信息 -->
<delete id="deleteCustomer" parameterType="Integer">
delete from customer
where id =#{id}

</delete>
</mapper>

MybatisTest.java

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
package com.dm.po.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.junit.internal.runners.model.EachTestNotifier;

import com.dm.po.Customer;

/*
* 入门程序测试:
* */
public class MybatisTest {
/*
* 根据用户id查询数据
* */
@Test
public void findCustomerByNameTest() throws Exception{
//1.读取配置文件
String resource="mybatis-config.xml";
InputStream inputStream =Resources.getResourceAsStream(resource);
//2.根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
//3.通过sqlsessionFactory构建sqlsession
SqlSession sqlSession=sqlSessionFactory.openSession();
//4.sqlsession执行映射文件中定义的Sql,并返回映射结果
//第一个参数是sql的id,第二个参数是传入给sql的占位符参数
Customer customer=sqlSession.selectOne("com.dm.po.Customer.findCustomerByID",2);
//打印输出结果
System.out.println("根据id查询用户");
System.out.println(customer.toString());
//5.关闭sqlsession
sqlSession.close();

}
/*
* 根据用户名称模糊查询
* */
@Test
public void findCustomerByName()throws Exception{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
List <Customer> list=sqlSession.selectList("com.dm.po.Customer.findCustomerByName", "d");
System.out.println("根据模糊查询用户姓名查询顾客");
for (Customer customer : list) {
System.out.println(customer);
}
sqlSession.close();
}

// 添加用户
@Test
public void addCustomerTest() throws Exception{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
// 添加操作 所以应该有对象
Customer customer=new Customer();
customer.setUsername("row");
customer.setJobs("学生");
customer.setPhone("7777777");
// 返回行数
int rows=sqlSession.insert("com.dm.po.Customer.addCustomer", customer);
if(rows>0){
System.out.println("您成功插入"+rows+"条数据");

}else{
System.out.println("执行插入操作失败");
}
// 增删改都需要提交事务
sqlSession.commit();
sqlSession.close();

}
//更新用户
@Test
public void updateCustomerTest() throws Exception{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
// 添加操作 所以应该有对象
Customer customer=new Customer();
customer.setId(3);
customer.setUsername("rows");
customer.setJobs("程序员");
customer.setPhone("7777777");
// 返回行数
int rows=sqlSession.update("com.dm.po.Customer.updateCustomer", customer);
if(rows>0){
System.out.println("您成功更新"+rows+"条数据");

}else{
System.out.println("执行插入操作失败");
}
// 增删改都需要提交事务
sqlSession.commit();
sqlSession.close();

}
//删除用户
@Test
public void deleteCustomerTest() throws Exception{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
int rows=sqlSession.delete("com.dm.po.Customer.deleteCustomer", 3);
if(rows>0){
System.out.println("您成功删除"+rows+"条数据");

}else{
System.out.println("执行插入操作失败");
}
// 增删改都需要提交事务
sqlSession.commit();
sqlSession.close();
}


}

log4j.properties

1
2
3
4
5
6
7
8
# Global logging configuration 
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.dm=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mybatis-config.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
<?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">
<configuration>
<!-- 配置环境 默认的环境 id为 mysql -->
<environments default="mysql">
<environment id="mysql">
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 配置mapper的位置 -->
<mappers>
<mapper resource="com/dm/po/CustomerMapper.xml" />
</mappers>
</configuration>
-------------本文结束感谢您的阅读-------------
0%