当前位置:网站首页>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
边栏推荐
- Source code analysis of synchronousqueue
- Buuctf Web [gxyctf2019] no dolls
- QT redraw events and cuts
- uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
- At instruction of nbiot
- Zero trust in network information security
- Try the server for one month for free, and attach the tutorial
- box-sizing
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- Lesson 26 static member functions of classes
猜你喜欢
![[vulnhub range] - DC2](/img/b7/c34f69a231dad653b8a912f1f36bef.png)
[vulnhub range] - DC2

Please help me see what this is, mysql5 5. Thanks

【每日一题】棋盘问题
![[csnote] ER diagram](/img/97/82e8c2183fcafda50950a953ca0955.png)
[csnote] ER diagram

【csnote】ER图

QT one process runs another

How do traditional enterprises cope with digital transformation? These books give you the answer

Redis deployment of cloud native kubesphere

box-sizing

Introduction to kubernetes
随机推荐
Pre competition practice of TIANTI competition
Qt绘制文字
How to solve the computer system card?
mysql中 innoDB执行过程分析
Worder font page font comparison table
Recommended programming AIDS: picture tool snipaste
Trier les principales utilisations de l'Agent IP réseau
万事有你 未来可期 | ONES 2022校园招聘正式开启
消息队列概述
Kubernets Getting started tutoriel
Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
4. DRF permission & access frequency & filtering & sorting
SynchronousQueue 源码解析
【vulnhub靶场】-dc2
一个平面设计师的异想世界|ONES 人物
Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
实现一个盒子在父盒子中水平垂直居中的几种“姿势”
Ad20 supplementary note 3 - shortcut key + continuous update
Kubernetes 入門教程