Spring Boot 2.x整合mybatis及druid数据源及逆向工程

1逆向工程

1)db.properties

#============================#
#===== Database sttings =====#
#============================#
#jdbc.url=jdbc:mysql://127.0.0.1:3306/wechat?serverTimezone=Asia/Shanghai

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wechat?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123

2)generatorConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >  
<generatorConfiguration>  
    <!-- 引入配置文件 -->  
    <properties resource="db.properties"/>  
    
    <!-- MyBatis3Simple:不生成 Example相关类及方法-->
	<!-- <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
		<property name="beginningDelimiter" value="`" />
		<property name="endingDelimiter" value="`" />  -->
      
    <!-- 一个数据库一个context -->  
    <context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		
		<!-- 配置数据库连接 -->
		<jdbcConnection 
			driverClass="${jdbc.driver}"
			connectionURL="${jdbc.url}" 
			userId="${jdbc.username}"
			password="${jdbc.password}">
		</jdbcConnection>
 
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
 
		<!-- 指定javaBean生成的位置 -->
		<javaModelGenerator targetPackage="com.fei.domain"
			targetProject=".\src\main\java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
 
		<!--指定sql映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
 
		<!-- 指定dao接口生成的位置,mapper接口 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.fei.mapper" targetProject=".\src\main\java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
 
 
		<!-- table指定每个表的生成策略 -->
		<!-- <table tableName="agent_user_login" domainObjectName="AgentUserLogin"></table> -->
		
		<!-- 配置需要生成的表 -->
		<!-- tableName:数据库表名,%代表所有,domainObjectName:生成文件名 ,schema:数据源 -->
		<table tableName="%">
			<generatedKey column="id" sqlStatement="Mysql" identity="true" />
		</table>
		
	</context> 
</generatorConfiguration> 

3)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.1.9.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.fei</groupId>
	<artifactId>spring-boot-wechat-login</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-wechat-login</name>
	<description>My project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

		<!-- 整合mybatis及分页插件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.0</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.5</version>
		</dependency>

		<!-- 整合数据源及驱动 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!-- 热部署工具 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</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>
			</plugin>

			<!-- 逆向工程插件start -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.7</version>
				<configuration>
					<!-- 指定generatorConfig.xml配置文件位置 -->
					<configurationFile>
						${basedir}/src/main/resources/generatorConfig.xml
					</configurationFile>
					<verbose>true</verbose><!--允许移动生成的文件 -->
					<overwrite>true</overwrite><!-- 是否覆盖 -->
				</configuration>

				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.30</version>
					</dependency>
				</dependencies>
			</plugin><!-- 逆向工程插件end -->

		</plugins>
	</build>

</project>

5)application.properties

# port config
server.port=8081

#============================#
#==== datasource config =====#
#============================#
# can identify automatically
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/wechat?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123

# spring boot use com.zaxxer.hikari.HikariDataSource as default datasource 
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

2接口配置文件自动映射到属性和实体类配置

1)书写配置类,并为相应属性生成set、get方法

package com.fei.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 配置类 
 * Created by zxf on 2019年10月19日
 */
@Configuration // 表明这是一个配置类,使用@Component注解也可以,但是@Configuration比@Component更细,有语义作用
//告诉配置类从哪个配置文件读取
@PropertySource(value = "classpath:application.properties")
public class AppConfig {

	/**
	 * 公众号appid
	 */
	@Value("${wxpay.appid}")
	private String appId;
	
	/**
	 * 公众号密钥
	 */
	@Value("${wxpay.appsecret}")
	private String appsecret;

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getAppsecret() {
		return appsecret;
	}

	public void setAppsecret(String appsecret) {
		this.appsecret = appsecret;
	}
	
}

2)书写配置文件application.properties

#============================#
#====== wechat config =======#
#============================#
wxpay.appid=wx5beac15ca207cdd40c
wxpay.appsecret=554801238f17fdsdd6f96b382fe548215e9

3)书写测试Controller

package com.fei.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fei.config.AppConfig;

/**
 * 用于测试的Controller控制器 
 * Created by zxf on 2019年10月19日
 */
@RestController
public class TestController {
	
	@Autowired
	private AppConfig appConfig;

	@RequestMapping("/test_config")
	public String testConfig() {
		System.out.println("appConfig.getAppId():" + appConfig.getAppId());
		return appConfig.getAppId();
	}

}

4)打开浏览器访问http://localhost:8080/test_config测试

总结:spring boot整合mybatis步骤

  1. 添加依赖
<!-- 整合mybatis及分页插件 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

<!-- 整合数据源及驱动 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
  1. 加入配置文件
# port config
server.port=8081

#============================#
#==== datasource config =====#
#============================#
# can identify automatically
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/wechat?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123

# spring boot use com.zaxxer.hikari.HikariDataSource as default datasource 
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  1. 启动类增加mapper扫描
package com.fei;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
// mapper接口扫描
@MapperScan("com.fei.mapper")
public class SpringBootWechatLoginApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootWechatLoginApplication.class, args);
	}

}
  1. 书写mapper接口
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;

public interface VideoMapper {
	/**
	 * 查询所有, mapper接口扫描@MapperScan("com.fei.mapper")
	 * 
	 * @return
	 */
	@Select("select * from video")
	List<Video> findAll();
}
  1. 书写测试类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用于测试的Controller控制器 
 * Created by zxf on 2019年10月19日
 */
@RestController
public class TestController {
		
	@Autowired
	private VideoMapper videoMapper;
	
	@RequestMapping("/test_db")
	public Object testDB() {
		return videoMapper.findAll();
	}

}
posted on 2019-10-19 21:21  行之间  阅读(478)  评论(0编辑  收藏  举报