<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

    Java專題文章博客和開(kāi)源

    常用鏈接

    統(tǒng)計(jì)

    最新評(píng)論

    抽取spring數(shù)據(jù)庫(kù)連接部分到項(xiàng)目中

      用spring來(lái)管理項(xiàng)目的數(shù)據(jù)庫(kù)部分,往往比自己去寫(xiě)連接要容易管理的多 ,步驟也比較簡(jiǎn)單
     
       1.項(xiàng)目根目錄下建立conf,lib目錄,將spring相關(guān)包c(diǎn)oop到lib中并導(dǎo)入,建立2個(gè)文件jdbc.properties,bean.xml
     
       jdbc.properties:
     
       [html]
     
       driverClassName=org.gjt.mm.mysql.Driver
     
       url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
     
       username=root
     
       password=root
     
       initialSize=1
     
       maxActive=300
     
       maxIdle=2
     
       minIdle=1
     
       bean.xml:
     
       [html]
     
       <?xml version="1.0" encoding="UTF-8"?>
     
       <beans xmlns=".springframework.org/schema/beans"
     
       xmlns:xsi=".w3.org/2001/XMLSchema-instance" xmlns:context=".springframework.org/schema/context"
     
       xmlns:aop=".springframework.org/schema/aop" xmlns:tx=".springframework.org/schema/tx"
     
       xsi:schemaLocation=".springframework.org/schema/beans
     
       .springframework.org/schema/beans/spring-beans-2.5.xsd
     
       .springframework.org/schema/context .springframework.org/schema/context/spring-context-2.5.xsd
     
       .springframework.org/schema/aop .springframework.org/schema/aop/spring-aop-2.5.xsd
     
       .springframework.org/schema/tx .springframework.org/schema/tx/spring-tx-2.5.xsd">
     
       <!-- 讀取jdbc.properties配置文件 location="classpath:jdbc.properties"-->
     
       <context:property-placeholder location="classpath:jdbc.properties" />
     
       <!--配置數(shù)據(jù)源 -->
     
       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
     
       destroy-method="close">
     
       <property name="driverClassName" value="${driverClassName}" />
     
       <property name="url" value="${url}" />
     
       <property name="username" value="${username}" />
     
       <property name="password" value="${password}" />
     
       <!-- 連接池啟動(dòng)時(shí)的初始值 -->
     
       <property name="initialSize" value="${initialSize}" />
     
       <!-- 連接池的最大值 -->
     
       <property name="maxActive" value="${maxActive}" />
     
       <!-- 最大空閑值.當(dāng)經(jīng)過(guò)一個(gè)高峰時(shí)間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
     
       <property name="maxIdle" value="${maxIdle}" />
     
       <!-- 最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時(shí),連接池就會(huì)預(yù)申請(qǐng)去一些連接,以免洪峰來(lái)時(shí)來(lái)不及申請(qǐng) -->
     
       <property name="minIdle" value="${minIdle}" />
     
       </bean>
     
       <!--
     
       采用注解方式來(lái)配置事務(wù)。針對(duì)數(shù)據(jù)源的事務(wù)管理器,
     
       把我們定義的數(shù)據(jù)源注入到DataSourceTransactionManager類的屬性dataSource中
     
       -->
     
       <bean id="txManager"
     
       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     
       <property name="dataSource" ref="dataSource" />
     
       </bean>
     
       <!--
     
       引入命名空間: 1.xmlns:tx=".springframework.org/schema/tx
     
       2..springframework.org/schema/tx
     
       .springframework.org/schema/tx/spring-tx-2.5.xsd
     
       采用@Transaction注解方式使用事務(wù)管理器
     
       -->
     
       <tx:annotation-driven transaction-manager="txManager" />
     
       <!-- 配置業(yè)務(wù)bean:PersonServiceBean -->
     
       <bean id="playerService" class="com.spring.db.PlayerServiceBean">
     
       <!-- 向?qū)傩詃ataSource注入數(shù)據(jù)源 -->
     
       <property name="dataSource" ref="dataSource"></property>
     
       </bean>
     
       </beans>
     
       jdbc 不用說(shuō)了,bean.xml是主要的配置文件主要有2個(gè)部分
     
       (1) <bean id="dataSource" ...>為配置數(shù)據(jù)源,從jdbc.properties讀取。
     
       如果數(shù)據(jù)源有多個(gè),只要復(fù)制這一部分就可以了
     
       (2) <!-- 配置業(yè)務(wù)bean:PersonServiceBean -->部分,這部分為配置自己的業(yè)務(wù)service,比如我配置的一個(gè)托福答案
     
       <bean id="playerService" class="com.spring.db.PlayerServiceBean">
     
       <!-- 向?qū)傩詃ataSource注入數(shù)據(jù)源 -->
     
       <property name="dataSource" ref="dataSource"></property>
     
       </bean>
     
       id 為唯一的標(biāo)識(shí), class是類路徑,name="dataSource"是類中的屬性,ref就是要用到的數(shù)據(jù)源id
     
       2.配置文件寫(xiě)好后,就可以寫(xiě)數(shù)據(jù)庫(kù)讀寫(xiě)的部分了,主要管理在DBServer.java 中
     
       [java]
     
       package com.spring.db;
     
       import org.springframework.context.ApplicationContext;
     
       import org.springframework.context.support.ClassPathXmlApplicationContext;
     
       public class DBServer {
     
       private PlayerService playerService;
     
       private static DBServer instance = null;
     
       public static DBServer getInstance() {
     
       if (instance == null) {
     
       instance = new DBServer();
     
       }
     
       return instance;
     
       }
     
       public DBServer(){
     
       ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
     
       playerService = (PlayerService) act.getBean("playerService");
     
       }
     
       public PlayerService getPlayerService() {
     
       return playerService;
     
       }
     
       public void setPlayerService(PlayerService playerService) {
     
       this.playerService = playerService;
     
       }
     
       }
     
       為了方便,就直接在構(gòu)造函數(shù)里面初始化了,代碼也容易理解
     
       playerService = (PlayerService) act.getBean("playerService");,就是對(duì)應(yīng)bean.xml中的
     
       <bean id="dataSource" ..>部分,有多個(gè)service 繼續(xù)添加就可以了
     
       PlayerService和PlayerServiceBean分別為接口和實(shí)現(xiàn)類,將需要的方法 定義在service中并在相應(yīng)的實(shí)現(xiàn)類
     
       實(shí)現(xiàn)就可以了。
     
       3.上述弄完之后,寫(xiě)個(gè)測(cè)試類測(cè)一下
     
       先在數(shù)據(jù)庫(kù)建個(gè)user(id,name)表,測(cè)試代碼:
     
       [java]
     
       public class TestSpring {
     
       public static void main(String[] args) {
     
       Player p1 = new Player(1, "spring jdbc");
     
       //add www.2cto.com
     
       DBServer.getInstance().getPlayerService().add(p1);
     
       //query
     
       Player p2 = DBServer.getInstance().getPlayerService().getPlayer(1);
     
       System.out.println("id = "+p2.getId()+" name = "+p2.getName());
     
       }
     
       運(yùn)行下,得到結(jié)果:
     
       id = 1 name = spring jdbc
     
     

    posted on 2013-06-22 10:21 好不容易 閱讀(141) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    PK10開(kāi)獎(jiǎng) PK10開(kāi)獎(jiǎng)
    主站蜘蛛池模板: 亚洲区小说区图片区QVOD| 久久这里只有精品国产免费10| 亚洲第一网站男人都懂| 亚洲av无码一区二区三区在线播放| 色se01短视频永久免费| 亚洲成a人片毛片在线| 日本片免费观看一区二区| 99久久亚洲综合精品成人网| 国产永久免费高清在线| 国产亚洲精品一品区99热| 国产做国产爱免费视频| 亚洲AV无码精品无码麻豆| 嫩草成人永久免费观看| 亚洲av不卡一区二区三区| 99re免费在线视频| 亚洲精品视频在线观看免费| 在线观看AV片永久免费| 狠狠色伊人亚洲综合网站色| 成**人免费一级毛片| 精品一区二区三区免费毛片| 亚洲日韩中文在线精品第一| 免费人成毛片动漫在线播放| 久久久久亚洲AV无码专区首JN| 免费看美女裸露无档网站| 亚洲av第一网站久章草| 四虎国产精品免费视| 国产人成网在线播放VA免费| 精品亚洲综合在线第一区| 久久久久久亚洲av无码蜜芽| 国产精品无码免费视频二三区| 亚洲老熟女五十路老熟女bbw| 免费无码又爽又刺激高潮| 亚洲人成色777777精品| 国产一精品一aⅴ一免费| 理论亚洲区美一区二区三区| 亚洲欧洲中文日韩av乱码| 国产成人无码精品久久久久免费 | 亚洲卡一卡二卡乱码新区| 最近免费中文字幕mv电影| 亚洲第一区视频在线观看| 免费视频专区一国产盗摄|