开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第14天,点击查看活动详情
Spring Boot 连接 MySQL 实践
MySQL 是最流行的关系型数据库管理系统之一,在 Web 应用开发中,MySQL 关系型数据库是一个十分不错的选择 Java 程序在进行与 MySQL 的连接时需要通过 JDBC 来实现,JDBC 全称为 Java Data Base Connectivity(Java 数据库连接),主要由接口组成,是一种用于执行 SQL 语句的 Java API,各个数据库厂家基于它各自实现了自己的驱动程序(Driver)
回忆以前连接mysql的方法
// 第一步,注册驱动程序
Class.forName("数据库驱动的完整类名");
// 第二步,获取一个数据库的连接
Connection conn = DriverManager.getConnection("数据库地址","用户名","密码");
// 第三步,创建一个会话
Statement stmt=conn.createStatement();
// 第四步,执行SQL语句
stmt.executeUpdate("SQL 语句");
// 或者查询记录
ResultSet rs = stmt.executeQuery("查询记录的SQL语句");
// 第五步,对查询的结果进行处理
while(rs.next()){
// 操作
}
// 第六步,关闭连接
rs.close();
stmt.close();
conn.close();
Spring Boot如何使用JDBC
首先引入依赖的jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
在添加完相关依赖之后,需要启动MySQL数据库并在新建的Spring Boot项目中配置数据库连接的地址和账号密码,这样才能正确连接到数据库。在application.properties配置文件中添加如下配置代码:
spring.datasource.name=newbee-mall-datasource
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?useUnicode=true&serverTimezone=GMT&characterEncoding=utf8&autoReconnect=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
这里编写一个测试类来测试能否连接数据库。
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class NewbeeMallApplicationTests {
// 注入数据源对象
@Autowired
private DataSource defaultDataSource;
@Test
public void datasourceTest() throws SQLException {
// 获取数据库连接对象
Connection connection = defaultDataSource.getConnection();
System.out.print("获取连接:");
// 判断连接对象是否为空
System.out.println(connection != null);
connection.close();
}
@Test
void contextLoads() {
}
}
MyBatis
Spring Boot 整合 MyBatis 过程
添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
application.properties 配置
Spring Boot 整合 MyBatis 时几个比较需要注意的配置参数:
-
mybatis.config-location
配置 mybatis-config.xml 路径,mybatis-config.xml 中配置 MyBatis 基础属性,如果项目中配置了 mybatis-config.xml 文件需要设置该参数
-
mybatis.mapper-locations
配置 Mapper 文件对应的 XML 文件路径
-
mybatis.type-aliases-package
配置项目中实体类包路径
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*Dao.xml
mybatis.type-aliases-package=ltd.newbee.mall.entity
开发时只配置 mapper-locations 即可,最终的 application.properties 文件如下
spring.datasource.name=newbee-mall-datasource
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true&useAffectedRows=true
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
启动类增加 Mapper 扫描
在启动类中添加对 Mapper 包扫描 @MapperScan,Spring Boot 启动的时候会自动加载包路径下的 Mapper 接口:
@SpringBootApplication
@MapperScan("ltd.newbee.mall.dao") //添加 @Mapper 注解
public class NewbeeMallApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}