中文JAVA技術平等自由協作創造

          Java專題文章博客和開源

          常用鏈接

          統計

          最新評論

          抽取spring數據庫連接部分到項目中

            用spring來管理項目的數據庫部分,往往比自己去寫連接要容易管理的多 ,步驟也比較簡單
           
             1.項目根目錄下建立conf,lib目錄,將spring相關包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" />
           
             <!--配置數據源 -->
           
             <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}" />
           
             <!-- 最大空閑值.當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
           
             <property name="maxIdle" value="${maxIdle}" />
           
             <!-- 最小空閑值.當空閑的連接數少于閥值時,連接池就會預申請去一些連接,以免洪峰來時來不及申請 -->
           
             <property name="minIdle" value="${minIdle}" />
           
             </bean>
           
             <!--
           
             采用注解方式來配置事務。針對數據源的事務管理器,
           
             把我們定義的數據源注入到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注解方式使用事務管理器
           
             -->
           
             <tx:annotation-driven transaction-manager="txManager" />
           
             <!-- 配置業務bean:PersonServiceBean -->
           
             <bean id="playerService" class="com.spring.db.PlayerServiceBean">
           
             <!-- 向屬性dataSource注入數據源 -->
           
             <property name="dataSource" ref="dataSource"></property>
           
             </bean>
           
             </beans>
           
             jdbc 不用說了,bean.xml是主要的配置文件主要有2個部分
           
             (1) <bean id="dataSource" ...>為配置數據源,從jdbc.properties讀取。
           
             如果數據源有多個,只要復制這一部分就可以了
           
             (2) <!-- 配置業務bean:PersonServiceBean -->部分,這部分為配置自己的業務service,比如我配置的一個托福答案
           
             <bean id="playerService" class="com.spring.db.PlayerServiceBean">
           
             <!-- 向屬性dataSource注入數據源 -->
           
             <property name="dataSource" ref="dataSource"></property>
           
             </bean>
           
             id 為唯一的標識, class是類路徑,name="dataSource"是類中的屬性,ref就是要用到的數據源id
           
             2.配置文件寫好后,就可以寫數據庫讀寫的部分了,主要管理在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;
           
             }
           
             }
           
             為了方便,就直接在構造函數里面初始化了,代碼也容易理解
           
             playerService = (PlayerService) act.getBean("playerService");,就是對應bean.xml中的
           
             <bean id="dataSource" ..>部分,有多個service 繼續添加就可以了
           
             PlayerService和PlayerServiceBean分別為接口和實現類,將需要的方法 定義在service中并在相應的實現類
           
             實現就可以了。
           
             3.上述弄完之后,寫個測試類測一下
           
             先在數據庫建個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());
           
             }
           
             運行下,得到結果:
           
             id = 1 name = spring jdbc
           
           

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


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


          網站導航:
           
          PK10開獎 PK10開獎
          主站蜘蛛池模板: 同心县| 上虞市| 泸溪县| 长泰县| 额尔古纳市| 蓬莱市| 太保市| 隆昌县| 东海县| 桐柏县| 九龙坡区| 尼玛县| 太保市| 柏乡县| 邮箱| 茂名市| 宜阳县| 宁明县| 含山县| 乌兰浩特市| 建宁县| 湘西| 富平县| 沂源县| 克拉玛依市| 曲松县| 金山区| 桐城市| 白银市| 嵩明县| 绥芬河市| 寻甸| 罗源县| 武鸣县| 历史| 新营市| 扬州市| 炎陵县| 恩平市| 临泽县| 赤水市|