`
hyshucom
  • 浏览: 830231 次
文章分类
社区版块
存档分类
最新评论

初学S2SH搭建(二)

 
阅读更多

一、Srping Hibnernate 的整合

4)开发

这里采用自底向上的开发模式。

a)底层的开发

打开DB Explorer,连接上数据库,找到我们所需要的表,然后点击右键选择【Hibernate reverse Engineering

Id Generator选择native,如果是Oracle可以选择increment




生成的代码如下:Employee.java

package cn.zhang.crm.model.pojo;

/**

*Employeeentity.@authorMyEclipsePersistenceTools

*/

publicclass Employee implements java.io.Serializable {

privatestaticfinallongserialVersionUID = 4740966089797222237L;

private Integer id;

private String name;

private String address;

private String phone;

// Constructors

/**defaultconstructor*/

public Employee() {

}

/**fullconstructor*/

public Employee(String name, String address, String phone) {

this.name = name;

this.address = address;

this.phone = phone;

}

// Property accessors

public Integer getId() {

returnthis.id;

}

publicvoid setId(Integer id) {

this.id = id;

}

public String getName() {

returnthis.name;

}

publicvoid setName(String name) {

this.name = name;

}

public String getAddress() {

returnthis.address;

}

publicvoid setAddress(String address) {

this.address = address;

}

public String getPhone() {

returnthis.phone;

}

publicvoid setPhone(String phone) {

this.phone = phone;

}

}

Employee.hbm.xml

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

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

<class name="cn.zhang.crm.model.pojo.Employee" table="employee" catalog="test">

<id name="id" type="java.lang.Integer">

<column name="id" />

<generator class="native"></generator>

</id>

<property name="name" type="java.lang.String">

<column name="name" />

</property>

<property name="address" type="java.lang.String">

<column name="address" />

</property>

<property name="phone" type="java.lang.String">

<column name="phone" />

</property>

</class>

</hibernate-mapping>

同时我们需要将Employee.hbm.xml的路径放到applicationContext中。

<property name="mappingResources">

<list>

<value>cn/zhang/crm/model/pojo/Employee.hbm.xml</value>

</list>

</property>

bDAO接口

实现CRUD方法,具体的EmployeeDao.java代码如下:

package cn.zhang.crm.dao;

import java.util.List;

import cn.zhang.crm.model.pojo.Employee;

publicinterface EmployeeDao {

publicvoid add(Employee e);

public Employee getEmployee(int id);

public List getEmployees();

publicvoid update(Employee e);

publicvoid delete(int id);

}

c)实现DAO接口方法

实现EmployeeDao接口方法,同时要继承HibernateDaoSupport类,EmployeeDaoImpl.java的具体代码如下:

package cn.zhang.crm.dao.hibernate;

import java.util.List;

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

import cn.zhang.crm.dao.EmployeeDao;

import cn.zhang.crm.model.pojo.Employee;

public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {

public void add(Employee e) {

getHibernateTemplate().save(e);

}

public void delete(int id) {

getHibernateTemplate().delete(getEmployee(id));

}

public Employee getEmployee(int id) {

return (Employee)getHibernateTemplate().get(Employee.class, id);

}

public List getEmployees() {

//这里一定要写Employee,千万不要误写成employee,因为Hibernate是从对象中读取数据的

return getHibernateTemplate().find("from Employee");

}

public void update(Employee e) {

getHibernateTemplate().update(e);

}

}

dservice(或businessManager服务层,主要是事务处理,利用的是代码生成技术(具体作用是什么现在还不是很清楚,在今后的学习中再好好的体会)

EmployeeManager.java(它是一个接口)代码如下:

package cn.zhang.crm.service;

import java.util.List;

import cn.zhang.crm.model.pojo.Employee;

publicinterface EmployeeManager {

publicvoid addEmployee(Employee e);

public Employee getEmployee(int id);

public List listEmployee();

publicvoid updateEmployee(Employee e);

publicvoid deleteEmployee(int id);

}

实现这个接口EmployeeManagerImpl.java

package cn.zhang.crm.service.impl;

import java.util.List;

import cn.zhang.crm.dao.EmployeeDao;

import cn.zhang.crm.model.pojo.Employee;

import cn.zhang.crm.service.EmployeeManager;

publicclass EmployeeManagerImpl implements EmployeeManager {

private EmployeeDao employeeDao;

publicvoid setEmployeeDao(EmployeeDao employeeDao) {

this.employeeDao = employeeDao;

}

publicvoid addEmployee(Employee e) {

employeeDao.add(e);

}

publicvoid deleteEmployee(int id) {

employeeDao.delete(id);

}

public Employee getEmployee(int id) {

returnemployeeDao.getEmployee(id);

}

public List listEmployee() {

returnemployeeDao.getEmployees();

}

publicvoid updateEmployee(Employee e) {

employeeDao.update(e);

}

}

上述完成后,我们就需要在spring中注册类了。打开applicationContext.xml,然后注册<bean>

增加的代码如下:

<bean id="employeeDao"

class="cn.zhang.crm.dao.hibernate.EmployeeDaoHibernate">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

<bean id="employeeManager"

class="cn.zhang.crm.service.impl.EmployeeManagerImpl">

<property name="employeeDao">

<ref bean="employeeDao" />

</property>

</bean>

到此完成了SpringHibernate的整合。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics