1 package com.liuyangfirst.conf; 2 3 import com.jolbox.bonecp.BoneCPDataSource; 4 //import com.liuyangfirst.dao.UserDao; 5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.ComponentScan; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.context.annotation.PropertySource; 10 11 import javax.sql.DataSource; 12 13 /** 14 * Created by liuya 15 * User: liuya 16 * Date: 2018/3/24 17 * Time: 22:06 18 * projectName:20180324versionone 19 */ 20 21 @Configuration 22 @PropertySource(value = {"classpath:jdbc.properties"}, ignoreResourceNotFound = true) 23 public class SpringConf { 24 25 @Value("${jdbc.url}") 26 private String jdbcUrl; 27 28 @Value("${jdbc.driverClassName}") 29 private String jdbcDriverClassName; 30 31 @Value("${jdbc.username}") 32 private String jdbcUsername; 33 34 @Value("${jdbc.password}") 35 private String jdbcPassword; 36 37 @Bean(destroyMethod = "close",name = "dataSource") 38 public DataSource dataSource() { 39 BoneCPDataSource bOneCPDataSource= new BoneCPDataSource(); 40 // 数据库驱动 41 boneCPDataSource.setDriverClass(jdbcDriverClassName); 42 // 相应驱动的jdbcUrl 43 boneCPDataSource.setJdbcUrl(jdbcUrl); 44 // 数据库的用户名 45 boneCPDataSource.setUsername(jdbcUsername); 46 // 数据库的密码 47 boneCPDataSource.setPassword(jdbcUsername); 48 // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 49 boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60); 50 // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 51 boneCPDataSource.setIdleMaxAgeInMinutes(30); 52 // 每个分区最大的连接数 53 boneCPDataSource.setMaxConnectionsPerPartition(100); 54 // 每个分区最小的连接数 55 boneCPDataSource.setMinConnectionsPerPartition(5); 56 return boneCPDataSource; 57 } 58 59 60 }
2、建立jdbc.properties,写入需要配置的信息
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc\:mysql\://localhost\:3306/taotao?characterEncoding\=utf8&useUnicode\=true&autoReconnect\=true&allowMultiQueries\=true 3 jdbc.username=root 4 jdbc.password=123456
3、配置pom.xml
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.0modelVersion> 5 6 <groupId>com.liuyangfirstgroupId> 7 <artifactId>20180330versiontwoartifactId> 8 <version>0.0.1-SNAPSHOTversion> 9 <packaging>warpackaging> 10 11 <name>20180330versiontwoname> 12 <description>Demo project for Spring Bootdescription> 13 <url>http://maven.apache.orgurl> 14 15 <properties> 16 <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> 17 <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> 18 <java.version>1.8java.version> 19 <junit.version>4.12junit.version> 20 properties> 21 <parent> 22 <groupId>org.springframework.bootgroupId> 23 <artifactId>spring-boot-starter-parentartifactId> 24 <version>1.4.1.RELEASEversion> 25 parent> 26 27 <dependencies> 28 29 <dependency> 30 <groupId>junitgroupId> 31 <artifactId>junitartifactId> 32 <version>${junit.version}version> 33 <scope>testscope> 34 dependency> 35 <dependency> 36 <groupId>org.springframeworkgroupId> 37 <artifactId>spring-webmvcartifactId> 38 <version>4.3.7.RELEASEversion> 39 dependency> 40 41 <dependency> 42 <groupId>org.springframeworkgroupId> 43 <artifactId>spring-testartifactId> 44 <version>4.3.9.RELEASEversion> 45 dependency> 46 <dependency> 47 <groupId>org.springframeworkgroupId> 48 <artifactId>spring-testartifactId> 49 <version>4.3.9.RELEASEversion> 50 dependency> 51 <dependency> 52 <groupId>org.springframework.bootgroupId> 53 <artifactId>spring-boot-starter-testartifactId> 54 <scope>testscope> 55 dependency> 56 <dependency> 57 <groupId>junitgroupId> 58 <artifactId>junitartifactId> 59 <version>4.12version> 60 dependency> 61 <dependency> 62 <groupId>org.springframework.bootgroupId> 63 <artifactId>spring-boot-testartifactId> 64 <version>RELEASEversion> 65 dependency> 66 <dependency> 67 <groupId>org.springframework.bootgroupId> 68 <artifactId>spring-bootartifactId> 69 <version>2.0.0.RELEASEversion> 70 dependency> 71 <dependency> 72 <groupId>org.springframework.bootgroupId> 73 <artifactId>spring-boot-autoconfigureartifactId> 74 <version>1.4.1.RELEASEversion> 75 dependency> 76 77 <dependency> 78 <groupId>javax.servlet.jspgroupId> 79 <artifactId>jsp-apiartifactId> 80 <version>2.2version> 81 <scope>providedscope> 82 dependency> 83 84 <dependency> 85 <groupId>javax.servletgroupId> 86 <artifactId>servlet-apiartifactId> 87 <version>2.5version> 88 <scope>providedscope> 89 dependency> 90 91 <dependency> 92 <groupId>commons-logginggroupId> 93 <artifactId>commons-loggingartifactId> 94 <version>1.2version> 95 dependency> 96 97 98 <dependency> 99 <groupId>com.jolboxgroupId> 100 <artifactId>bonecp-springartifactId> 101 <version>0.8.0.RELEASEversion> 102 dependency> 103 104 <dependency> 105 <groupId> org.apache.cassandragroupId> 106 <artifactId>cassandra-allartifactId> 107 <version>0.8.1version> 108 109 <exclusions> 110 <exclusion> 111 <groupId>org.slf4jgroupId> 112 <artifactId>slf4j-log4j12artifactId> 113 exclusion> 114 <exclusion> 115 <groupId>log4jgroupId> 116 <artifactId>log4jartifactId> 117 exclusion> 118 exclusions> 119 dependency> 120 121 <dependency> 122 <groupId>org.slf4jgroupId> 123 <artifactId>slf4j-simpleartifactId> 124 <version>1.7.25version> 125 dependency> 126 127 <dependency> 128 <groupId>mysqlgroupId> 129 <artifactId>mysql-connector-javaartifactId> 130 <version>5.1.13version> 131 dependency> 132 133 <dependency> 134 <groupId>com.google.guavagroupId> 135 <artifactId>guavaartifactId> 136 <version>21.0version> 137 dependency> 138 139 dependencies> 140 141 <build> 142 <finalName>${project.artifactId}finalName> 143 <plugins> 144 145 <plugin> 146 <groupId>org.apache.maven.pluginsgroupId> 147 <artifactId>maven-resources-pluginartifactId> 148 <configuration> 149 <encoding>UTF-8encoding> 150 configuration> 151 plugin> 152 153 <plugin> 154 <groupId>org.apache.maven.pluginsgroupId> 155 <artifactId>maven-compiler-pluginartifactId> 156 <configuration> 157 <source>1.7source> 158 <target>1.7target> 159 <encoding>UTF-8encoding> 160 configuration> 161 plugin> 162 plugins> 163 build> 164 165 project>
4、测试数据库联通
1 package com.liuyangfirst.test; 2 3 import com.liuyangfirst.conf.SpringConf; 4 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 import javax.sql.DataSource; 6 import java.sql.Connection; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 11 /** 12 * Created by liuya 13 * User: liuya 14 * Date: 2018/3/30 15 * Time: 22:56 16 * projectName:20180330versiontwo 17 */ 18 public class TestDateSource { 19 20 public static void main(String[] args) { 21 // 通过Java配置来实例化Spring容器 22 AnnotationConfigApplicationContext cOntext= new AnnotationConfigApplicationContext(SpringConf.class); 23 24 DataSource dataSource = context.getBean(DataSource.class); 25 26 Connection cOnnection= null; 27 28 try { 29 cOnnection= dataSource.getConnection(); 30 if (connection != null) { 31 System.out.println("Connection successful!"); 32 Statement stmt = connection.createStatement(); 33 ResultSet rs = stmt.executeQuery(" select * from tb_user "); // do something with the connection. 34 while (rs.next()) { 35 System.out.println(rs.getString(1)); // should print out "1"' 36 System.out.println(rs.getString(2)); // should print out "1"' 37 } 38 } 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } finally { 42 try { 43 connection.close(); 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } 47 } 48 } 49 }
5、测试结果