工程文件预览:
操作系统:Windows10
数据库:MySQL8
IDE:Eclipse 2019
JDK:1.8.0_181
Maven:3.8.1
一、环境搭建
1、创建数据库表
2、创建Spring项目并引入相关配置
(1)登录网站https://start.spring.io/
(2)选择项目类型,语言,文件名
(3)选择依赖配置并点击创建按钮
(4)加入阿里巴巴的Druid数据源依赖
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
(5)完整的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 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.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBootMyBatisTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootMyBatisTest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>
</properties>
<dependencies>
<!-- web开发的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jdbc依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybatis的起步依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- mysql的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 测试类依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Maven打包工具插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、端口号、数据库配置
创建application.yml文件
server:
port: 8081
spring:
dataSource:
url: jdbc:mysql://localhost:3306/db_mybatis?serverTimezone=UTC
username: root
password: root
#使用Druid数据源
driver-class-name: com.mysql.cj.jdbc.Driver
maxActive: 100
initialSize: 20
minIdle: 10
mybatis:
mapper-locations: classpath:mapper/*.xml #mapper映射文件路径
4、自定义配置类进行Druid数据源属性值注入
- 创建DataSourceConfig.java文件
- @Configuration注解标识了一个自定义配置类DataSourceConfig,在该配置类中通过@Bean注解注入了一个DataSource实例对象
- @ConnfigurationProperties(prifix=“spring.datasource”)注解的作用是将全局配置文件中以spring.datasource开头的属性值注入到getDruid()方法返回的DataSource类对象属性中,这样就可以完成第三方数据源参数值的注入
package com.example.config;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource getDruid() {
return new DruidDataSource();
}
}
二、SpringBoot整合Mybatis
1、创建实体类User及其setter、getter方法
package com.example.domain;
public class User {
private int id;
private String username;
private String jobs;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String toString() {
return "User{"+
"id="+id+
",username="+username+
",password="+jobs+
"}";
}
}
2、创建实体类的映射文件UserMapper.xml
<?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.example.mapper.UserMapper">
<select id="findUserById" parameterType="Integer" resultType="com.example.domain.User">
select * from t_user where id=#{id}
</select>
</mapper>
3、创建接口文件UserMapper.java
- 定义方法,目的是调用映射文件中封装的SQL语句
- @Mapper注解表示该类是一个MyBatis接口文件,并保证能够被SpringBoot自动扫描到Spring容器中
package com.example.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.example.domain.User;
@Mapper
public interface UserMapper {
public User findUserById(int id);
}
4、定义API接口
创建HelloController.java
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.domain.User;
import com.example.mapper.UserMapper;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello World6666";
}
@Autowired
private UserMapper userMapper;
@RequestMapping("/tt")
public void find() {
User user=userMapper.findUserById(1);
System.out.println(user.toString());
}
}
三、实现效果
利用Java应用程序启动主函数
(1)在浏览器中输入localhost:8081/hello
(2)在浏览器中输入localhost:8081/tt
在控制台输出查询的数据库信息:
四、常见问题
1、导入SpringBoot项目后,pom.xml文件第一行爆错(红叉)
原因:maven的插件版本问题,造成与IDE不兼容
解决方法:增加如下代码