Using spring JdbcTemplate in spring boot (1) - connection pool to create multiple connections

This article refers to some articles on the Internet, and finally organizes the practice.

1. Create a springboot demo program, you can refer to my article:

Create springboot demo program

2. Take a look at the directory structure:


3. First, you need to introduce dependencies in the pom file:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

4. Add the corresponding connection pool and connection object to configure in the configuration file datasourcemysql.xml, and you need to add the corresponding jdbc.url and other specific configurations in the properties file (ignored here).

<?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">

    <!-- datasource-->
    <bean id="dataSourcePool" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- Basic attributes url, user, password -->
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- Configure initialization size, minimum, maximum -->
        <property name="initialSize" value="1"/>
        <property name="minIdle" value="1"/>
        <property name="maxActive" value="20"/>
        <!-- Configure the time for getting a connection to wait for timeout-->
        <property name="maxWait" value="10000"/>
        <!-- How long is the configuration interval to perform detection, and detect idle connections that need to be closed, in milliseconds-->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <!-- Configure the minimum lifetime of a connection in the pool, in milliseconds-->
        <property name="minEvictableIdleTimeMillis" value="300000"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <!-- Open PSCache and specify the size of PSCache on each connection. If Oracle is used, configure poolPreparedStatements to true, and mysql can be configured to false. -->
        <property name="poolPreparedStatements" value="false"/>
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
    </bean>

    <!-- datasource-->
    <bean id="dataSourcePool2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- Basic attributes url, user, password -->
        <property name="url" value="${jdbc.url2}"/>
        <property name="username" value="${jdbc.username2}"/>
        <property name="password" value="${jdbc.password2}"/>
        <!-- Configure initialization size, minimum, maximum -->
        <property name="initialSize" value="1"/>
        <property name="minIdle" value="1"/>
        <property name="maxActive" value="20"/>
        <!-- Configure the time for getting a connection to wait for timeout-->
        <property name="maxWait" value="10000"/>
        <!-- How long is the configuration interval to perform detection, and detect idle connections that need to be closed, in milliseconds-->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <!-- Configure the minimum lifetime of a connection in the pool, in milliseconds-->
        <property name="minEvictableIdleTimeMillis" value="300000"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <!-- Open PSCache and specify the size of PSCache on each connection. If Oracle is used, configure poolPreparedStatements to true, and mysql can be configured to false. -->
        <property name="poolPreparedStatements" value="false"/>
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
    </bean>

    <bean id="remoteJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSourcePool"></property>
    </bean>

    <bean id="localJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSourcePool2"></property>
    </bean>


</beans>

5. Inject two jdbc connection objects into the code (it should be noted here that the name of this variable must be the same as the id of the bean declared in xml, otherwise you need to add an annotation to @resource to specify the name)

6. At the same time, you need to add the following content to the startup type of springboot:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ImportResource(locations = {"classpath:datasourcemysql.xml"})

The function of this is to prohibit springboot from automatically binding the configuration of dbc to generate errors. Another is to add the bean declared in datasourcemysql.xml to the scanning scope of springboot.

7. This is how it is used.

@Resource
    private JdbcTemplate remoteJdbcTemplate;


    @Resource
    private JdbcTemplate localJdbcTemplate;

8. Then it can be used normally.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325641156&siteId=291194637