1逆向工程

1)db.properties

  1. #============================#
  2. #===== Database sttings =====#
  3. #============================#
  4. #jdbc.url=jdbc:mysql://127.0.0.1:3306/wechat?serverTimezone=Asia/Shanghai
  5. jdbc.driver=com.mysql.jdbc.Driver
  6. jdbc.url=jdbc:mysql://localhost:3306/wechat?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
  7. jdbc.username=root
  8. jdbc.password=123

2)generatorConfig.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
  3. <generatorConfiguration>
  4. <!-- 引入配置文件 -->
  5. <properties resource="db.properties"/>
  6. <!-- MyBatis3Simple:不生成 Example相关类及方法-->
  7. <!-- <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
  8. <property name="beginningDelimiter" value="`" />
  9. <property name="endingDelimiter" value="`" /> -->
  10. <!-- 一个数据库一个context -->
  11. <context id="DB2Tables" targetRuntime="MyBatis3">
  12. <commentGenerator>
  13. <property name="suppressAllComments" value="true" />
  14. </commentGenerator>
  15. <!-- 配置数据库连接 -->
  16. <jdbcConnection
  17. driverClass="${jdbc.driver}"
  18. connectionURL="${jdbc.url}"
  19. userId="${jdbc.username}"
  20. password="${jdbc.password}">
  21. </jdbcConnection>
  22. <javaTypeResolver>
  23. <property name="forceBigDecimals" value="false" />
  24. </javaTypeResolver>
  25. <!-- 指定javaBean生成的位置 -->
  26. <javaModelGenerator targetPackage="com.fei.domain"
  27. targetProject=".\src\main\java">
  28. <property name="enableSubPackages" value="true" />
  29. <property name="trimStrings" value="true" />
  30. </javaModelGenerator>
  31. <!--指定sql映射文件生成的位置 -->
  32. <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
  33. <property name="enableSubPackages" value="true" />
  34. </sqlMapGenerator>
  35. <!-- 指定dao接口生成的位置,mapper接口 -->
  36. <javaClientGenerator type="XMLMAPPER"
  37. targetPackage="com.fei.mapper" targetProject=".\src\main\java">
  38. <property name="enableSubPackages" value="true" />
  39. </javaClientGenerator>
  40. <!-- table指定每个表的生成策略 -->
  41. <!-- <table tableName="agent_user_login" domainObjectName="AgentUserLogin"></table> -->
  42. <!-- 配置需要生成的表 -->
  43. <!-- tableName:数据库表名,%代表所有,domainObjectName:生成文件名 ,schema:数据源 -->
  44. <table tableName="%">
  45. <generatedKey column="id" sqlStatement="Mysql" identity="true" />
  46. </table>
  47. </context>
  48. </generatorConfiguration>

3)pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.1.9.RELEASE</version>
  10. <relativePath /> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.fei</groupId>
  13. <artifactId>spring-boot-wechat-login</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>spring-boot-wechat-login</name>
  16. <description>My project for Spring Boot</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  24. </dependency>
  25. <!-- 整合mybatis及分页插件 -->
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.mybatis.spring.boot</groupId>
  32. <artifactId>mybatis-spring-boot-starter</artifactId>
  33. <version>2.1.0</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>com.github.pagehelper</groupId>
  37. <artifactId>pagehelper-spring-boot-starter</artifactId>
  38. <version>1.2.5</version>
  39. </dependency>
  40. <!-- 整合数据源及驱动 -->
  41. <dependency>
  42. <groupId>com.alibaba</groupId>
  43. <artifactId>druid-spring-boot-starter</artifactId>
  44. <version>1.1.10</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>mysql</groupId>
  48. <artifactId>mysql-connector-java</artifactId>
  49. <scope>runtime</scope>
  50. </dependency>
  51. <!-- 热部署工具 -->
  52. <dependency>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-devtools</artifactId>
  55. <scope>runtime</scope>
  56. <optional>true</optional>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.projectlombok</groupId>
  60. <artifactId>lombok</artifactId>
  61. <optional>true</optional>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-starter-test</artifactId>
  66. <scope>test</scope>
  67. </dependency>
  68. </dependencies>
  69. <build>
  70. <plugins>
  71. <plugin>
  72. <groupId>org.springframework.boot</groupId>
  73. <artifactId>spring-boot-maven-plugin</artifactId>
  74. </plugin>
  75. <!-- 逆向工程插件start -->
  76. <plugin>
  77. <groupId>org.mybatis.generator</groupId>
  78. <artifactId>mybatis-generator-maven-plugin</artifactId>
  79. <version>1.3.7</version>
  80. <configuration>
  81. <!-- 指定generatorConfig.xml配置文件位置 -->
  82. <configurationFile>
  83. ${basedir}/src/main/resources/generatorConfig.xml
  84. </configurationFile>
  85. <verbose>true</verbose><!--允许移动生成的文件 -->
  86. <overwrite>true</overwrite><!-- 是否覆盖 -->
  87. </configuration>
  88. <dependencies>
  89. <dependency>
  90. <groupId>mysql</groupId>
  91. <artifactId>mysql-connector-java</artifactId>
  92. <version>5.1.30</version>
  93. </dependency>
  94. </dependencies>
  95. </plugin><!-- 逆向工程插件end -->
  96. </plugins>
  97. </build>
  98. </project>

