springboot与mybatis整合技术
2021-07-28 本文已影响0人
JohnYuCN
本文接续前一篇Mybatis框架使用基础
1. pom.xml 进行增加新的内容,同时对配置进行了优化:
<?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>cn.johnyu</groupId>
<artifactId>mybatis-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2. mapper及映射文件:
如果按以下结构(同一classpath下,并且主文件名称相同),则可以采用零配置的方式进行整合(只需要在主启动中加入@MapperScan)
截屏2021-07-28下午4.49.11.png
(1)mapper接口 (此情况可以不加入@Mapper)
package cn.johnyu.mapper;
import cn.johnyu.pojo.Employee;
import org.springframework.stereotype.Component;
@Component
public interface EmployeeMapper {
Employee load(Integer id);
}
(2)映射文件:
此时文件的位置、名字,namespace和id的值都是采用的“约定”方式
<?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="cn.johnyu.mapper.EmployeeMapper">
<select id="load" resultType="cn.johnyu.pojo.Employee">
select empno as id,ename,job from emp where empno=#{id}
</select>
</mapper>
3. application.yml
要求数据源必须由IOC接管
#要求数据源必须由IOC接管
spring:
datasource:
url: jdbc:mysql://localhost:3306/scott
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123
4. 如果Mapper和映射文件不使用约定方式,则需要进行配置:
如果映射文件的名字更动了,则需要进行以下的配置
applicaiton.yml中添加以下内容:
mybatis:
# 在配置文件指定映射文件位置
# config-location: classpath:mybatis-conf.xml
# 也可以直接指定:映射文件或mapper类(和以上配置,二选一)
mapper-locations: classpath:cn/johnyu/mapper/MyEmployeeMapper.xml
或者:添加mybatis的配置文件(注意二选一):
mybatis-conf.xml :
<?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>
<!-- 有关环境的配置将转移到Spring IOC中-->
<!-- <environments > ... -->
<mappers>
<mapper resource="cn/johnyu/mapper/MyEmployeeMapper.xml"/>
</mappers>
</configuration>
5. 最后主启动类和测试类:
package cn.johnyu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
@SpringBootApplication
@MapperScan(value = "cn.johnyu.mapper")
public class MainApp {
public static void main(String[] args) throws IOException {
SpringApplication.run(MainApp.class,args);
}
}
测试类:
package cn.johnyu;
import cn.johnyu.mapper.EmployeeMapper;
import cn.johnyu.pojo.Employee;
import org.junit.Test;
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;
@SpringBootTest
@RunWith(SpringRunner.class)
public class Test1 {
@Autowired
private EmployeeMapper mapper;
@Test
public void test1(){
Employee employee=mapper.load(7369);
System.out.println(employee.getEname());
}
}