日期:2014-05-16  浏览次数:20382 次

HaishenDB的配置

HaishenDB目前只有基于spring jdbc的实现,配置完全采用spring jdbc的方式.

?

spring的配置文件 applicationContext.xml

?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
	
	<!-- The placeholders are resolved from jdbc.properties through -->
	<!-- the PropertyPlaceholderConfigurer in applicationContext.xml -->
	<bean id="dataSourceCore" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>

	<!-- Transaction manager for a single JDBC DataSource -->
	<!-- (see dataAccessContext-jta.xml for an alternative) -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSourceCore"/>
	</bean>


	<!-- ========================= DAO DEFINITIONS: IBATIS IMPLEMENTATIONS ========================= -->
        <bean id="test" class="com.haishen.db.test.api.impl.ITestImpl">
            <property name="dao" ref="dao"/>
        </bean>
        
        <bean id="dao" class="com.haishen.db.api.impl.IDAOSpringJdbcImpl">
            <property name="dataSource" ref="dataSourceCore"/>
            <property name="sqlBuilder" ref="sqlbuilder"/>
        </bean>
        
        <bean id="sqlbuilder" class="com.haishen.db.util.PostgreSQLBuilder"></bean>
</beans>
?

接下来配置数据库连接.这个配置是一个属性文件jdbc.properties.跟上面的xml文件放在同一个路径下.内容如下.

?

jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/Test
jdbc.username=postgres
jdbc.password=test

?

说明,com.haishen.db.api.impl.IDAOSpringJdbcImpl是基于spring jdbc的实现类.com.haishen.db.util.PostgresSQLBuilder是HaishenDB生成符合postgres数据库要求的sql语句工具类.如果是别的数据库,可以使用相应的工具类.目前HaishenDB提供postgres,oracle,sql server这三个数据库的工具类。