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();
}
   
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值