5)application.properties

  1. # port config
  2. server.port=8081
  3. #============================#
  4. #==== datasource config =====#
  5. #============================#
  6. # can identify automatically
  7. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  8. spring.datasource.url=jdbc:mysql://localhost:3306/wechat?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
  9. spring.datasource.username=root
  10. spring.datasource.password=123
  11. # spring boot use com.zaxxer.hikari.HikariDataSource as default datasource
  12. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

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

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

  1. package com.fei.config;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.PropertySource;
  5. /**
  6. * 配置类
  7. * Created by zxf on 2019年10月19日
  8. */
  9. @Configuration // 表明这是一个配置类,使用@Component注解也可以,但是@Configuration比@Component更细,有语义作用
  10. //告诉配置类从哪个配置文件读取
  11. @PropertySource(value = "classpath:application.properties")
  12. public class AppConfig {
  13. /**
  14. * 公众号appid
  15. */
  16. @Value("${wxpay.appid}")
  17. private String appId;
  18. /**
  19. * 公众号密钥
  20. */
  21. @Value("${wxpay.appsecret}")
  22. private String appsecret;
  23. public String getAppId() {
  24. return appId;
  25. }
  26. public void setAppId(String appId) {
  27. this.appId = appId;
  28. }
  29. public String getAppsecret() {
  30. return appsecret;
  31. }
  32. public void setAppsecret(String appsecret) {
  33. this.appsecret = appsecret;
  34. }
  35. }

2)书写配置文件application.properties

  1. #============================#
  2. #====== wechat config =======#
  3. #============================#
  4. wxpay.appid=wx5beac15ca207cdd40c
  5. wxpay.appsecret=554801238f17fdsdd6f96b382fe548215e9

3)书写测试Controller

  1. package com.fei.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import com.fei.config.AppConfig;
  6. /**
  7. * 用于测试的Controller控制器
  8. * Created by zxf on 2019年10月19日
  9. */
  10. @RestController
  11. public class TestController {
  12. @Autowired
  13. private AppConfig appConfig;
  14. @RequestMapping("/test_config")
  15. public String testConfig() {
  16. System.out.println("appConfig.getAppId():" + appConfig.getAppId());
  17. return appConfig.getAppId();
  18. }
  19. }

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

总结:spring boot整合mybatis步骤

  1. 添加依赖
  1. <!-- 整合mybatis及分页插件 -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>2.1.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.github.pagehelper</groupId>
  9. <artifactId>pagehelper-spring-boot-starter</artifactId>
  10. <version>1.2.5</version>
  11. </dependency>
  12. <!-- 整合数据源及驱动 -->
  13. <dependency>
  14. <groupId>com.alibaba</groupId>
  15. <artifactId>druid-spring-boot-starter</artifactId>
  16. <version>1.1.10</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>mysql</groupId>
  20. <artifactId>mysql-connector-java</artifactId>
  21. <scope>runtime</scope>
  22. </dependency>
  1. 加入配置文件
  1. # port config
  2. server.port=8081
  3. #============================#
  4. #==== datasource config =====#
  5. #============================#
  6. # can identify automatically
  7. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  8. spring.datasource.url=jdbc:mysql://localhost:3306/wechat?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
  9. spring.datasource.username=root
  10. spring.datasource.password=123
  11. # spring boot use com.zaxxer.hikari.HikariDataSource as default datasource
  12. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  1. 启动类增加mapper扫描
  1. package com.fei;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. // mapper接口扫描
  7. @MapperScan("com.fei.mapper")
  8. public class SpringBootWechatLoginApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(SpringBootWechatLoginApplication.class, args);
  11. }
  12. }
  1. 书写mapper接口
  1. import java.util.List;
  2. import org.apache.ibatis.annotations.Param;
  3. import org.apache.ibatis.annotations.Select;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. public interface VideoMapper {
  6. /**
  7. * 查询所有, mapper接口扫描@MapperScan("com.fei.mapper")
  8. *
  9. * @return
  10. */
  11. @Select("select * from video")
  12. List<Video> findAll();
  13. }
  1. 书写测试类
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * 用于测试的Controller控制器
  6. * Created by zxf on 2019年10月19日
  7. */
  8. @RestController
  9. public class TestController {
  10. @Autowired
  11. private VideoMapper videoMapper;
  12. @RequestMapping("/test_db")
  13. public Object testDB() {
  14. return videoMapper.findAll();
  15. }
  16. }

