原始注入sessionFactory:
package org.ssh.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.ssh.dao.StudentDao;
import org.ssh.model.Student;
public class StudentDaoImpl implements StudentDao {
private SessionFactory sessionFactory;
public void save(Student student) {
Session session = this.sessionFactory.openSession();
Transaction tc = session.beginTransaction();
session.save(student);
tc.commit();
}
/**
* @return the sessionFactory
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* @param sessionFactory
* the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
spring中的配置:-------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentDao" class="org.ssh.dao.impl.StudentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="studentService" class="org.ssh.service.StudentService">
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>
-----------------------------------------使用HibernateTemplate--------------------------------
首先在AppliactionContext文件中注入bean
<!-- 将sessionFactory交给HibernateTemplate 管理 给注入进来 -->
<bean id="HibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
简化后的dao:--------------------------------
package org.ssh.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.ssh.dao.StudentDao;
import org.ssh.model.Student;
public class StudentDaoImpl implements StudentDao {
/**
* 使用HibernateTemplate简化代码
* 在HibernateTemplate中spring为我们封装了其所有方法
* session也不用去管理 非常好用
* */
private HibernateTemplate hibernateTemplate;
public void save(Student student) {
this.hibernateTemplate.save(student);
}
/**
* @return the hibernateTemplate
*/
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
/**
* @param hibernateTemplate the hibernateTemplate to set
*/
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
// private SessionFactory sessionFactory;
//
// public void save(Student student) {
// Session session = this.sessionFactory.openSession();
//
// Transaction tc = session.beginTransaction();
//
// session.save(student);
//
// tc.commit();
//
// }
//
// /**
// * @return the sessionFactory
// */
// public SessionFactory getSessionFactory() {
// return sessionFactory;
// }
//
// /**
// * @param sessionFactory
// * the sessionFactory to set
// */
// public void setSessionFactory(SessionFactory sessionFactory) {
// this.sessionFactory = sessionFactory;
// }
}
----------------spring 配置文件中注入---------------hibernateTemplate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentDao" class="org.ssh.dao.impl.StudentDaoImpl">
<property name="hibernateTemplate " ref="hibernateTemplate "/>
</bean>
<bean id="studentService" class="org.ssh.service.StudentService">
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>