上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源。SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,SpringBoot就会基于约定根据配置信息实例化对象,但是一般大型的项目都是有多个子系统或者多个数据源组成,那怎么使用SpringBoot进行Mybatis多数据源配置呢?

一、数据库准备

我们这里准备使用主从两个数据库来进行演示多数据源配置。一个主库用来写write,一个从库用来读read.至于两个数据库的数据同步问题这里暂时不考虑。两个数据库只是数据库名不一样,主库为mybatis1,从库为mybatis,表结构是一样的。

主库(write):

  1. CREATE DATABASE `mybatis1` /*!40100 DEFAULT CHARACTER SET utf8 */;
  2. CREATE TABLE `user` (
  3. `id` int(11) NOT NULL AUTO_INCREMENT,
  4. `name` varchar(20) DEFAULT NULL,
  5. `age` int(11) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;

从库(read):

  1. CREATE DATABASE `mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;
  2. CREATE TABLE `user` (
  3. `id` int(11) NOT NULL AUTO_INCREMENT,
  4. `name` varchar(20) DEFAULT NULL,
  5. `age` int(11) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;

二、引入依赖

这里主要引入mysql数据库、mybatis架构、Druid相关的SpringBoot依赖。下面的是由于要使用jsp显示内容所以也假如了jsp相关的依赖。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.example</groupId>
  7. <artifactId>demo</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>demo</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>2.0.1.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25.  
  26. </properties>
  27.  
  28. <dependencies>
  29.  
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-web</artifactId>
  33. </dependency>
  34.  
  35. <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5
  36. <dependency>
  37. <groupId>org.thymeleaf</groupId>
  38. <artifactId>thymeleaf-spring5</artifactId>
  39. <version>3.0.9.RELEASE</version>
  40. </dependency>
  41. -->
  42.  
  43. <dependency>
  44. <groupId>org.apache.tomcat.embed</groupId>
  45. <artifactId>tomcat-embed-jasper</artifactId>
  46. <scope>provided</scope>
  47. </dependency>
  48. <dependency>
  49. <groupId>javax.servlet</groupId>
  50. <artifactId>jstl</artifactId>
  51. <scope>provided</scope>
  52. </dependency>
  53. <dependency>
  54. <groupId>javax.servlet</groupId>
  55. <artifactId>javax.servlet-api</artifactId>
  56. <scope>provided</scope>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-starter-test</artifactId>
  61. <scope>test</scope>
  62. </dependency>
  63. <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
  64. <dependency>
  65. <groupId>org.mybatis.spring.boot</groupId>
  66. <artifactId>mybatis-spring-boot-starter</artifactId>
  67. <version>1.3.2</version>
  68. </dependency>
  69. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  70. <dependency>
  71. <groupId>mysql</groupId>
  72. <artifactId>mysql-connector-java</artifactId>
  73. <version>8.0.11</version>
  74. </dependency>
  75.  
  76. <dependency>
  77. <groupId>com.alibaba</groupId>
  78. <artifactId>druid-spring-boot-starter</artifactId>
  79. <version>1.1.10</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>org.springframework.boot</groupId>
  83. <artifactId>spring-boot-configuration-processor</artifactId>
  84. <optional>true</optional>
  85. </dependency>
  86. </dependencies>
  87.  
  88. <build>
  89. <plugins>
  90. <plugin>
  91. <groupId>org.springframework.boot</groupId>
  92. <artifactId>spring-boot-maven-plugin</artifactId>
  93. </plugin>
  94. </plugins>
  95. </build>
  96.  
  97. </project>

三、创建Mapper

从这里开始就比较坑了,为了写这篇博客昨天搞到夜里两点中,Druid官方介绍的比较少,demo也不是与mybatis相结合,就倒置怎么把mapper与数据源配置对照上也是问题,因为默认单数据源的话,配置下数据源信息以及Mapper就好,但是如果是多数据源那就要手动指定数据源在哪里,怎么和Mapper对照上。

这里先创建两个Mappe,一个是写的一个是读的。这里要注意的地方是要加上@Mapper注解。

ReadUserMapper:

  1. package com.example.read.mapper;
  2. import java.util.List;
  3. import com.example.model.User;
  4.  
  5. import org.apache.ibatis.annotations.Delete;
  6. import org.apache.ibatis.annotations.Insert;
  7. import org.apache.ibatis.annotations.Mapper;
  8. import org.apache.ibatis.annotations.Result;
  9. import org.apache.ibatis.annotations.Results;
  10. import org.apache.ibatis.annotations.Select;
  11. import org.apache.ibatis.annotations.Update;
  12.  
  13. @Mapper
  14. public interface ReadUserMapper {
  15.  
  16. @Select("SELECT name FROM user")
  17.  
  18. @Results({
  19.  
  20. @Result(property = "Name", column = "name")
  21.  
  22. })
  23.  
  24. List<User> getAll();
  25.  
  26. @Select("SELECT name FROM user WHERE id = #{id}")
  27.  
  28. @Results({
  29.  
  30. @Result(property = "Name", column = "name")
  31.  
  32. })
  33.  
  34. User getOne(int id);
  35.  
  36. @Insert("INSERT INTO user(name,age) VALUES(#{name}, #{age})")
  37.  
  38. void insert(User user);
  39.  
  40. @Update("UPDATE user SET name=#{name},age=#{age} WHERE id =#{id}")
  41.  
  42. void update(User user);
  43.  
  44. @Delete("DELETE FROM user WHERE id =#{id}")
  45.  
  46. void delete(int id);
  47. }

WriteUserMapper:

  1. package com.example.write.mapper;
  2. import java.util.List;
  3.  
  4. import org.apache.ibatis.annotations.Delete;
  5. import org.apache.ibatis.annotations.Insert;
  6. import org.apache.ibatis.annotations.Mapper;
  7. import org.apache.ibatis.annotations.Result;
  8. import org.apache.ibatis.annotations.Results;
  9. import org.apache.ibatis.annotations.Select;
  10. import org.apache.ibatis.annotations.Update;
  11.  
  12. import com.example.model.*;
  13.  
  14. @Mapper
  15. public interface WriteUserMapper {
  16.  
  17. @Select("SELECT name FROM user")
  18.  
  19. @Results({
  20.  
  21. @Result(property = "Name", column = "name")
  22.  
  23. })
  24.  
  25. List<User> getAll();
  26.  
  27. @Select("SELECT name FROM user WHERE id = #{id}")
  28.  
  29. @Results({
  30.  
  31. @Result(property = "Name", column = "name")
  32.  
  33. })
  34.  
  35. User getOne(int id);
  36.  
  37. @Insert("INSERT INTO user(name,age) VALUES(#{name}, #{age})")
  38.  
  39. void insert(User user);
  40.  
  41. @Update("UPDATE user SET name=#{name},age=#{age} WHERE id =#{id}")
  42.  
  43. void update(User user);
  44.  
  45. @Delete("DELETE FROM user WHERE id =#{id}")
  46.  
  47. void delete(int id);
  48. }

四、配置数据源

如果使用SpringBoot默认配置类,可以直接在application.properties中配置就好了,它会自动扫描mapper类与数据源进行关联,但是如果是多个数据源的话,那就需要进行手动配置。这里分别创建了读DataSourceReadConfig、写DataSourceWriteConfig数据源配置类。

DataSourceWriteConfig:

  1. package com.example.config;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.mybatis.spring.SqlSessionFactoryBean;
  7. import org.mybatis.spring.SqlSessionTemplate;
  8. import org.mybatis.spring.annotation.MapperScan;
  9. import org.springframework.beans.factory.annotation.Qualifier;
  10. import org.springframework.boot.context.properties.ConfigurationProperties;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.context.annotation.Primary;
  14. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  15. import org.springframework.stereotype.Component;
  16.  
  17. import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
  18.  
  19. @Configuration
  20. @MapperScan(basePackages = "com.example.write.mapper", sqlSessionTemplateRef = "writeSqlSessionTemplate")
  21. public class DataSourceWriteConfig {
  22. @Bean(name = "writeDataSource")
  23. @ConfigurationProperties(prefix = "spring.datasource.druid.write")
  24. @Qualifier("writeDataSource")
  25. @Primary
  26. public DataSource writeDataSource() {
  27. return DruidDataSourceBuilder.create().build();
  28. }
  29.  
  30. @Bean(name = "writeSqlSessionFactory")
  31. @Primary
  32. public SqlSessionFactory writeSqlSessionFactory(@Qualifier("writeDataSource") DataSource dataSource) throws Exception {
  33. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  34. bean.setDataSource(dataSource);
  35. return bean.getObject();
  36. }
  37.  
  38. @Bean(name = "writeTransactionManager")
  39. @Primary
  40. public DataSourceTransactionManager writeTransactionManager(@Qualifier("writeDataSource") DataSource dataSource) {
  41. return new DataSourceTransactionManager(dataSource);
  42. }
  43.  
  44. @Bean(name = "writeSqlSessionTemplate")
  45. @Primary
  46. public SqlSessionTemplate writeSqlSessionTemplate(@Qualifier("writeSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  47. return new SqlSessionTemplate(sqlSessionFactory);
  48. }
  49. }

DataSourceReadConfig:

  1. package com.example.config;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.mybatis.spring.SqlSessionFactoryBean;
  7. import org.mybatis.spring.SqlSessionTemplate;
  8. import org.mybatis.spring.annotation.MapperScan;
  9. import org.springframework.beans.factory.annotation.Qualifier;
  10. import org.springframework.boot.context.properties.ConfigurationProperties;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  14. import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
  15.  
  16. @Configuration
  17. @MapperScan(basePackages = "com.example.read.mapper", sqlSessionTemplateRef = "readSqlSessionTemplate")
  18. public class DataSourceReadConfig {
  19. @Bean(name = "readDataSource")
  20. @ConfigurationProperties(prefix = "spring.datasource.druid.read")
  21. @Qualifier("readDataSource")
  22. public DataSource readDataSource() {
  23. return DruidDataSourceBuilder.create().build();
  24. }
  25.  
  26. @Bean(name = "readSqlSessionFactory")
  27.  
  28. public SqlSessionFactory readSqlSessionFactory(@Qualifier("readDataSource") DataSource dataSource) throws Exception {
  29. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  30. bean.setDataSource((javax.sql.DataSource) dataSource);
  31. return bean.getObject();
  32. }
  33.  
  34. @Bean(name = "readTransactionManager")
  35.  
  36. public DataSourceTransactionManager readTransactionManager(@Qualifier("readDataSource") DataSource dataSource) {
  37. return new DataSourceTransactionManager(dataSource);
  38. }
  39.  
  40. @Bean(name = "readSqlSessionTemplate")
  41.  
  42. public SqlSessionTemplate readSqlSessionTemplate(@Qualifier("readSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  43. return new SqlSessionTemplate(sqlSessionFactory);
  44. }
  45. }

这部分是遇到坑最多的地方,由于Druid官方github上并没有具体参考的demo,看其他的博客又与gitgub介绍的有出入,比如获取配置类中获取DataSource方法中,其他我看使用的是DataSourceBuilder,但Druid GitHub上的是DruidDataSourceBuilder,不知道是不是版本的问题,如果使用DataSourceBuilder,配置多数据库时不起作用。github上也有这句话:Spring Boot 2.X 版本不再支持配置继承,多数据源的话每个数据源的所有配置都需要单独配置,否则配置不会生效。还有就是DataSource引入的包名,我开始引入的并不是import javax.sql.DataSource;这个也是一个坑。

五、Druid多数据源配置

这里也遇到了坑,由于在配置数据源类中并未使用DruidDataSourceBuilder,而是使用的DataSourceBuilder,这就导致下面配置的没用,而且在设置数据库url还报错,需要使用jdbc-url.

  1. spring.mvc.view.prefix=/view/
  2.  
  3. spring.mvc.view.suffix=.jsp
  4. mybatis.type-aliases-package=com.example.model
  5. #mybatis.config-location=classpath:mybatis/mybatis-config.xml
  6. #mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
  7.  
  8. spring.datasource.druid.read.web-stat-filter.enabled=true
  9. spring.datasource.druid.read.web-stat-filter.url-pattern=/*
  10. spring.datasource.druid.read.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
  11. spring.datasource.druid.read.web-stat-filter.session-stat-enable=true
  12. spring.datasource.druid.read.web-stat-filter.session-stat-max-count=1000
  13. spring.datasource.druid.read.stat-view-servlet.enabled= true
  14. spring.datasource.druid.read.stat-view-servlet.url-pattern=/druid/*
  15. spring.datasource.druid.read.stat-view-servlet.reset-enable=true
  16. spring.datasource.druid.read.stat-view-servlet.login-username=druid
  17. spring.datasource.druid.read.stat-view-servlet.login-password=123456
  18. spring.datasource.druid.read.stat-view-servlet.allow=127.0.0.1
  19. spring.datasource.druid.read.stat-view-servlet.deny=192.168.0.19
  20. spring.datasource.druid.read.aop-patterns=com.example.read.mapper.*
  21.  
  22. spring.datasource.druid.read.url =jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  23. spring.datasource.druid.read.username = root
  24. spring.datasource.druid.read.password = 123456
  25. spring.datasource.druid.read.driver-class-name=com.mysql.cj.jdbc.Driver
  26. spring.datasource.druid.read.type=com.alibaba.druid.pool.DruidDataSource
  27.  
  28. spring.datasource.druid.write.max-active=20
  29. spring.datasource.druid.write.initial-size=1
  30. spring.datasource.druid.write.max-wait=60000
  31. spring.datasource.druid.write.pool-prepared-statements=true
  32. spring.datasource.druid.write.max-pool-prepared-statement-per-connection-size=20
  33. spring.datasource.druid.write.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  34. spring.datasource.druid.write.min-idle=1
  35. spring.datasource.druid.write.time-between-eviction-runs-millis=60000
  36. spring.datasource.druid.write.min-evictable-idle-time-millis=300000
  37. spring.datasource.druid.write.validation-query=select 1 from dual
  38. spring.datasource.druid.write.test-while-idle=true
  39. spring.datasource.druid.write.test-on-borrow=true
  40. spring.datasource.druid.write.test-on-return=true
  41.  
  42. spring.datasource.druid.write.web-stat-filter.enabled=true
  43. spring.datasource.druid.write.web-stat-filter.url-pattern=/*
  44. spring.datasource.druid.write.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
  45. spring.datasource.druid.write.web-stat-filter.session-stat-enable=true
  46. spring.datasource.druid.write.web-stat-filter.session-stat-max-count=1000
  47. spring.datasource.druid.write.stat-view-servlet.enabled= true
  48. spring.datasource.druid.write.stat-view-servlet.url-pattern=/druid/*
  49. spring.datasource.druid.write.stat-view-servlet.reset-enable=true
  50. spring.datasource.druid.write.stat-view-servlet.login-username=druid
  51. spring.datasource.druid.write.stat-view-servlet.login-password=123456
  52. spring.datasource.druid.write.stat-view-servlet.allow=127.0.0.1
  53. spring.datasource.druid.write.stat-view-servlet.deny=192.168.0.19
  54. spring.datasource.druid.write.url =jdbc:mysql://127.0.0.1:3306/mybatis1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  55. spring.datasource.druid.write.aop-patterns=com.example.write.mapper.*
  56. spring.datasource.druid.write.username = root
  57. spring.datasource.druid.write.password = 123456
  58. spring.datasource.druid.write.driver-class-name=com.mysql.cj.jdbc.Driver
  59. spring.datasource.druid.write.type=com.alibaba.druid.pool.DruidDataSource

六、多数据源的使用

这里并未设置Service层,而是直接在Controller中使用。在Controller中会装配一个写的mapper一个读的mapper,分别进行查询和新增操作。

  1. package com.example.demo;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10.  
  11. import com.example.model.User;
  12.  
  13. import com.example.read.mapper.ReadUserMapper;
  14. import com.example.write.mapper.WriteUserMapper;
  15.  
  16. @Controller
  17. @RequestMapping("/user")
  18. public class UserController {
  19.  
  20. @Autowired
  21. private WriteUserMapper userMapperWrite;
  22.  
  23. @Autowired
  24. private ReadUserMapper userMapperRead;
  25.  
  26. @RequestMapping(value = "/alluser.do",method = RequestMethod.GET)
  27. public String getallusers(Model model) {
  28. List<User> users=userMapperRead.getAll();
  29. model.addAttribute("users", users);
  30. return "userlist";
  31. }
  32. @RequestMapping(value = "/insert.do",method = RequestMethod.GET)
  33. public String adduser(Model model) {
  34. User user=new User();
  35. user.setName("cuiyw");
  36. user.setAge(27);
  37. userMapperWrite.insert(user);
  38. List<User> users=userMapperWrite.getAll();
  39. model.addAttribute("users", users);
  40. return "userlist";
  41. }
  42. }

七、指定数据源配置文件位置

上面基本把配置信息都配置好了,但是如果这样运行还是会报错误,它还是不能找到这个mapper,此时需要在main方法文件增加注解@ComponentScan(basePackages={"com.example.config","com.example.demo"}),让它扫描配置文件的包,然后在配置文件的包里面有配置@MapperScan来查找到mapper。

  1. package com.example.demo;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. //import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. //import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  7. import org.springframework.context.annotation.ComponentScan;
  8. //@EnableAutoConfiguration(exclude= {DataSourceAutoConfiguration.class})
  9.  
  10. @ComponentScan(basePackages={"com.example.config","com.example.demo"})
  11. @SpringBootApplication
  12. public class DemoApplication {
  13.  
  14. public static void main(String[] args) {
  15. SpringApplication.run(DemoApplication.class, args);
  16. }
  17. }
  1. Description:
  2.  
  3. Field userMapperWrite in com.example.demo.UserController required a bean of type 'com.example.write.mapper.WriteUserMapper' that could not be found.
  4.  
  5. Action:
  6. Consider defining a bean of type 'com.example.write.mapper.WriteUserMapper' in your configuration.

八、其他问题

1.这里还遇到404找不到路径的错误,这里还需要在@ComponentScan注解加上Controller对应的包,所以上面代码有@ComponentScan(basePackages={"com.example.config","com.example.demo"})。

  1. This application has no explicit mapping for /error, so you are seeing this as a fallback.
  2. Sun Jul 22 23:58:27 CST 2018
  3. There was an unexpected error (type=Not Found, status=404).
  4. No message available

2.设置手动配置问题

由于开始使用的是DataSourceBuilder,但在application.properties还是使用spring.datasource.druid.read这种方法进行配置,并没spring.datasource.url这样配置,导致报下面的错误。因为使用的是DataSourceBuilder所以SpringBoot还是认为用的默认配置,所以就找spring.datasource.url,此时可以使用@EnableAutoConfiguration(exclude= {DataSourceAutoConfiguration.class})注解来设置手动注解。

九、测试

这里还是分别输入http://localhost:8080/user/alluser.do,http://localhost:8080/user/insert.do,然后查看两个数据库user表的数据是否有没有改变,读数据库数据未变,写数据库数据增加。Druid的数据源监测也是有两条数据源信息。

SpringBoot入门之基于Druid配置Mybatis多数据源的更多相关文章

  1. SpringBoot入门之基于XML的Mybatis

    上一博客介绍了下SpringBoot基于注解引入Mybatis,今天介绍基于XML引入Mybatis.还是在上一篇demo的基础上进行修改. 一.Maven引入 这个与上一篇的一样,需要引入mybat ...

  2. SpringBoot入门之基于注解的Mybatis

    今天学习下SpringBoot集成mybatis,集成mybatis一般有两种方式,一个是基于注解的一个是基于xml配置的.今天先了解下基于注解的mybatis集成. 一.引入依赖项 因为是mybat ...

  3. (一)SpringBoot入门【基于2.x版本】

    SpringBoot入门[基于2.x版本] 一.SpringBoot简介 首先大家学习SpringBoot的话,我希望大家是有一定java基础的,如果是有Spring的基础的话,上手会更加得心应手,因 ...

  4. SpringBoot系列之集成Druid配置数据源监控

    SpringBoot系列之集成Druid配置数据源监控 继上一篇博客SpringBoot系列之JDBC数据访问之后,本博客再介绍数据库连接池框架Druid的使用 实验环境准备: Maven Intel ...

  5. SpringBoot入门之集成Druid

    Druid:为监控而生的数据库连接池.这篇先了解下它的简单使用,下篇尝试用它做多数据源配置.主要参考:https://github.com/alibaba/druid/wiki/常见问题 https: ...

  6. SpringBoot入门 (六) 数据库访问之Mybatis

    本文记录学习在SpringBoot中使用Mybatis. 一 什么是Mybatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 ...

  7. SpringBoot入门之内嵌Tomcat配置

    spring boot默认web程序启用tomcat内嵌容器tomcat,监听8080端口,servletPath默认为 / .需要用到的就是端口.上下文路径的修改,在spring boot中其修改方 ...

  8. Springboot入门2-配置druid

    Druid是Java语言中最好的数据库连接池,在连接池之外,还提供了非常优秀的监控功能. 下面来说明如何在 Spring Boot 中配置使用Druid 1.添加Maven依赖 (或jar包) < ...

  9. SpringBoot入门-多环境文件配置(二)

    pom.xml <name>springboot-application</name> <description>A project for Spring Boot ...

随机推荐

  1. 【轻松前端之旅】CSS盒子模型

    盒子模型,也叫框模型,在CSS里是很重要的概念. 每个元素都可以看做一个盒子.盒子包含四个部分:外边距(margin).边框(border).内边距(padding).元素内容(element con ...

  2. 2019.02.21 bzo1038: [ZJOI2008]瞭望塔(半平面交)

    传送门 题意:给出一个nnn个点的轮廓,要求找一个高度最小的点使得它能够看见所有拐点. 思路:之间建半平面交然后取半平面交上的每个交点和每个轮廓更新答案即可. 代码: #include<bits ...

  3. MFC源码解读(一)最原始一个MFC程序,手写不用向导

    从这一篇开始,详细记录一下MFC的源码解读 四个文件,分别为: stdafx.h,stdafx.cpp,hello.h,hello.cpp 代码如下: //stdafx.h #include < ...

  4. php中 curl, fsockopen ,file_get_contents 三个函数

    赵永斌:有些时候用file_get_contents()调用外部文件,容易超时报错.换成curl后就可以.具体原因不清楚curl 效率比file_get_contents()和fsockopen()高 ...

  5. less的功能和介绍

    在全新的css中,经过程序员们的开发和努力.又打造了全新的less样式表的全新界面,只需引入外部样式表即刻套用,加上了函数定义和取值.大大的降低了代码的书写量.也更加方便程序员的调用和修改!

  6. 分享Azure DevOps技术,来微信群吧!

    现在QQ用户越来越少,基本上都转移到微信上了. 讨论问题,动不动就来一个微信群.下面这样几百人的微信群,专门讨论Azure DevOps (TFS)技术,你加入了么? 还等什么,扫描吧!

  7. Input and Output File

    Notes from C++ Primer File State Condition state is used to manage stream state, which indicates if ...

  8. stm32驱动12832液晶屏程序(ST7565R控制器)

    LCD12832.c文件: #include"stm32f10x_lib.h" #include "OCM12232.h" void Lcd12232delay ...

  9. WIN10下Prolific USB-to-Serial Comm Port驱动

    最近在安装Prlific的时候,通过电脑自动安装启动后,发现系统无法识别,如下图所示: 还以为是驱动比较老,没有及时更新导致的,去官网下载最新的驱动,发现了这个列表: 这个驱动不支持win10. 后来 ...

  10. Java 8 停止维护,Java 9 难产,IDEA 2018 发布,还有……

    祝大家五一劳动节快乐,工作顺利! 又到了总结上个月干货的时候了,这个月我们带来了各种Java技术干货,各种送书抽奖福利,各种面试题分享,各种最新动态资讯等. 5.1重磅活动 | 区块链免费送书 &am ...