用spring來管理項目的數據庫部分
jdbc
[html]
driverClassName=org
url=jdbc:mysql://localhost:
username=root
password=root
initialSize=
maxActive=
maxIdle=
minIdle=
bean
[html]
<?xml version=
<beans xmlns=
xmlns:xsi=
xmlns:aop=
xsi:schemaLocation=
/schema/beans/spring
/schema/context /schema/context/spring
/schema/aop /schema/aop/spring
/schema/tx /schema/tx/spring
<!
<context:property
<!
<bean id=
destroy
<property name=
<property name=
<property name=
<property name=
<!
<property name=
<!
<property name=
<!
<property name=
<!
<property name=
</bean>
<!
采用注解方式來配置事務
把我們定義的數據源注入到DataSourceTransactionManager類的屬性dataSource中
<bean id=
class=
<property name=
</bean>
<!
引入命名空間
/schema/tx/spring
采用@Transaction注解方式使用事務管理器
<tx:annotation
<!
<bean id=
<!
<property name=
</bean>
</beans>
jdbc 不用說了
(
如果數據源有多個
(
<bean id=
<!
<property name=
</bean>
id 為唯一的標識
[java]
package com
import orgntext
import orgntext
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(
playerService = (PlayerService) act
}
public PlayerService getPlayerService() {
return playerService;
}
public void setPlayerService(PlayerService playerService) {
this
}
}
為了方便
playerService = (PlayerService) act
<bean id=
PlayerService和PlayerServiceBean分別為接口和實現類
實現就可以了
先在數據庫建個user(id
[java]
public class TestSpring {
public static void main(String[] args) {
Player p
//add
DBServer
//query
Player p
System
}
運行下
id =
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28088.html