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步骤
- 添加依赖
<!-- 整合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>
- 加入配置文件
# 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
- 启动类增加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);
}
}
- 书写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();
}
- 书写测试类
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();
}
}
Spring Boot 2.x整合mybatis及druid数据源及逆向工程的更多相关文章
- 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源
目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...
- spring boot 1.4 整合 mybatis druid
http://www.jianshu.com/p/cef49ad91ba9spring boot 1.4 整合 mybatis druid
- SpringBoot 源码解析 (九)----- Spring Boot的核心能力 - 整合Mybatis
本篇我们在SpringBoot中整合Mybatis这个orm框架,毕竟分析一下其自动配置的源码,我们先来回顾一下以前Spring中是如何整合Mybatis的,大家可以看看我这篇文章Mybaits 源码 ...
- spring boot:配置shardingsphere(sharding jdbc)使用druid数据源(druid 1.1.23 / sharding-jdbc 4.1.1 / mybatis / spring boot 2.3.3)
一,为什么要使用druid数据源? 1,druid的优点 Druid是阿里巴巴开发的号称为监控而生的数据库连接池 它的优点包括: 可以监控数据库访问性能 SQL执行日志 SQL防火墙 但spring ...
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...
- Spring Boot学习笔记(六)mybatis配置多数据源
application.properties #数据库配置 #数据源类型 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # ...
- 转-Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合
Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合 http://blog.csdn.net/balabalayi/article/detai ...
- Spring Boot 整合mybatis 使用多数据源
本人想要实现一个项目里面多个数据库源连接,所以就尝试写一个demo,不多说,先贴结构,再贴代码,可以根据以下的顺序,直接copy解决问题. 首先,dao和resource下的mappers可以用myb ...
- 07.深入浅出 Spring Boot - 数据访问之Mybatis(附代码下载)
MyBatis 在Spring Boot应用非常广,非常强大的一个半自动的ORM框架. 代码下载:https://github.com/Jackson0714/study-spring-boot.gi ...
随机推荐
- Monkey测试:日志信息分析
在跑monkey时,我们需要将日志输出到文件,然后对日志信息进行分析. 一.输出日志到文件 在monkey命令后加>文件地址 如:adb shell monkey 1000>E:/text ...
- Java中判断两个列表是否相等
CollectionUtils.isEqualCollection(final Collection a, final Collection b) CollectionUtils工具类中有一个查看两个 ...
- Asp .Net Mvc在DeBug模式下设置自定义IP
首先打开所在项目下的.vs文件(查看隐藏文件) 打开config下的applicationhost.config文件 往下拖大概100多行的位置,复制一下binding,然后设置本地ip,如果是设置i ...
- LoadRunner之关联
一.什么是关联 关联就是将服务器动态返回变化的值保存为一个参数以供后面需要用到的地方使用. 二.什么时候需要关联 1.服务器返回中存在动态变化的值,一般是类似session.token这样的无规则数据 ...
- Spring获取request、session以及servletContext
获取request: HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestA ...
- docker windows下挂载目录和文件
我们利用docker启动项目的时候不能直接修改容器中的内容,只能在 run 的时候挂载到本地目录或者文件来进行修改. 例子:(路径可以忽略斜杠和反斜杠,我这边使用windows的路径没有报错.do ...
- CSS3—— 多列 用户界面 图片 按钮
多列 将文本内容设计成像报纸一样的多列布局 多列创建 间隙 列边框 边框颜色+宽度 指定列的宽度 指定元素跨越多少列 用户界面 由用户调整元素大小[谷歌浏览器等] 以确切的方式定义适应某个区域的具体内 ...
- 关于WordPress中字体加载慢的问题解决方案(转)
2016-04-15 最近发现Wordpress有时候加载的特别慢,于是就想办法找了下原因.之前听网上说是因为wordpress用的是Google的字体库,而且是每次都要加载,导致访问慢的,于是当时装 ...
- LeetCode算法题-Maximize Distance to Closest Person(Java实现)
这是悦乐书的第328次更新,第351篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第198题(顺位题号是849).在一排座位中,1表示一个人坐在该座位上,0表示座位是空的 ...
- xmake新增对WDK驱动编译环境支持
xmake v2.2.1新版本现已支持WDK驱动编译环境,我们可以直接在系统原生cmd终端下,执行xmake进行驱动编译,甚至配合vscode, sublime text, IDEA等编辑器+xmak ...