首页 java

在spring boot框架中使用mybatis操作数据库

发布于: 2024-04-21

加载mybatis依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependency>
<!--
数据库操作
https://github.com/mybatis/spring-boot-starter
https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/zh/index.html
-->
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

通过注解的方式

在配置文件配置数据库连接application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
server:
port: 8000

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/mall?characterEncoding=utf8&useSSL=false
mybatis:
configuration:
map-underscore-to-camel-case: true

定义CityMapper

1
2
3
4
5
6
7
8
9
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface CityMapper {
@Select("SELECT * FROM CITY WHERE state = #{state}")
City findByState(@Param("state") String state);
}

当数据库表特别多的时候,我们写一个interface的时候都需要加上@Mapper注解,这时候我们只需要在入口加上

1
2
3
4
5
6
7
8
9
10
11
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.github.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}

定义City实体

1
2
3
4
5
6
import lombok.Data;

@Data
public class City {
private Integer id;
}

写测试用例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CityMapperTest {

@Autowired
private CityMapper cityMapper;

public void testFindByState() {
City byState = cityMapper.findByState("1");
System.out.println(byState.getId());
}
}

Xml方式使用

在配置文件配置数据库连接application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
server:
port: 8000

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/example?characterEncoding=utf8&useSSL=false
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*.xml

首先定义city.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.github.example.mapper.CityMapper">
<select id="queryByState" resultType="com.github.example.model.City">
SELECT * FROM CITY WHERE state = #{state}
</select>
</mapper>

namespace:复制完整的包名并包含class

queryByState:class中的queryByState方法

resultType:返回结果映射的实体

定义CityMapper和City

通过注解方式已定义,只需要在CityMapper添加queryByState方法

1
2
3
4
5
6
7
8
9
10
11
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface CityMapper {
@Select("SELECT * FROM CITY WHERE state = #{state}")
City findByState(@Param("state") String state);

City queryByState(String state);
}

写测试用例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CityMapperTest {

@Autowired
private CityMapper cityMapper;

public void testqueryByState() {
City byState = cityMapper.queryByState("1");
System.out.println(byState.getId());
}
}