当前位置:网站首页>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
边栏推荐
猜你喜欢

Image attribute of input: type attribute of fashion cloud learning -h5

消息队列概述

XinChaCha Trust SSL Organization Validated

XinChaCha Trust SSL Organization Validated

Use source insight to view and edit source code

基于卷积神经网络的遥感影像分类识别系统

解决disagrees about version of symbol device_create

云原生KubeSphere部署Redis

网站首页文件被攻击篡改的形式有哪些

没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
随机推荐
Pagoda panel command line help tutorial (including resetting password)
One way ANOVA of SPSS
使用Source Insight查看编辑源代码
Qt进程间通信
实现一个盒子在父盒子中水平垂直居中的几种“姿势”
云原生KubeSphere部署Mysql
Try the server for one month for free, and attach the tutorial
BUUCTF WEB [BUUCTF 2018]Online Tool
甲辰篇 創世紀《「內元宇宙」聯載》
Stacks and queues a
Kubernetes 入門教程
Qt绘制文字
Stm32cubeprogrammer basic instructions
SSL certificate refund instructions
SQLserver怎么插入或更新当天的星期数,bit而不是文本
On using go language to create websocket service
一个平面设计师的异想世界|ONES 人物
S2-062 remote command execution vulnerability recurrence (cve-2021-31805)
BUUCTF WEB [BJDCTF2020]The mystery of ip
网络信息安全之零信任