鱼骨文

2018-10-24Spring 多种数据库连接池配置

2018-10-25  本文已影响0人  am330

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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 注解扫描 -->
    <context:component-scan base-package="com.hello"/>

    <!-- Spring 自带的连接池 
        DriverClassName 用来配置驱动名字
        Url  数据库
        Username 用户名
        Password 密码
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://10.25.161.7:3306/jss"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    -->
    
    <!-- 使用 DBCP 连接池 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://10.25.161.7:3306/jss"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    -->
    
    <!-- 使用 C3P0 连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://10.25.161.7:3306/jss"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    
    
    <!-- 配置 JDBC 模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

java测试类

package com.hello.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;



@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTest {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Test
    public void run() {
    
        jdbcTemplate.update("insert into tb_user values (null, ?, ?)", "cuihua2", 10.24);
    }
    

    
}
上一篇下一篇

猜你喜欢

热点阅读