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=tigerDept.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());
}
}
}
本文介绍如何使用Spring框架与Hibernate整合,通过配置bean.xml文件、db.properties文件、实体类Dept.java、映射文件Dept.hbm.xml以及DAO层实现对象关系映射,完成数据库操作。
2845

被折叠的 条评论
为什么被折叠?



