Hibernate与Spring整合(懒人专用)

本文详细介绍了使用Hibernate和Spring框架整合的过程,包括导入必要的jar包、创建model类、配置Hibernate和Spring的相关文件等内容,并提供了完整的测试示例。

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

写了两天的Hibernate,到最后是数据库的链接写错了!怨呀!废话不多说,进入主题。


1. 导入jar包。用迅雷下载jar:http://dl2.cr173.com//soft3/s2sh_jar.zip

2. model类Student(在com.zjh.model下):

package com.zjh.model;

public class Student {

	private int id;
	private String name;
	private String computerMac;
	private int num;
	private char sex;
	private int age;

	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Student() {
	}

	public Student(String name, String computerMac, int num) {
		this.name = name;
		this.computerMac = computerMac;
		this.num = num;
	}
}

3. 在com.zjh.model下建立Student对应的映射文件

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.zjh.model.Student" table="student">
		<id name="id" column="id" type="int">
			<generator class="identity"/>
		</id>

		<property name="num" not-null="true" />
		<property name="computerMac" not-null="true" />
		<property name="name" not-null="true" />
		<property name="sex" />
		<property name="age" />

	</class>
</hibernate-mapping>

4. 在根目录下建立hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<mapping resource="com/zjh/model/student.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

5. 接下来,是最重要的spring的配置文件spring.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ctx="http://www.springframework.org/schema/context"
	xmlns:http="http://servicemix.apache.org/http/1.0" xmlns:bar="http://testserver"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://servicemix.apache.org/http/1.0
http://servicemix.apache.org/schema/servicemix-http-3.2.3.xsd">

	<bean id="student" class="com.zjh.model.Student">
		<property name="num" value="20121181" />
		<property name="name" value="zjh" />
		<property name="computerMac" value="HJ-SDF-SD-SD"/>
	</bean>

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://localhost/test" />
		<property name="user" value="root" />
		<property name="password" value="root" />
		<property name="minPoolSize" value="5" />
		<property name="maxPoolSize" value="50" />
		<property name="maxIdleTime" value="1800" />
		<property name="acquireIncrement" value="2" />
		<property name="maxStatements" value="0" />
		<property name="initialPoolSize" value="10" />
		<property name="idleConnectionTestPeriod" value="30" />
		<property name="acquireRetryAttempts" value="30" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocations">
			<list>
				<value>classpath:hibernate.cfg.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.use_sql_comments">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>

	<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<bean id="hibernateTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>

	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager">
			<ref bean="hibernateTransactionManager" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>
<!-- 
	<bean id="hibernateSupport" class="com.zjh.dao.HibernateSupport">
		<property name="hibernateTemplate" ref="hibernateTemplete" />
	</bean>
 -->
	<bean id="baseDao" class="com.zjh.dao.BaseDao" abstract="true"
		 >
		<property name="hibernateTemplate" ref="hibernateTemplete" />
		 </bean>

	<bean id="studentDao" class="com.zjh.dao.StudentDao" parent="baseDao">
	</bean>
</beans>

6.从上面的配置文件可以看出,还有两个自定义的类

BaseDao、StudentDao

我把它们放在model.zjh.dao下面

BaseDao

package com.zjh.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class BaseDao extends HibernateDaoSupport {
	public void delete(Object object) {
		super.getHibernateTemplate().delete(object);
	}

	public void add(Object object) {
		super.getHibernateTemplate().save(object);
	}

	public void update(Object boject) {
		super.getHibernateTemplate().update(boject);
	}

	public List findByParameter(String para, String value,Object object) {
		String string = "from " + object.getClass()+" as object where " +para + "="+ value;
		return super.getHibernateTemplate().find(string);
	}
}

StudentDao

package com.zjh.dao;

import java.util.List;

import com.zjh.model.Student;

public class StudentDao extends BaseDao {
//	private Student student;
//
//	public Student getStudent() {
//		return student;
//	}
//
//	public void setStudent(Student student) {
//		this.student = student;
//	}
//	
	public void delete(Student student) {
		super.delete(student);
	}

	public void add(Student student) {
		super.add(student);
	}

	public void update(Student student) {
		super.update(student);
	}

	public List findByParameter(String para, String value,Student object) {
		String string = "from " + object.getClass()+" as object where " +para + "="+ value;
		return super.getHibernateTemplate().find(string);
	}
}


7. 好的,下面就只剩下测试了(在com.zjh.test下面建Test类)!

package com.zjh.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zjh.dao.StudentDao;
import com.zjh.model.Student;
import com.zjh.model.SuperClass;

public class Test {
	@org.junit.Test
	public void ObjectTest() {
		SuperClass d = new SuperClass() {
		};
		Integer i = new Integer(2);
		System.out.println(i instanceof Object);
		System.out.println(d instanceof Object);
	}

	@org.junit.Test
	public void HibernateAndSpringTest() {
		ApplicationContext spring = new ClassPathXmlApplicationContext(
				"spring.xml");
		Student student = (Student) spring.getBean("student");
		StudentDao studentDao = (StudentDao) spring.getBean("studentDao");
		studentDao.add(student);
	}
	
	public static void main(String[] args) {
		ApplicationContext spring = new ClassPathXmlApplicationContext(
				"spring.xml");
		Student student = (Student) spring.getBean("student");
		StudentDao studentDao = (StudentDao) spring.getBean("studentDao");
		studentDao.add(student);
	}
}


好了,大功告成!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值