Spring Boot 2.x整合mybatis及druid数据源及逆向工程的更多相关文章

  1. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  2. spring boot 1.4 整合 mybatis druid

    http://www.jianshu.com/p/cef49ad91ba9spring boot 1.4 整合 mybatis druid

  3. SpringBoot 源码解析 (九)----- Spring Boot的核心能力 - 整合Mybatis

    本篇我们在SpringBoot中整合Mybatis这个orm框架,毕竟分析一下其自动配置的源码,我们先来回顾一下以前Spring中是如何整合Mybatis的,大家可以看看我这篇文章Mybaits 源码 ...

  4. 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 ...

  5. spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid

    SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...

  6. Spring Boot学习笔记(六)mybatis配置多数据源

    application.properties #数据库配置 #数据源类型 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # ...

  7. 转-Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合

    Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合 http://blog.csdn.net/balabalayi/article/detai ...

  8. Spring Boot 整合mybatis 使用多数据源

    本人想要实现一个项目里面多个数据库源连接,所以就尝试写一个demo,不多说,先贴结构,再贴代码,可以根据以下的顺序,直接copy解决问题. 首先,dao和resource下的mappers可以用myb ...

  9. 07.深入浅出 Spring Boot - 数据访问之Mybatis(附代码下载)

    MyBatis 在Spring Boot应用非常广,非常强大的一个半自动的ORM框架. 代码下载:https://github.com/Jackson0714/study-spring-boot.gi ...

随机推荐

  1. kaptcha Spring 整合

    转自:http://my.oschina.net/CandyDesire/blog/209364 生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的 ...

  2. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_3 Mybatis的CRUD-修改和删除操作

    增加更新操作 更新用户的配置 测试类 删除的操作 这里的parpameterType值可以是:Integer.INTEGER.int.java.lang.Integer 讲到typeAliases标签 ...

  3. ActionList及Action使用

    ActionList及Action使用 https://blog.csdn.net/adamrao/article/details/7450889 2012年04月11日 19:09:27 阅读数:1 ...

  4. spring(二)

    什么是AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP(面向对 ...

  5. 【Go语言】map在goroutine通信中的使用问题

    简介 本篇文章的主要内容是解决go语言map在使用中遇到的两个问题,对于初学者是不可避免的坑 一.cannot assign to struct field 当map中存在struct类型的成员,如果 ...

  6. xmake v2.2.2, 让C/C++拥有包依赖自动构建

    前言 历经四个多月,xmake终于更新了新版本v2.2.2,并且上线了重量级功能:原生支持的远程依赖包管理. 而这个特性,其实我陆陆续续写了将近一年的时间,才初步完成,对于此特性的开发进展和历史,有兴 ...

  7. 了不起的NodeJS命令行工具

    一个命令行工具实例 这个实例包含了处理进程中的stdin和stdout相关的api,以及文件系统有关的api,使用回调和事件机制来实现并发,主要锻炼基于非阻塞事件的I/O编程中的流控制. // 声明模 ...

  8. 防抖和节流 lodash插件

    lodash.debounce lodash.debounce(function(){ },1000) 函数防抖原理 调用函数时,马上清理定时器.然后再设置一个定时器包含函数

  9. 使用Log4Net将系统日志信息记录到记事本和数据库中

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hxpjava1/article/details/32714855 一.使用Log4Net将日志记录到 ...

  10. 计算机系统结构总结_Memory Review

    这次就边学边总结吧,不等到最后啦 Textbook: <计算机组成与设计——硬件/软件接口>  HI <计算机体系结构——量化研究方法>       QR Ch3. Memor ...