1. 程式人生 > >Spring中整合Hibernate連線MySQL資料庫配置

Spring中整合Hibernate連線MySQL資料庫配置

Spring配置檔案applicationContext.xml中加入資料庫連線配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">t3://127.0.0.1:7001</prop>
<prop key="java.naming.security.principal">weblogic</prop>
<prop key="java.naming.security.credentials">weblogic</prop>
</props>
</property>
</bean>
<!--利用Weblogic配置資料庫jndi連線-->
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>MySQLDataSource</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate" />
</property>
</bean>
<!--直接配置資料庫連線-->
< bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="jndiDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
</props>
</property>
<!-- property name="mappingResources">
<list>
<value>cn/git/common/hbm/UserInfo.hbm.xml</value>
</list>
</property-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/cn/git/common/hbm</value>
</list>
</property>
<!--配置Spring中的Hibernate模板類-->
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--在類中直接呼叫hibernate模板類來訪問資料庫-->
<bean id="systemLoginDAO" class="cn.git.systemLogin.dao.hibernate.SystemLoginDAO">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
</beans>

配置完成後即可在systemLoginDAO類中直接訪問資料庫了:)
package cn.git.systemLogin.dao.hibernate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.classic.Session;
import cn.git.systemLogin.dao.ISystemLoginDAO;
public class SystemLoginDAO extends HibernateDaoSupport implements
ISystemLoginDAO ...{
private Log log=LogFactory.getLog(this.getClass());

public boolean checkUserPassword(String userName,String userPassword)...{

Session session=getHibernateTemplate().getSessionFactory().openSession();
Connection conn = session.connection();
String query="select * from userInfo where userName=? and userPassword=?";
try...{
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1,userName);
pstmt.setString(2,userPassword);
ResultSet rs = pstmt.executeQuery();
if(rs.next())...{
return true;
}else...{
return false;
}
}
catch(Exception e)...{
return false;

}finally...{
session.close();
}

}
}