用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