当前位置:网站首页>SSM framework series - data source configuration day2-1
SSM framework series - data source configuration day2-1
2022-04-23 12:42:00 【Concise programming】
SSM Frame series —— Data source configuration
- data source
-
- data source ( Connection pool ) effect
- Development steps
- Sample code (Druid)
- Profile extraction (jdbcDruid.properties)
- Spring Configure data sources
- Optimize Spring Configure data sources
data source
data source ( Connection pool ) effect
data source ( Connection pool ) It appears to improve program performance
Instantiate the data source in advance , Initialize some connection resources
Get from the data source when using the connection resource
Return the connected resources to the data source after use
Development steps
- Import the coordinates of the data source and database driven coordinates
- Create data source objects
- Set the basic connection data of the data source
- Use the data source to obtain connection resources and return connection resources
Sample code (Druid)
Because now druid It's the most popular, so I use druid, The others are similar , But I haven't used it
1. Import the coordinates of the data source and database driven coordinates
Go to maven The central warehouse finds the corresponding coordinates
Warehouse address :https://mvnrepository.com
Find the corresponding version
I mysql The version is 8.0.16
choice maven Coordinate copy
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
Entry project pom.xml File import and refresh maven
Druid Is the same
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
2. Create data source objects
// Create data source objects
DruidDataSource source = new DruidDataSource();
3. Set the basic connection data of the data source
source.setDriverClassName("com.mysql.cj.jdbc.Driver");
source.setUrl("jdbc:mysql://localhost:3306/syf?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true");
source.setUsername("root");
source.setPassword("syf20020816");
4. Use the data source to obtain connection resources and return connection resources
// 4. Use the data source to obtain connection resources and return connection resources
DruidPooledConnection connection = source.getConnection();
System.out.println(connection);
connection.close();
Complete code
package com.exampl.testUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
public class TestDruid {
@Test
public void DruidTest() throws SQLException {
// Create data source objects
DruidDataSource source = new DruidDataSource();
// 3. Set the basic connection data of the data source
source.setDriverClassName("com.mysql.cj.jdbc.Driver");
source.setUrl("jdbc:mysql://localhost:3306/syf?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true");
source.setUsername("root");
source.setPassword("syf20020816");
// 4. Use the data source to obtain connection resources and return connection resources
DruidPooledConnection connection = source.getConnection();
System.out.println(connection);
connection.close();
}
}
Results and explanatory screenshots
Profile extraction (jdbcDruid.properties)
1. stay test.resources Create a new resource package
2. Extract configuration
driverClassName=com.mysql.cj.jdbc.Driver
url:jdbc:mysql://localhost:3306/syf?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
username:root
password:syf20020816
3. Use the configuration file for database connection
package com.exampl.testUtil;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
import java.util.ResourceBundle;
public class TestDruidPro {
@Test
public void testDruidPro() throws Exception {
// Read configuration file
Properties properties = new Properties();
InputStream resourceAsStream = TestDruidPro.class.getClassLoader().getResourceAsStream("jdbcDruid.properties");
properties.load(resourceAsStream);
System.out.println(resourceAsStream);
// Connect
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
Results screenshots
Spring Configure data sources
Use set Methods to inject
1. Import dependence
Here are all my dependencies ,mysql,druid,junit,spring You can just add what you don't have
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${
junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${
junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
2. To write applicationContext.xml
Be careful
stay xml in &
Character to use &
To replace
<property name="url" value="jdbc:mysql://localhost:3306/syf?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true"></property>
Full configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="syf20020816"></property>
<property name="url" value="jdbc:mysql://localhost:3306/syf?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true"></property>
</bean>
</beans>
3. test
package com.exampl.testUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class TestDruid {
@Test
public void springDruid() throws SQLException {
// load spring The configuration file
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) app.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
result
Optimize Spring Configure data sources
Our optimization plan is to jdbc Configuration files and spring Separation of configuration files , Form two separate domains , Then call the domain
1. Introduce namespace and address
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
2. Load external properties
<context:property-placeholder location="classpath:jdbcDruid.properties"></context:property-placeholder>
3. rewrite < property >
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="url"
value="${url}"></property>
</bean>
complete applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbcDruid.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="url"
value="${url}"></property>
</bean>
</beans>
版权声明
本文为[Concise programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231239441898.html
边栏推荐
猜你喜欢
随机推荐
Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
leetcode-791. 自定义字符串排序
SQL exercise (I)
Metalama简介4.使用Fabric操作项目或命名空间
Trier les principales utilisations de l'Agent IP réseau
Luogu p5540 [balkanoi2011] timeismoney | minimum product spanning tree problem solution
Pagoda panel command line help tutorial (including resetting password)
Qt重绘事件与剪切
Outsourcing for five years, abandoned
Introduction to metalama 4 Use fabric to manipulate items or namespaces
BUUCTF WEB [BJDCTF2020]The mystery of ip
Image attribute of input: type attribute of fashion cloud learning -h5
XinChaCha Trust SSL Organization Validated
QT draw text
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
QT draw image
A graphic designer's fantasy world | ones characters
RT-thread中关键词解释及部分API
Lesson 26 static member functions of classes
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】