❶ springmvc spring hibernate 怎麼配置連接兩個資料庫
persistent.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="userPU" transaction-type="RESOURCE_LOCAL">
<!--jpa的提供者-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!--聲明資料庫連接的驅動-->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!--jdbc資料庫的連接地址-->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/manager?characterEncoding=gbk"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<!--配置方言-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<!--激活查詢日誌功能-->
<property name="hibernate.show_sql" value="true"/>
<!--優雅地輸出Sql-->
<property name="hibernate.format_sql" value="true"/>
<!--添加一條解釋型標注-->
<property name="hibernate.use_sql_comments" value="false"/>
<!--配置如何根據java模型生成資料庫表結構,常用update,validate-->
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
applicationContext.xml配置
<!--第二步-->
<!--定義實體的工廠bean-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.">
<property name="persistenceUnitName" value="userPU" />
<property name="persistenceXmlLocation" value="classpath:persistence.xml"></property>
</bean>
<!--第三步-->
<!--定義事務管理器-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>