SSH多数据库连接

本文档展示了如何在SSH框架基础上实现Oracle和MySQL两个数据库的连接。通过配置db-config.properties文件,设置不同数据库的数据源,然后在spring.xml中配置多个数据源、sessionFactory、hibernateTemplate和transactionManager。在BaseDao类中根据数据库类型获取相应的Session、SessionFactory和HibernateTemplate,最后通过UserDao类的findUser方法演示了多数据库操作。
摘要由CSDN通过智能技术生成

本实例在ssh框架基础上实现 oracle和mysql 2个数据库连接

一:db-config.properties:(数据源配置)

hibernate.cglib.use_reflection_optimizer=true
hibernate.connection.proxool_provider_class=org.hibernate.connection.ProxoolConnectionProvider
hibernate.connection.c3p0_provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.proxool.pool_alias=spring
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.hbm2ddl.auto=update
hibernate.default_schema=mySchema

#mysql数据库
mysql.hibernate.dialect=org.hibernate.dialect.MySQLDialect
mysql.datasource.driverClassName=com.mysql.jdbc.Driver
mysql.datasource.url=jdbc:mysql://localhost:3306/youth?useUnicode=true&characterEncoding=utf8
mysql.datasource.username=root
mysql.datasource.password=root

#oracle数据库
oracle.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
#oracle.hibernate.dialect=com.common.jdbc.OracleDialectExtType
oracle.hibernate.dialect=org.hibernate.dialect.OracleDialect
oracle.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:ORCL
oracle.datasource.username=scott
oracle.datasource.password=scott

datasource.testSql=select current_date from dual

二:spring.xml配置:

注意:配置spring.xml时
1.可以多个数据源,名字随意
2.可以多个sessionFactory,名字随意
3.可以多个hibernateTemplate,名字随意(但至少有一个名字为 hibernateTemplate,本人亲测,可能是刚好碰上了, 如果有人测试不是这样,请勿喷
4.可以多个transactionManager,名字随意

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/tool
        http://www.springframework.org/schema/tool/spring-tool.xsd
        http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<context:annotation-config />
<!-- 自动扫描 -->
<context:component-scan base-package="com,com.baseSystem.web.*" />
<!-- 强制使用CGLIB代理  -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!--连接池配置-->
<context:property-placeholder location="classpath:jdbc/db-config.properties" />

<!-- oracle数据库 -->

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值