龙空技术网

springboot数据源配置

令狐峰宝 937

前言:

如今我们对“apache配置minetype”都比较关切,你们都需要分析一些“apache配置minetype”的相关资讯。那么小编在网摘上汇集了一些对于“apache配置minetype””的相关知识,希望我们能喜欢,咱们快快来学习一下吧!

springboot微服务框架的最佳选择,该框架将服务有效果springmvc,spring,mybatis等框架进行了有效的结合,在我们敏捷开发中非常不错的框架选择。

对于数据库访问的操作形式,通常如下:

常用数据源选择

springboot最小项目配置信息如下:

1.一般目前开发项目都是使用的maven来进行管理,日常使用的工具包可以通过(pom.xml)文件,来实现配置

<?xml version="1.0" encoding="UTF-8"?><project xmlns="" xmlns:xsi=""xsi:schemaLocation=" "> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 项目可以通过该配置以jar,war的方式实现 --> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 去除springweb中的默认是日志框架 --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 使用log4j2来实现日志框架--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter.log4j2</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <exclusions> <!-- 去除tomcat中默认的数据源引入 --> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency><!-- 引入Druid依赖 --><dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

到这里,需要的工具包配置信息就算基本完成了。

很明显比以前的spring framework,spring mvc等框架引入的等配置信息要简便很多,并且像以前的applicationContext.xml改为application.properties,来进行配置相关信息。

本文中主要说的就是如何配置数据源池和访问数据库的工具。

配置数据源,因为通常都是使用的alibaba的德鲁伊数据库连接池,内容如下:

spring.datasource.book.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.book.driver-class-name=com.mysql.jdbc.Driverspring.datasource.book.jdbc-url=jdbc:mysql://localhost:3306/super_marketspring.datasource.book.username=rootspring.datasource.book.password=19920927#连接池配置spring.datasource.book.initialSize=5spring.datasource.book.minIdle=5spring.datasource.book.maxActive=20#连接等待超时时间spring.datasource.book.maxWait=60000#配置隔多久进行一次检测(检测可以关闭的空闲连接)spring.datasource.book.timeBetweenEvictionRunsMillis=60000#配置连接在池中的最小生存时间spring.datasource.book.minEvictableIdleTimeMillis=300000spring.datasource.book.validationQuery=SELECT 1 FROM DUALspring.datasource.book.testWhileIdle=truespring.datasource.book.testOnBorrow=falsespring.datasource.book.testOnReturn=false# 打开PSCache,并且指定每个连接上PSCache的大小spring.datasource.book.poolPreparedStatements=truespring.datasource.book.maxPoolPreparedStatementPerConnectionSize=20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙spring.datasource.book.filters=stat# 通过connectProperties属性来打开mergeSql功能;慢SQL记录spring.datasource.book.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

因为对于该配置文件,druid框架无法自动的读取相关的配置信息,只通过创建一个类来进行set

/*** Created by wushaoyan on 2018/5/24.*/@Configurationpublic class DataSourceConfig {@Value("${spring.datasource.book.jdbc-url}")private String dbUrl;@Value("${spring.datasource.book.username}")private String username;@Value("${spring.datasource.book.password}")private String password;@Value("${spring.datasource.book.driver-class-name}")private String driverClassName;@Value("${spring.datasource.book.initialSize}")private int initialSize;@Value("${spring.datasource.book.minIdle}")private int minIdle;@Value("${spring.datasource.book.maxActive}")private int maxActive;@Value("${spring.datasource.book.maxWait}")private int maxWait;@Value("${spring.datasource.book.timeBetweenEvictionRunsMillis}")private int timeBetweenEvictionRunsMillis;@Value("${spring.datasource.book.minEvictableIdleTimeMillis}")private int minEvictableIdleTimeMillis;@Value("${spring.datasource.book.validationQuery}")private String validationQuery;@Value("${spring.datasource.book.testWhileIdle}")private boolean testWhileIdle;@Value("${spring.datasource.book.testOnBorrow}")private boolean testOnBorrow;@Value("${spring.datasource.book.testOnReturn}")private boolean testOnReturn;@Value("${spring.datasource.book.filters}")private String filters;@Value("${spring.datasource.book.connectionProperties}")private String connectionProperties;@Value("${spring.datasource.book.poolPreparedStatements}")private boolean poolPreparedStatements;@Value("${spring.datasource.book.maxPoolPreparedStatementPerConnectionSize}")private int maxPoolPreparedStatementPerConnectionSize;@Bean(destroyMethod = "close", initMethod = "init")@Qualifier("primaryDataSource")public DruidDataSource druidDataSource() {DruidDataSource datasource = new DruidDataSource();datasource.setUrl(dbUrl);datasource.setUsername(username);datasource.setPassword(password);datasource.setDriverClassName(driverClassName);datasource.setInitialSize(initialSize);datasource.setMinIdle(minIdle);datasource.setMaxActive(maxActive);datasource.setMaxWait(maxWait);datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);datasource.setValidationQuery(validationQuery);datasource.setTestWhileIdle(testWhileIdle);datasource.setTestOnBorrow(testOnBorrow);datasource.setTestOnReturn(testOnReturn);datasource.setPoolPreparedStatements(poolPreparedStatements);datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); return datasource;}@Bean(name = "primaryJdbcTemplate")public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {return new JdbcTemplate(dataSource);}}

小编这里使用的是jdbctemplate来实现数据库的访问,配置好上面的相关配置信息的化,那就可以开始工作了。

创建dao,service,controller,等功能,实现自己的相关业务功能。

/*** Created by wushaoyan on 2018/5/26.*/@Repositorypublic class GoodsDao implements IGoodsDao {private Log logger = LogFactory.getLog("name");@Resource @Qualifier("primaryJdbcTemplate")private JdbcTemplate jdbcTemplate;@Overridepublic Goods getGood(String name) {if (jdbcTemplate.getDataSource() instanceof DruidDataSource) {logger.info("验证确实通过@Bean注入了DruidDataSource数据源");}List<Goods> goodsList = jdbcTemplate.query("select * from items where name = ?", new Object[]{name}, new BeanPropertyRowMapper(Goods.class));Goods goods =goodsList.isEmpty() ? new Goods() : goodsList.get(0); return goods;}@Overridepublic Goods getGoodById(int id) {if (jdbcTemplate.getDataSource() instanceof DruidDataSource) {logger.info("验证确实通过@Bean注入了DruidDataSource数据源");logger.debug("debug");}List<Goods> goodsList = jdbcTemplate.query("select * from items where id = ?", new Object[]{id}, new BeanPropertyRowMapper(Goods.class));Goods goods =goodsList.isEmpty() ? new Goods() : goodsList.get(0); return goods;}}

标签: #apache配置minetype