提供源碼,下載地址:http://download.csdn.net/detail/t_jl1979/7920137
1.導入的jar包.
- hibernate-4.3.5的required包中的所有
- optional包中的c3p0中的所有
- 下載slf4j,導入slf4j-api.jar 和 slf4j-nop.jar
- 導入mysql-connector-java.jar
- log4j用的1.2.17,低版本因爲缺少TRACE類函數,會報錯。
2.包結構
爲便於測試,均放於一個目錄下
/tmp/Student.java
/tmp/StudentDao.java
/tmp/TestSpringHibernate.java
/tmp/applicationContext.xml
3.數據模型類Student.java
<span style="font-family:Microsoft YaHei;font-size:14px;">package tmp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Student {
private int sid; //學號
private String sname; //姓名
public Student() {
}
@Id
@GeneratedValue
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + "]";
}
}
</span>
4.數據庫操作類
<span style="font-family:Microsoft YaHei;font-size:14px;">package tmp;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
public class StudentDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public void saveStudent(Student s) throws SQLException {
Session session = sessionFactory.getCurrentSession();
session.save(s);
}
public Student queryStudent(String sid) {
return null;
}
@Transactional
@SuppressWarnings("unchecked")
public List<Student> listStudent() {
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Student.class);
return criteria.list();
}
}</span>
5.hiberate spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre"> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre"> </span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre"> </span>xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre"> </span>xmlns:tx="http://www.springframework.org/schema/tx"
<span style="white-space:pre"> </span>xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.sam.web" />
<aop:aspectj-autoproxy />
<!--配置數據源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="xx" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan">
<list>
<value>tmp</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!--事務管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--Annotation方式事務管理 -->
<tx:annotation-driven transaction-manager="txManager" />
<bean id="StudentDao" class="tmp.StudentDao"></bean>
</beans>
6 測試類
package tmp;
import java.sql.SQLException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringHibernate {
private StudentDao dao;
private ApplicationContext context;
@Before
public void setUp() throws Exception {
context = new ClassPathXmlApplicationContext(
"/tmp/applicationContext.xml");
setDao((StudentDao) context.getBean("StudentDao"));
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSave() throws SQLException {
Student s = new Student();
s.setSname("sam");
getDao().saveStudent(s);
}
@Test
public void testQuery() throws SQLException {
System.out.println(getDao().listStudent());
}
public StudentDao getDao() {
return dao;
}
public void setDao(StudentDao dao) {
this.dao = dao;
}
}