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

          Java專題文章博客和開源

          常用鏈接

          統(tǒng)計

          最新評論

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

            用spring來管理項目的數(shù)據(jù)庫部分,往往比自己去寫連接要容易管理的多 ,步驟也比較簡單
           
             1.項目根目錄下建立conf,lib目錄,將spring相關(guān)包coop到lib中并導入,建立2個文件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}" />
           
             <!-- 連接池啟動時的初始值 -->
           
             <property name="initialSize" value="${initialSize}" />
           
             <!-- 連接池的最大值 -->
           
             <property name="maxActive" value="${maxActive}" />
           
             <!-- 最大空閑值.當經(jīng)過一個高峰時間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
           
             <property name="maxIdle" value="${maxIdle}" />
           
             <!-- 最小空閑值.當空閑的連接數(shù)少于閥值時,連接池就會預(yù)申請去一些連接,以免洪峰來時來不及申請 -->
           
             <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個部分
           
             (1) <bean id="dataSource" ...>為配置數(shù)據(jù)源,從jdbc.properties讀取。
           
             如果數(shù)據(jù)源有多個,只要復(fù)制這一部分就可以了
           
             (2) <!-- 配置業(yè)務(wù)bean:PersonServiceBean -->部分,這部分為配置自己的業(yè)務(wù)service,比如我配置的一個托福答案
           
             <bean id="playerService" class="com.spring.db.PlayerServiceBean">
           
             <!-- 向?qū)傩詃ataSource注入數(shù)據(jù)源 -->
           
             <property name="dataSource" ref="dataSource"></property>
           
             </bean>
           
             id 為唯一的標識, 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" ..>部分,有多個service 繼續(xù)添加就可以了
           
             PlayerService和PlayerServiceBean分別為接口和實現(xiàn)類,將需要的方法 定義在service中并在相應(yīng)的實現(xiàn)類
           
             實現(xiàn)就可以了。
           
             3.上述弄完之后,寫個測試類測一下
           
             先在數(shù)據(jù)庫建個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());
           
             }
           
             運行下,得到結(jié)果:
           
             id = 1 name = spring jdbc
           
           

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


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


          網(wǎng)站導航:
           
          PK10開獎 PK10開獎
          主站蜘蛛池模板: 锦州市| 淮滨县| 龙口市| 蚌埠市| 颍上县| 宁安市| 兴海县| 翼城县| 余姚市| 堆龙德庆县| 鲁甸县| 城步| 恩平市| 佛教| 通城县| 昌吉市| 仲巴县| 故城县| 凤庆县| 应城市| 辽中县| 中西区| 苗栗县| 清镇市| 重庆市| 麻江县| 舒城县| 安顺市| 嘉善县| 云林县| 五河县| 镇巴县| 东台市| 乃东县| 汤阴县| 五常市| 广汉市| 托克逊县| 松江区| 凤翔县| 新巴尔虎左旗|