在我们的日常开发中,数据库操作最多的就是通过Mybatis做ORM框架,Druid做数据源连接工具。之前在工作中,实际使用SpringBoot开发的场景并不是很多,以至于在这方面的整合没有积累经验。鉴于最近自己的项目需要支持SpringBoot,故借此机会,将这两个框架和SpringBoot做一个快速整合。
废话不多说,开始上代码~
1. 操作步骤
1.1 环境准备
- Spring Boot版本:2.1.5.RELEASE
- 数据库:Mysql
1.2 初始化Spring Boot工程
初始化Spring Boot工程的方式有多种,可以通过 https://start.spring.io/ 或者 idea 创建;因为是一个整合教程,我这次选择纯手工创建一个Spring Boot工程。
整体工程是标准的Maven结构,如下:
lizard-data-admin
|-src
|-main
|-java
|-io.lizardframework.data.admin
|-configuration
|-DataSourceConfiguration.java
|-LizardDataAdminApplication.java
|-resources
|-mapper
|-LizardDataServiceEntityMapper.xml
|-application.properties
|-log4j2.xml
|-test
|-pom.xml
1.3 pom文件详解
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- 查找顺序:relativePath元素中的地址–本地仓库–远程仓库 设定一个空值将始终从仓库中获取,不从本地路径获取 -->
</parent>
<groupId>io.lizardframework.data.admin</groupId>
<artifactId>lizard-data-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lizard-data-admin</name>
<description>Lizard Data Project Admin</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- webmvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Databse -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>