您的位置:首页 > Web前端 > JavaScript

jsp项目数据源多种配置

2013-12-08 20:36 465 查看
/**

*数据源(mysql、sqlserver、oracle)+数据源信息配置(jndi、properties、xml)+数据源信息读取(jdbc、spring、hibernate)

*本文件总结了mysql,sqlserver,oracle三种数据源的连接方式

*其中包括Spring、Hibernate的连接方式

*并通过属性文件、JNDI、配置文件三种方式进行连接

*/

1.通过属性文件 读取数据源配置

--jdbc.properties

1)mysql

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncodeing=utf-8

jdbc.username=root

jdbc.password=123456

2)sqlserver

jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver

jdbc.url=jdbc:jtds:sqlserver://localhost:1433/test

jdbc.username=sa

jdbc.password=sa

3)oracle

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:orcale:thin://localhost:1521/nquser

jdbc.username=wapcms

jdbc.password=wapcms

--Spring-applicationContext-data.xml

<beans>

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName"

value="${jdbc.driverClassName}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

</bean>

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:jdbc.properties</value>

</list>

</property>

</bean>

<!-- 设定transactionManager -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

<tx:annotation-driven/>

</beans>

2.用JNDI 读取数据源server.xml

在<GlobalNamingResources>标签元素下填加

1)mysql

<Resource name="jdbc/mysqlonline" type="javax.sql.DataSource"

driverClassName="com.mysql.jdbc.Driver"

password="123456"

maxIdle="30"

maxWait="10000"

username="root"

url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gb2312

maxActive="100" />

2)orcale

<Resource name="jdbc/wapcms" type="javax.sql.DataSource"

driverClassName="oracle.jdbc.driver.OracleDriver"

password="123456"

maxIdle="30"

maxWait="10000"

username="root"

url="jdbc:oracle:thin:@localhost:1521:nquser"

maxActive="100"

removeAbandoned="true"

removeAbandonedTimeout="300"

logAbandoned="true"/>

3)sqlserver

<Resource name="jdbc/user" type="javax.sql.DataSource"

driverClassName="net.sourceforge.jtds.jdbc.Driver"

password="sa"

maxIdle="30"

maxWait="10000"

username="sa"

url="jdbc:jtds:sqlserver://localhost:1433/test"

maxActive="100"

removeAbandoned="true"

removeAbandonedTimeout="300"

logAbandoned="true"/>

在<Host>下

<Context path="" docBase="E:/WAP/WAP_CMS/WebContent"

debug="0" reloadable="true" privileged="true" crossContext="true"

useNaming="true">

<ResourceLink name="jdbc/wapcms" global="jdbc/wapcms" type="javax.sql.DataSource" />

<ResourceLink name="jdbc/mysqlonline" global="jdbc/mysqlonline" type="javax.sql.DataSource" />

</Context>

</Host>

--web.xml

<resource-ref>

<description>DB Connection</description>

<res-ref-name>jdbc/wapcms</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

--Spring-application-data.xml

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName">

<value>java:comp/env/jdbc/wapcms</value>

</property>

</bean>

<!-- 设定transactionManager -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

3.用配置文件 读取数据源信息

---hibernate.cfg.xml

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

<property name="connection.url">jdbc:oracle:thin:@localhost:1521:ora</property>

<property name="connection.username">koooso</property>

<property name="connection.password">koooso</property>

<!-- JDBC connection pool (use the built-in) -->

<property name="connection.pool_size">5</property>

<!-- 注意如果是运行在application中时,必须要有current_session_context_class这个属性,且值为

thread。如果是运行在WEB服务器中则需要将值设置成jta。否则在运行时会报Exception

in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured! 这个异常-->

<property name="current_session_context_class">jta</property>

<!-- Echo all executed SQL to stdout -->

<property name="show_sql">true</property>

<!-- SQL dialect -->

<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

<!-- Drop and re-create the database schema on startup -->

<property name="hbm2ddl.auto">create</property>

<mapping resource="com/chinamworld/hibernate/tf/MyTest.hbm.xml"/>

</session-factory>

</hibernate-configuration>

---sqlserver Spring

<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName"

value="net.sourceforge.jtds.jdbc.Driver">

</property>

<property name="url"

value="jdbc:jtds:sqlserver://localhost:1433/test">

</property>

<property name="username" value="sa"></property>

<property name="password" value="admin"></property>

</bean>

<!-- 将Hibernate交由Spring管理(Hibernate相关配置信息) ,创建SessionFactory-->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.SQLServerDialect

</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>com/wuwei/struts/dao/User.hbm.xml</value>

</list>

</property>

</bean>

4.多种数据源配置

<!-- DBCP data source -->

<!--

<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

<property name="defaultAutoCommit" value="false"></property>

<property name="maxActive" value="50"></property>

<property name="url" value="jdbc:mysql://localhost:3306/exam"></property>

<property name="username" value="root"></property>

<property name="password" value=""></property>

<property name="password" value="true"></property>

<property name="password" value="true"></property>

<property name="password" value="180"></property>

</bean>

-->

<!-- C3P0 data source -->

<bean id="dataSource"

class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

<property name="driverClass" value="com.mysql.jdbc.Driver"></property>

<property name="autoCommitOnClose" value="false"></property>

<property name="maxIdleTime" value="1800"></property> <!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃 -->

<property name="maxPoolSize" value="50"></property>

<property name="initialPoolSize" value="5"></property>

<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/exam"></property>

<property name="user" value="root"></property>

<property name="password" value=""></property>

</bean>

<!-- JNDI data source -->

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName">

<value>java:comp/env/jdbc/wapcms</value>

</property>

</bean>

<!-- Hibernate sessionFactory -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>com/brady/exam/model/Grade.hbm.xml</value>

<value>com/brady/exam/model/Paper.hbm.xml</value>

<value>com/brady/exam/model/Test.hbm.xml</value>

<value>com/brady/exam/model/Subject.hbm.xml</value>

</list>

</property>

</bean>

<!-- Tranasction Manager -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="insert*" propagation="REQUIRED"/>

<tx:method name="edit*" propagation="REQUIRED"/>

<tx:method name="save*" propagation="REQUIRED"/>

<tx:method name="update*" propagation="REQUIRED"/>

<tx:method name="merge*" propagation="REQUIRED"/>

<tx:method name="remove*" propagation="REQUIRED"/>

<tx:method name="delete*" propagation="REQUIRED"/>

<tx:method name="find*" read-only="true"/>

<tx:method name="get*" read-only="true"/>

</tx:attributes>

</tx:advice>

<aop:config>

<aop:pointcut id="daoPointcut"

expression="execution(* com.brady.exam.dao.*.*(..))"/>

<aop:advisor advice-ref="txAdvice" pointcut-ref="daoPointcut"/>

</aop:config>

<!-- End Tranasction Manager -->

<bean id="userDAO" class="com.brady.exam.dao.impl.hibernate.UserDAO">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

<bean id="basicDAO" class="com.brady.exam.dao.impl.hibernate.BasicDAO">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: