1. 程式人生 > >Spring通過XML配置c3p0連線池和dao層註解注入使用 jdbcTemplate

Spring通過XML配置c3p0連線池和dao層註解注入使用 jdbcTemplate

Spring通過註解配置c3p0連線池和dao使用 jdbcTemplate

1.Spring配置c3p0連線池

第一步:匯入c3p0的jar包

這裡寫圖片描述

第二步:建立Spring配置檔案,配置連線池

平常我們寫c3p0連線池時是這樣寫的:

        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/template"
); dataSource.setUser("root"); dataSource.setPassword("");

現在有了Spring後,我們就通過Spring的容器(IOC思想,控制反轉)來進行JavaBean的物件管理。

Spring的配置檔案:

<?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"
>
<!-- 配置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://localhost:3306/template"></property> <property name="user" value="root"></property> <property name="password" value=""></property> </bean> </beans>

2.在dao層,使用jdbcTemplate

既然在dao層裡面使用jdbcTemplate。那麼現在就定義一個完整的,結構,包括dao層,service層的例子:

dao層:

//通過註解方式建立物件,物件被Spring管理。
@Component(value="userDao")
public class UserDao {

    //註解方式注入物件
    @Resource(name="jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    public User showUser(String name){
        User user=jdbcTemplate.queryForObject("select * from user where name=?", new RowMapper<User>(){

            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user=new User(rs.getString("name"), rs.getString("password"));
                return user;
            }}, name);
        return user;
    }
}

service層:

//通過註解方式建立物件,物件被Spring管理。
@Service(value="userService")
public class UserService {
    //通過註解方式注入物件
    @Resource(name="userDao")
    private UserDao userDao;

    public User showUser(String name){
        return userDao.showUser(name);
    }
}

Spring的核心配置檔案

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

    <!-- 開啟JavaBean註釋掃描 -->
    <context:component-scan base-package="cn.domarvel"></context:component-scan>
    <!-- 建立c3p0連線池物件,並且初始化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://localhost:3306/template"></property>
        <property name="user" value="root"></property>
        <property name="password" value=""></property>
    </bean>
    <!-- 建立jdbcTemplate,並且通過IOC的依賴注入的構造注入方式注入c3p0連線池物件,那麼獲取到jdbcTemplate後就能夠直接使用 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>
</beans>

這裡要注意一點:當我們是構造注入的時候,這裡的name屬性的值為建構函式傳參那裡定義的名稱,而不是成員變數的名稱。

比如我這裡是用的JdbcTemplate的有參構造方法。

//這是JdbcTemplate的建構函式原始碼
    public JdbcTemplate(DataSource dataSource) {
        setDataSource(dataSource);
        afterPropertiesSet();
    }

因為傳參的地方引數名為dataSource所以,在Spring的核心配置檔案中name屬性值也是dataSource

現在講講上面的實現邏輯:

我想通過JdbcTemplate實現查詢資訊,裡面的連線池用的是c3p0.

我定義了一個UserDao類,目的是為了查詢資料庫裡面User的具體資訊。

要想在UserDao裡面使用JdbcTemplate,就必須先建立好c3p0連線池,建立c3p0連線池在Spring核心配置裡面完成,有了c3p0連線池後就開始建立JdbcTemplate,建立JdbcTemplate是通過構造方法注入c3p0連線池物件的。JdbcTemplate建立好後,就在UserDao裡面定義好JdbcTemplate,並且注入JdbcTemplate物件,注入後就能夠通過JdbcTemplate物件查詢資料庫的資訊了。後面就是通過註解方式建立UserDao,建立UserService,並且在UserService裡面注入UserDao並且使用了。使用註解方式建立JavaBean物件還要開啟JavaBean註解掃描。