您现在的位置是:首页 >其他 >SpringBoot整合Mybatis网站首页其他
SpringBoot整合Mybatis
简介SpringBoot整合Mybatis
目录
2.4 使用Mybatis自动生成mapper层,Service层,Controller层以及mapper映射文件
1.整合数据源
首先无论是Mybatis还是Mybatis-Plus都需要整合数据源,这里拿MySQL数据库作为演示。
1.1引入依赖
<!--mysql的驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
1.2配置数据源信息
这里用druid最为数据库连接池,写在在resoures下面自动创建的一个配置文件application.properties。
1.3测试连接池是否配置成功
在test类中测试一下如下图
测试成功如图
2.SpringBoot整合Mybatis
2.1引入相关依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ykq</groupId>
<artifactId>qy163-springboot02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>qy163-springboot02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis和springboot整合的依赖 启动依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2添加配置文件
添加配置文件application.properties映射:
#指定映射文件所在的路径-- mybatis.mapper-locations=classpath:mapper/*.xml #mybatis日志文件 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #更改端口号 默认8080 (可以不更改) server.port=8181
2.3 创建数据库
创建完随便添加点数据进行测试
create table company.student
(
cid int auto_increment comment '学生编号'
primary key,
classno int not null comment '班级序号',
name varchar(255) not null comment '学生名字',
age int not null comment '学生年龄'
);
2.4 使用Mybatis自动生成mapper层,Service层,Controller层以及mapper映射文件
生成之后的目录
(1)重写mapper接口
public interface StudentMapper {
public List<Student> findAll();
}
(2)为mapper接口生成代理实现类
@SpringBootApplication
@MapperScan(basePackages ="com.yzl.mapper")
public class SpringbootworkApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootworkApplication.class, args);
}
}
(3)测试
@SpringBootTest
class SpringbootworkApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
void contextLoads() {
System.out.println(studentMapper.findAll());
}
}
(4)测试成功结果展示
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。