- 下面我们以面向接口的方式再进行一次 CRUD 的操作。
八、面向接口的方式进行CRUD
下面我们以面向接口的方式再进行一次 CRUD 的操作。
新建一个模块
mybatis-005-interface-crud
,然后:修改
pom.xml
文件,添加mybatis
、mysql
、junit
、logback
依赖。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
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.f</groupId>
<artifactId>mybatis-005-interface-crud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
</dependency>
<!--junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!--logback依赖-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.12</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>创建
SqlSessionUtil
工具类,放在utils
包下。- 注意:在
SqlSessionUtil
工具类中使用了ThreadLocal
。
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
46package com.f.mybatis.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* @author fzy
* @date 2023/12/31 20:59
*/
public class SqlSessionUtil {
private static SqlSessionFactory sqlSessionFactory = null;
private static ThreadLocal<SqlSession> local = new ThreadLocal<>();
static {
try {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
private SqlSessionUtil() {
}
public static SqlSession openSession() {
SqlSession sqlSession = local.get();
if (sqlSession == null) {
sqlSession = sqlSessionFactory.openSession();
// 将SqlSession对象绑定到当前线程上
local.set(sqlSession);
}
return sqlSession;
}
public static void close(SqlSession sqlSession) {
if (sqlSession != null) {
sqlSession.close();
// 解绑SqlSession对象。
// 因为Tomcat服务器支持线程池,也就是说,用过的线程对象t1,可能下一次还会使用这个t1线程,
// 为了避免错误使用之前绑定的SqlSession对象,所以要解绑
local.remove();
}
}
}- 注意:在
创建 POJO 类
Car
,放在pojo
包下。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
88package com.f.mybatis.pojo;
/**
* @author fzy
* @date 2023/12/31 22:06
* 封装汽车相关信息的javabean类。普通的的java类。
*/
public class Car {
// javabean 的属性应该和数据表中的字段一一对应
private Long id;
private String carNum;
private String brand;
private Double guidePrice;
private String produceTime;
private String carType;
public Car() {
}
public Car(Long id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {
this.id = id;
this.carNum = carNum;
this.brand = brand;
this.guidePrice = guidePrice;
this.produceTime = produceTime;
this.carType = carType;
}
public String toString() {
return "Car{" +
"id=" + id +
", carNum='" + carNum + '\'' +
", brand='" + brand + '\'' +
", guidePrice=" + guidePrice +
", produceTime='" + produceTime + '\'' +
", carType='" + carType + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCarNum() {
return carNum;
}
public void setCarNum(String carNum) {
this.carNum = carNum;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getGuidePrice() {
return guidePrice;
}
public void setGuidePrice(Double guidePrice) {
this.guidePrice = guidePrice;
}
public String getProduceTime() {
return produceTime;
}
public void setProduceTime(String produceTime) {
this.produceTime = produceTime;
}
public String getCarType() {
return carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
}创建
CarMapper
接口,放在mapper
包下。注意:在 MVC 架构模式中,将与数据库直接交互的接口命名为
CarDao
、包命名为dao
。而如果使用 mybatis 的话,一般不叫
XxxDao
,而是叫做XxxMapper
。包名也不叫
dao
,而是一般叫做mapper
。
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
50package com.f.mybatis.mapper; //mapper包就是dao包
import com.f.mybatis.pojo.Car;
import java.util.List;
/**
* @author fzy
* @date 2024/1/7 11:06
*/
// 使用mybatis的话,一般不叫XxxDao,而是叫做XxxMapper
public interface CarMapper {
/**
* 新增car
*
* @param car
*/
int insert(Car car);
/**
* 根据id删除car
*
* @param id
* @return
*/
int deleteById(Long id);
/**
* 更新car
*
* @param car
* @return
*/
int update(Car car);
/**
* 根据id查找car
*
* @param id
* @return
*/
Car selectById(Long id);
/**
* 查找所有car
*
* @return
*/
List<Car> selectAll();
}将配置文件
mybatis-config.xml
、jdbc.properties
、CarMapper.xml
、logback.xml
添加到resource
包下,即相当于添加到类的根路径下。其中:
CarMapper.xml
中的namespace
和id
需要保证和CarMapper
接口中的名称相对应,即:namespace
必须和mapper
接口的全限定名称一致,id
必须和mapper
接口中方法名一致。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
<mapper namespace="com.f.mybatis.mapper.CarMapper">
<insert id="insert">
INSERT INTO t_car VALUES (null, #{carNum}, #{brand}, #{guidePrice}, #{produceTime}, #{carType})
</insert>
<delete id="deleteById">
DELETE FROM t_car WHERE id = #{id}
</delete>
<update id="update">
UPDATE t_car
SET car_num = #{carNum},
brand = #{brand},
guide_price = #{guidePrice},
produce_time = #{produceTime},
car_type = #{carType}
WHERE id = #{id}
</update>
<select id="selectById" resultType="com.f.mybatis.pojo.Car">
SELECT
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
FROM t_car
WHERE id = #{id}
</select>
<select id="selectAll" resultType="com.f.mybatis.pojo.Car">
SELECT
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
FROM t_car
</select>
</mapper>
创建测试类
CarMapperTest
,对CarMapper
接口中的方法进行测试。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
73package com.f.mybatis.test;
import com.f.mybatis.mapper.CarMapper;
import com.f.mybatis.pojo.Car;
import com.f.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
/**
* @author fzy
* @date 2024/1/7 11:21
*/
public class CarMapperTest {
public void testInsert() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
Car car = new Car();
car.setCarNum("1003");
car.setBrand("问界");
car.setGuidePrice(40.00);
car.setProduceTime("2022-10-12");
car.setCarType("新能源");
int count = carMapper.insert(car);
sqlSession.commit();
System.out.println(count);
}
public void testDeleteById() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
int count = carMapper.deleteById(1L);
sqlSession.commit();
System.out.println(count);
}
public void testUpdate() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
Car car = new Car();
car.setId(2L);
car.setCarNum("1111");
car.setBrand("劳斯莱斯");
car.setGuidePrice(100.00);
car.setProduceTime("2023-10-12");
car.setCarType("混动");
int count = carMapper.update(car);
sqlSession.commit();
System.out.println(count);
}
public void testSelectById() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
Car car = carMapper.selectById(2L);
System.out.println(car);
}
public void testSelectAll() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
List<Car> cars = carMapper.selectAll();
cars.forEach(car -> {
System.out.println(car);
});
}
}