当前位置:网站首页>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
边栏推荐
- Sort out several uses of network IP agent
- Qt一个进程运行另一个进程
- Source code analysis of synchronousqueue
- Buuctf Web [bjdctf2020] zjctf, but so
- Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
- 解决disagrees about version of symbol device_create
- Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
- AD20补充笔记3—快捷键+持续更新
- BUUCTF WEB [BUUCTF 2018]Online Tool
- box-sizing
猜你喜欢
Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").
0基础可以考CPDA数据分析师证书吗
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
31岁才转行程序员,目前34了,我来说说我的经历和一些感受吧...
SSM框架系列——数据源配置day2-1
box-sizing
万事有你 未来可期 | ONES 2022校园招聘正式开启
Qt绘制图像
Introduction to kubernetes
QT interprocess communication
随机推荐
QT double buffer drawing
Unlock openharmony technology day! The annual event is about to open!
A graphic designer's fantasy world | ones characters
Outsourcing for five years, abandoned
消息队列概述
NBIOT的AT指令
Introduction to metalama 4 Use fabric to manipulate items or namespaces
Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
C, calculation code of parameter points of two-dimensional Bezier curve
MySQL function - recursive function
Qt重绘事件与剪切
Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
BUUCTF WEB [GXYCTF2019]禁止套娃
万事有你 未来可期 | ONES 2022校园招聘正式开启
云原生KubeSphere部署Mysql
基于卷积神经网络的遥感影像分类识别系统
Bert base Chinese Download (SMART)
Plato Farm-以柏拉图为目标的农场元宇宙游戏
QT redraw events and cuts
航芯技术分享 | ACM32 MCU安全特性概述