hibernate-4.3.5 与Spring(Spring MVC4.0.4)注解方式集成

本文提供了一段基于 Spring 和 Hibernate 的完整数据库操作源码,包括数据模型类 Student.java、数据库操作类 StudentDao.java、hibernateSpring 配置文件 applicationContext.xml 以及测试类 TestSpringHibernate.java。详细介绍了如何导入依赖、配置数据源、实现数据持久化操作,并通过测试验证了功能的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提供源码,下载地址:http://download.youkuaiyun.com/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;
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值