求问 springmvc+hibernate 怎么样配置多个数据库,使用时怎么注入??

WaterBai 2016-02-06 11:04:31
现在链接一个数据库是这样配置的:

hibernate.cfg.xml配置:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/mydb
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
localhost_mysql_mydb
</property>
<mapping resource="com/pojo/User.hbm.xml" />
<mapping resource="com/pojo/Rolegroup.hbm.xml" />
<mapping resource="com/pojo/Menu.hbm.xml" />
<mapping resource="com/pojo/Role.hbm.xml" />
</session-factory>

</hibernate-configuration>


Spring配置springmvc-servlet.xml:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="com.controller,com.dao,com.service" />

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>


现在想要配置两个数据库,都是MySQL的数据库。怎么样配置??sessionFactory又要用??

刚开始学,觉得好难,有没有人帮忙看下~~
...全文
552 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueboz2 2016-02-07
  • 打赏
  • 举报
回复
这个简单,只需要@Repository("sessionFactory1"),@Repository("sessionFactory2"),但是只能用其中的一个,不能同时使用哦

@Repository("这里填写注入的ID")
public class UserDao extends HibernateDaoSupport{
     
    @Resource
    public void setMySessionFactory(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }
     
    @SuppressWarnings("unchecked")
    public User getUserbyId(String userId) {
        SQLQuery query=this.getSession().createSQLQuery("select * from User where userid=?");
        query.addEntity(User.class);
        query.setString(0, userId); 
        List<User> resultList=query.list();
        if(resultList != null ){
            User results = resultList.get(0);
            return results;
        }
        return null;
    }
}
WaterBai 2016-02-06
  • 打赏
  • 举报
回复
引用 4 楼 blueboz 的回复:
这个其实也不是很难,主要原理是,sessionFactory和dataSource sessionFactory主要配置数据库的链接参数,dataSource主要配置使用数据源C3P0,DBCP 如果想要链接多个数据库,切入点就在于配置多个sessionFactory,但是id不能一样


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
</bean>

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate2.cfg.xml</value>
		</property>
</bean>
我现在数据库访问层是这样写的: UserDao:

@Repository("userDao")
public class UserDao extends HibernateDaoSupport{
	
	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	public User getUserbyId(String userId) {
		SQLQuery query=this.getSession().createSQLQuery("select * from User where userid=?");
		query.addEntity(User.class);
        query.setString(0, userId); 
        List<User> resultList=query.list();
        if(resultList != null ){
        	User results = resultList.get(0);
        	return results;
        }
        return null;
	}
}
我要怎么样用配置的两个sessionFactory???
WaterBai 2016-02-06
  • 打赏
  • 举报
回复
引用 4 楼 blueboz 的回复:
这个其实也不是很难,主要原理是,sessionFactory和dataSource sessionFactory主要配置数据库的链接参数,dataSource主要配置使用数据源C3P0,DBCP 如果想要链接多个数据库,切入点就在于配置多个sessionFactory,但是id不能一样


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
</bean>

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate2.cfg.xml</value>
		</property>
</bean>
这样配置之后,再Dao类里面要怎么注入sessionFactory??
WaterBai 2016-02-06
  • 打赏
  • 举报
回复
引用 3 楼 qq_32352205 的回复:
[quote=引用 2 楼 qq_21992489 的回复:] 老问题了 直接给你 飞机票吧 大神多年之前就有答案了 http://blog.csdn.net/oracle_microsoft/article/details/4466810 http://bbs.csdn.net/topics/320191834
http://blog.csdn.net/oracle_microsoft/article/details/4466810 这个里面的方法,这样写就不用写hibernate.cfg.xml配置了??全部配置在Spring里面?? 这样写就是在spring配置文件里面,把数据库连接注入到Dao里面,然后Dao的方法可以不用改变,直接就能用对应数据库的sessionFactory,这样的么??[/quote] 现在一个数据库配置成这样,但是启动tomcat的时候会报错 <?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.controller,com.dao,com.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/mydb"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>com/pojo/User.hbm.xml</value> <value>com/pojo/Rolegroup.hbm.xml</value> <value>com/pojo/Menu.hbm.xml</value> <value>com/pojo/Role.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> <!-- <property name="configLocation" value="classpath:hibernate-Test.cfg.xml"/> --> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
blueboz2 2016-02-06
  • 打赏
  • 举报
回复
这个其实也不是很难,主要原理是,sessionFactory和dataSource sessionFactory主要配置数据库的链接参数,dataSource主要配置使用数据源C3P0,DBCP 如果想要链接多个数据库,切入点就在于配置多个sessionFactory,但是id不能一样


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
</bean>

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate2.cfg.xml</value>
		</property>
</bean>
WaterBai 2016-02-06
  • 打赏
  • 举报
回复
引用 2 楼 qq_21992489 的回复:
老问题了 直接给你 飞机票吧 大神多年之前就有答案了 http://blog.csdn.net/oracle_microsoft/article/details/4466810 http://bbs.csdn.net/topics/320191834
http://blog.csdn.net/oracle_microsoft/article/details/4466810 这个里面的方法,这样写就不用写hibernate.cfg.xml配置了??全部配置在Spring里面?? 这样写就是在spring配置文件里面,把数据库连接注入到Dao里面,然后Dao的方法可以不用改变,直接就能用对应数据库的sessionFactory,这样的么??
qq_21992489 2016-02-06
  • 打赏
  • 举报
回复
老问题了 直接给你 飞机票吧 大神多年之前就有答案了 http://blog.csdn.net/oracle_microsoft/article/details/4466810 http://bbs.csdn.net/topics/320191834

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