Spring入門(三)之整合jdbc

三、spring入門之整合jdbc

通過繼承org.springframework.jdbc.core.support.JdbcDaoSupport類,通過Spring的xml向該類中注入連接池

在子類中使用org.springframework.jdbc.core.support.JdbcDaoSupport.getJdbcTemplate()方法獲取JdbcTemplate對象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
    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-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    <context:property-placeholder location="classpath:c3p0.properties" />
    
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <bean name="UserDaoImpl"  class="cn.zw.jdbc.UserDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
</beans>
package cn.zw.jdbc;
/**
 * 
 * @ClassName:  UserDao   
 * @Description:TODO(Dao的接口)   
 * @date:   2018年12月3日 下午3:25:57   
 */
public interface UserDao {
    void save();
    void delete();
    void update();
    void select();
}

package cn.zw.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * 
 * @ClassName:  UserDaoImpl   
 * @Description:TODO(模擬數(shù)據(jù)層dao的操作)   
 * @date:   2018年12月3日 下午3:28:28   
 */
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
    
    @Override
    public void save() {
        System.out.println("增加");
    }

    @Override
    public void delete() {
        System.out.println("刪除");
    }

    @Override
    public void update() {
        getJdbcTemplate().update("update Sutdent s set s.name = 'xiaoming' where code = '97001'");
    }

    @Override
    public void select() {
        System.out.println("查詢");
    }

}
package cn.zw.jdbc;



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;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-context7.xml")
public class DemoTest {

    @Autowired
    private UserDaoImpl userDaoImpl;
    
    @Test
    public void fun() throws Exception {
        userDaoImpl.update();
    }
        
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,612評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評(píng)論 6 342
  • 本文是我自己在秋招復(fù)習(xí)時(shí)的讀書筆記,整理的知識(shí)點(diǎn),也是為了防止忘記,尊重勞動(dòng)成果,轉(zhuǎn)載注明出處哦!如果你也喜歡,那...
    波波波先森閱讀 12,453評(píng)論 6 86
  • 1.古賢披卷廢食寢, 圓嘴向天我欲睡。 眼盲不解西子色, 舌壞哪知擂茶味。 2.蝸行譬思滯,車馳喻想明。 一時(shí)榨耗...
    農(nóng)民小哥閱讀 212評(píng)論 0 0
  • 認(rèn)識(shí)喬貴是在二十多年前。就在我快將這個(gè)名字淡忘時(shí),我卻永遠(yuǎn)地記住了他。 那個(gè)夏天,空氣中飄著淡淡的木芙蓉的味道,午...
    靜篤閱讀 604評(píng)論 1 3

友情鏈接更多精彩內(nèi)容