Spring整合Hibernate

本文介绍如何使用Spring框架与Hibernate整合,通过配置bean.xml文件、db.properties文件、实体类Dept.java、映射文件Dept.hbm.xml以及DAO层实现对象关系映射,完成数据库操作。

bean.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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${driverClass}"/>  
        <property name="jdbcUrl" value="${jdbcUrl}"/>  
        <property name="user" value="${user}"/>  
        <property name="password" value="${password}"/>  
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
                <value>com/mapping/Dept.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
            </value>
        </property>
    </bean>
    
    <bean id="deptDao" class="com.dao.DeptDAOHibernateImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
 </beans>
db.properties:

driverClass=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
user=scott
password=tiger
Dept.java :

public class Dept {
	
	private int deptno;
	private String dname;
	private String loc;
	
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	
}
Dept.hbm.xml :

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.pojo">

    <class name="Dept" table="dept">
        <id name="deptno">
            <generator class="native"/>
        </id>
        <property name="dname" type="string">
            <column name="dname"/>
        </property>
        <property name="loc" type="string">
            <column name="loc"/>
        </property>
    </class>

</hibernate-mapping>
DeptDAO.java :

public interface DeptDAO {
	public void save(Dept dept);
	public void delete(int deptno);
	public void update(Dept dept);
	public Dept findById(int deptno);
	public List<Dept> findAll();
}
DeptDAOHibernateImpl.java :

public class DeptDAOHibernateImpl extends HibernateDaoSupport implements
		DeptDAO {

	@Override
	public void delete(int deptno) {
		this.getHibernateTemplate().delete(findById(deptno));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dept> findAll() {
		return this.getHibernateTemplate().find("from Dept");
	}

	@Override
	public Dept findById(int deptno) {
		return (Dept) this.getHibernateTemplate().get(Dept.class,deptno);
	}

	@Override
	public void save(Dept dept) {
		this.getHibernateTemplate().save(dept);
	}

	@Override
	public void update(Dept dept) {
		this.getHibernateTemplate().update(dept);
	}

}
Test.java :

public class Test {
	
	public static void main(String[] args) {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
		DeptDAO dao=(DeptDAO) ctx.getBean("deptDao");
		List<Dept> depts=dao.findAll();
		for(Dept dept:depts){
			System.out.println(dept.getDeptno()+" "+dept.getDname()+" "+dept.getLoc());
		}
	}
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值