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

          Java專題文章博客和開源

          常用鏈接

          統(tǒng)計(jì)

          最新評論

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

            用spring來管理項(xiàng)目的數(shù)據(jù)庫部分,往往比自己去寫連接要容易管理的多 ,步驟也比較簡單
           
             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)過一個(gè)高峰時(shí)間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
           
             <property name="maxIdle" value="${maxIdle}" />
           
             <!-- 最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時(shí),連接池就會(huì)預(yù)申請去一些連接,以免洪峰來時(shí)來不及申請 -->
           
             <property name="minIdle" value="${minIdle}" />
           
             </bean>
           
             <!--
           
             采用注解方式來配置事務(wù)。針對數(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 不用說了,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)識, class是類路徑,name="dataSource"是類中的屬性,ref就是要用到的數(shù)據(jù)源id
           
             2.配置文件寫好后,就可以寫數(shù)據(jù)庫讀寫的部分了,主要管理在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");,就是對應(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.上述弄完之后,寫個(gè)測試類測一下
           
             先在數(shù)據(jù)庫建個(gè)user(id,name)表,測試代碼:
           
             [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 好不容易 閱讀(143) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          PK10開獎(jiǎng) PK10開獎(jiǎng)
          主站蜘蛛池模板: 丰都县| 青岛市| 普兰店市| 望江县| 西城区| 台江县| 寿阳县| 峡江县| 吉首市| 宣武区| 贵阳市| 韩城市| 措美县| 庄浪县| 土默特左旗| 紫金县| 阳信县| 松滋市| 余姚市| 鄂托克旗| 于都县| 金秀| 凌云县| 莲花县| 太谷县| 桂阳县| 探索| 富民县| 鄂托克前旗| 漾濞| 威海市| 喀喇| 濉溪县| 芦山县| 宁强县| 交城县| 建瓯市| 成武县| 奉新县| 海门市| 五莲县|