Springboot 整合 MyBatis + Sqlite

Springboot 整合 MyBatis + Sqlite

pom.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<!-- Source: https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.8</version>
</parent>

<properties>
<maven.compiler.source>27</maven.compiler.source>
<maven.compiler.target>27</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- Source: https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.48</version>
<scope>compile</scope>
</dependency>
<!-- Source: https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.53.4.0</version>
<scope>compile</scope>
</dependency>
<!-- Source: https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>compile</scope>
</dependency>
<!-- Source: https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>4.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>

项目结构

com.mine.mapper 和 pojo

正常写mapper和pojo

1
2
3
4
5
6
7
8
9
@Data
public class Customer {
private Integer customerId;
private String customerName;

public Customer(String customerName){
this.customerName = customerName;
}
}
1
2
3
4
5
6
7
8
9
@Mapper
public interface CustomerMapper {

List<Customer> list();

Customer getById(Integer customerId);

int insert(Customer customer);
}

resources/mapper

正常写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
<?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="com.mine.mapper.CustomerMapper">

<select id="list" resultType="com.mine.pojo.Customer">
SELECT customerId, customerName
FROM t_customer
</select>

<select id="getById" resultType="com.mine.pojo.Customer">
SELECT customerId, customerName
FROM t_customer
WHERE customerId = #{customerId}
</select>

<insert id="insert"
parameterType="com.mine.pojo.Customer"
useGeneratedKeys="true"
keyProperty="customerId">
INSERT INTO t_customer(customerName)
VALUES (#{customerName})
</insert>

</mapper>

重点:application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server:
port: 8081

mybatis:
mapper-locations: classpath:/mapper/*.xml # 自动扫描 mapper 所在位置,注意必须是xml结尾
type-aliases-package: com.mine.pojo # 自动别名
configuration:
map-underscore-to-camel-case: true # 自动映射数据库字段名称
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

spring:
datasource:
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite:app.db
username: ""
password: ""
sql:
init:
mode: always # 使用 resources/schema.sql 来自动初始化数据库

resources/schema.sql

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE IF NOT EXISTS t_customer (
customerId INTEGER PRIMARY KEY AUTOINCREMENT,
customerName TEXT
);

CREATE TABLE IF NOT EXISTS t_order (
orderId INTEGER PRIMARY KEY AUTOINCREMENT,
orderName TEXT,
customerId INTEGER,
FOREIGN KEY (customerId) REFERENCES t_customer (customerId)
);

Controller层

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private CustomerMapper customerMapper;

@RequestMapping()
public List<User> getUsers() {
customerMapper.insert(new Customer("123"));
return userMapper.selectUsers();
}
}

Springboot 整合 MyBatis + Sqlite
https://taylorandtony.github.io/2026/10/14/Springboot-整合-MyBatis-Sqlite/
作者
TaylorAndTony
发布于
2026年10月14日
许可协议