搭建好SSH的组合环境,简单的开发一个东西,一个页面输入用户的编号和用户名,年龄来进行页面的跳转,还用查询所有的对象信息。
安装Hibernate的真正意义是不用手动建数据库表的,只需要创建好实体类就通过Hibernate提供的
1.在MySql数据库中先创建一个数据库
2.在数据库ssha中创建一张表emps表
- create table emps
- (
- empId int auto_increment primary key,
- empName varchar(29) not null,
- empAge int not null
- )
3.数据库中插入数据
4.使用MyEclipse生成实体类
- package xiao.zhang.bean;
- /**
- * Emps entity.
- *
- * @author MyEclipse Persistence Tools
- */
- public class Emps implements java.io.Serializable {
- // Fields
- private Integer empId;
- private String empName;
- private Integer empAge;
- // Constructors
- /** default constructor */
- public Emps() {
- }
- /** minimal constructor */
- public Emps(String empName) {
- this.empName = empName;
- }
- /** full constructor */
- public Emps(String empName, Integer empAge) {
- this.empName = empName;
- this.empAge = empAge;
- }
- // Property accessors
- public Integer getEmpId() {
- return this.empId;
- }
- public void setEmpId(Integer empId) {
- this.empId = empId;
- }
- public String getEmpName() {
- return this.empName;
- }
- public void setEmpName(String empName) {
- this.empName = empName;
- }
- public Integer getEmpAge() {
- return this.empAge;
- }
- public void setEmpAge(Integer empAge) {
- this.empAge = empAge;
- }
- }
5.Emps.hbm.xml文件信息如下:
- <hibernate-mapping>
- <class name="xiao.zhang.bean.Emps" table="emps" catalog="ssha">
- <id name="empId" type="java.lang.Integer">
- <column name="empId" />
- <generator class="native" />
- </id>
- <property name="empName" type="java.lang.String">
- <column name="empName" length="20" not-null="true" />
- </property>
- <property name="empAge" type="java.lang.Integer">
- <column name="empAge" />
- </property>
- </class>
在Spring配种文件中配置实体类的映射文件,这一部分属于Hibernate和Spring的组合:
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- </props>
- </property>
- <!--*********************************************************-->
- <property name="mappingResources">
- <list>
- <value>xiao/zhang/bean/Emps.hbm.xml</value>
- </list>
- </property>
- <!--*********************************************************-->
- </bean>
*星号括起的部分信息。
6.生成Emps的DAO
- public class EmpsDAO extends HibernateDaoSupport implements ImpEmpsDao {
- private static final Log log = LogFactory.getLog(EmpsDAO.class);
- // property constants
- public static final String EMP_NAME = "empName";
- public static final String EMP_AGE = "empAge";
- protected void initDao() {
- // do nothing
- }
- public void save(Emps transientInstance) {
- log.debug("saving Emps instance");
- try {
- getHibernateTemplate().save(transientInstance);
- log.debug("save successful");
- } catch (RuntimeException re) {
- log.error("save failed", re);
- throw re;
- }
- }
- public void delete(Emps persistentInstance) {
- log.debug("deleting Emps instance");
- try {
- getHibernateTemplate().delete(persistentInstance);
- log.debug("delete successful");
- } catch (RuntimeException re) {
- log.error("delete failed", re);
- throw re;
- }
- }
- public Emps findById(java.lang.Integer id) {
- log.debug("getting Emps instance with id: " + id);
- try {
- Emps instance = (Emps) getHibernateTemplate().get(
- "xiao.zhang.bean.Emps", id);
- return instance;
- } catch (RuntimeException re) {
- log.error("get failed", re);
- throw re;
- }
- }
- public List findByExample(Emps instance) {
- log.debug("finding Emps instance by example");
- try {
- List results = getHibernateTemplate().findByExample(instance);
- log.debug("find by example successful, result size: "
- + results.size());
- return results;
- } catch (RuntimeException re) {
- log.error("find by example failed", re);
- throw re;
- }
- }
- public List findByProperty(String propertyName, Object value) {
- log.debug("finding Emps instance with property: " + propertyName
- + ", value: " + value);
- try {
- String queryString = "from Emps as model where model."
- + propertyName + "= ?";
- return getHibernateTemplate().find(queryString, value);
- } catch (RuntimeException re) {
- log.error("find by property name failed", re);
- throw re;
- }
- }
- public List findByEmpName(Object empName) {
- return findByProperty(EMP_NAME, empName);
- }
- public List findByEmpAge(Object empAge) {
- return findByProperty(EMP_AGE, empAge);
- }
- public List findAll() {
- log.debug("finding all Emps instances");
- try {
- String queryString = "from Emps";
- return getHibernateTemplate().find(queryString);
- } catch (RuntimeException re) {
- log.error("find all failed", re);
- throw re;
- }
- }
- public Emps merge(Emps detachedInstance) {
- log.debug("merging Emps instance");
- try {
- Emps result = (Emps) getHibernateTemplate().merge(detachedInstance);
- log.debug("merge successful");
- return result;
- } catch (RuntimeException re) {
- log.error("merge failed", re);
- throw re;
- }
- }
- public void attachDirty(Emps instance) {
- log.debug("attaching dirty Emps instance");
- try {
- getHibernateTemplate().saveOrUpdate(instance);
- log.debug("attach successful");
- } catch (RuntimeException re) {
- log.error("attach failed", re);
- throw re;
- }
- }
- public void attachClean(Emps instance) {
- log.debug("attaching clean Emps instance");
- try {
- getHibernateTemplate().lock(instance, LockMode.NONE);
- log.debug("attach successful");
- } catch (RuntimeException re) {
- log.error("attach failed", re);
- throw re;
- }
- }
- public static EmpsDAO getFromApplicationContext(ApplicationContext ctx) {
- return (EmpsDAO) ctx.getBean("EmpsDAO");
- }
- }
将数据访问对象交由Spring容器管理,在Spring的配置文件中配置该类的信息
- <!-- 数据库访问对象 -->
- <bean id="EmpsDAO" class="xiao.zhang.dao.imp.EmpsDAO">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
7.为了实现面向接口编程,同时能够将业务逻辑部分与数据库访问部分的代码进行解耦合。下面是文件树图:
8.接下来应该是手工编程,这样利于代码的控制。
9.根据7中已经对业务和数据访问进行的了分离,下面是分离后的代码:
|
上面是数据访问的接口,这样就可以面向接口编程
10.下面是业务逻辑层的代码:
先定义业务处理的接口:
- /**
- *
- * 实现了业务逻辑的封装,将业务操作和数据操作分离
- *
- * @author aiilive
- *
- */
- public interface ImpEmpsService {
- public void addEmps(Emps transientInstance);
- public void deleteEmpsById(int empId);
- public List findAll();
- public Emps getEmpsById(int empId);
- }
实现业务逻辑:
- import java.util.List;
- import xiao.zhang.bean.Emps;
- import xiao.zhang.dao.ImpEmpsDao;
- import xiao.zhang.service.ImpEmpsService;
- public class EmpsServiceImp implements ImpEmpsService {
- private ImpEmpsDao empsDao;
- public void addEmps(Emps transientInstance) {
- empsDao.save(transientInstance);
- }
- public void deleteEmpsById(int empId) {
- Emps emps = empsDao.findById(empId);
- if (emps == null) {
- return;
- }
- empsDao.delete(emps);
- }
- public List findAll() {
- return empsDao.findAll();
- }
- public Emps getEmpsById(int empId) {
- return empsDao.findById(empId);
- }
- public ImpEmpsDao getEmpsDao() {
- return empsDao;
- }
- public void setEmpsDao(ImpEmpsDao empsDao) {
- this.empsDao = empsDao;
- }
- }
在实现业务逻辑中有数据访问对象的实例属性,在Spring的配置文件中应该配置,来实现交由Spring容器对业务逻辑处理类的对象的管理
- <!-- 业务处理对象 -->
- <bean id="EmpsService"
- class="xiao.zhang.service.imp.EmpsServiceImp">
- <property name="empsDao">
- <ref bean="EmpsDAO" />
- </property>
- </bean>
这样就将业务逻辑层和数据访问层分开,这一部分主要是Hibernate和Spring的组合使用。