数据库连接池配置
# 数据源配置
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.cj.jdbc.Driverdruid:# 主库数据源master:url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: password# 从库数据源slave:# 从数据源开关/默认关闭enabled: falseurl: username: password: # 初始连接数initialSize: 5# 最小连接池数量minIdle: 10# 最大连接池数量maxActive: 20# 配置获取连接等待超时的时间maxWait: 60000# 配置连接超时时间connectTimeout: 30000# 配置网络超时时间socketTimeout: 60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒minEvictableIdleTimeMillis: 300000# 配置一个连接在池中最大生存的时间,单位是毫秒maxEvictableIdleTimeMillis: 900000# 配置检测连接是否有效validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsewebStatFilter: enabled: truestatViewServlet:enabled: true# 设置白名单,不填则允许所有访问allow:url-pattern: /druid/*# 控制台管理用户名和密码login-username: ruoyilogin-password: 123456filter:stat:enabled: true# 慢SQL记录log-slow-sql: trueslow-sql-millis: 1000merge-sql: truewall:config:multi-statement-allow: true
SpringBoot生成、读取二维码
生成读取二维码依赖
<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version>
</dependency>
<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.3</version>
</dependency>
代码示例
package com.demo.demo.QRcode;import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;@Controller
public class QRCodeController {@RequestMapping("QRCode")@ResponseBodypublic String QRCode(HttpServletRequest req){final int width = 300;final int height = 300;final String format = "png";final String content = "我爱你,中国!!!";//定义二维码的参数HashMap hints = new HashMap();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 指定编码格式hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 指定纠错等级hints.put(EncodeHintType.MARGIN, 2);//设置白边//生成二维码try{//编码的内容,编码的方式(二维码、条形码...),首选的宽度,首选的高度,编码时的额外参数BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);Path file = new File("F:\\ideaWorkSpace\\demo\\src\\main\\resources\\static\\img.png").toPath();MatrixToImageWriter.writeToPath(bitMatrix, format, file);}catch(Exception e){}return "success";}@RequestMapping("getQRCode")@ResponseBodypublic String getQRCode() throws Exception {MultiFormatReader formatReader = new MultiFormatReader();File file = new File("F:\\ideaWorkSpace\\demo\\src\\main\\resources\\static\\img.png");BufferedImage image = ImageIO.read(file);BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));//定义二维码的参数HashMap hints = new HashMap();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 指定编码格式Result result = formatReader.decode(binaryBitmap, hints);System.out.println("二维码解析结果:" + result.toString());System.out.println("二维码的格式:" + result.getBarcodeFormat());System.out.println("二维码的文本内容:" + result.getText());return result.getText();}
}
SpringBoot集成多数据源
依赖先行、pom.xml
代码结构
配置项
server:port:3666
spring:application:name: multiple-datadatasource:show-sql: falsedb1:jdbc-url: jdbc:mysql://ip地址:端口/数据库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=trueusername: 数据库账号password: 数据库密码driver-class-name: com.mysql.cj.jdbc.Driverdb2:jdbc-url: jdbc:postgresql://ip地址:端口/数据库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=trueusername: 数据库账号password: 数据库密码driver-class-name: org.postgresql.Driverdb3:jdbc-url: jdbc:sqlserver://ip地址:端口;databaseName=数据库名username: 数据库账号password: 数据库密码driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
配置类
数据源1的配置类
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.zkgl.dao.db1",sqlSessionFactoryRef = "db1SqlSessionFactory")
public class MysqlDataSourceConfig {static final String MAPPER_LOCATION = "classpath:/mapper/db1/*.xml";@Bean("db1DataSource")@ConfigurationProperties(prefix = "spring.datasource.db1")public DataSource getDb1DataSource(){return DataSourceBuilder.create().build();}@Bean("db1SqlSessionFactory")public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));return bean.getObject();}@Bean("db1SqlSessionTemplate")public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}
}
数据源2的配置类
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.zkgl.dao.db2",sqlSessionFactoryRef = "db2SqlSessionFactory")
public class PgDataSourceConfig {static final String MAPPER_LOCATION = "classpath:/mapper/db2/*.xml";@Bean("db2DataSource")@ConfigurationProperties(prefix = "spring.datasource.db2")public DataSource getDb2DataSource(){return DataSourceBuilder.create().build();}@Bean("db2SqlSessionFactory")public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));return bean.getObject();}@Bean("db2SqlSessionTemplate")public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}
}
数据源3的配置类
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;@Configuration
@MapperScan(basePackages = "com.zkgl.dao.db3",sqlSessionFactoryRef = "db3SqlSessionFactory")
public class SqlServerDataSourceConfig {static final String MAPPER_LOCATION = "classpath:/mapper/db3/*.xml";@Bean("db3DataSource")@ConfigurationProperties(prefix = "spring.datasource.db3")public DataSource getDb1DataSource(){return DataSourceBuilder.create().build();}@Bean("db3SqlSessionFactory")public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db3DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));return bean.getObject();}@Bean("db3SqlSessionTemplate")public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db3SqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}
}
SpringBoot入参字段定义为Date类型,不进行任何配置,默认支持
解析支持:时间戳和年-月-日字符串