zk+spring+hibernate项目配置实例,参考官网

http://yinger-fei.iteye.com/blog/1020999

 

在网上找过很多例子,最后发现官网的这个例子不错。但是过程中出了很多问题,所以记录下来,希望对后来人也有所帮助吧。参考网页:http://www.zkoss.org/smalltalks/hibnsprn/hibn_sprn_zk.html

 

配置环境:zk +spring 3.0.5.RELEASE + hibernate3.2

 

1.新建zk project,然后在WEB-INF下引入spring 3.0.5的jar包。因为运行的时候报错了,发现需要一个spring-dao.jar,因为spring 3.0.5 release 中没有,所以需要下载。

 

2.引入hibernate的 jar包,所有。这个就不用说了吧 嘻嘻

 

3.在WEB-INF下,建立applicationContext.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--  ApplicationContext.xml -->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://127.0.0.1:3306/friend_db</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>123456</value>
        </property>
    </bean>
    <bean id="factory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
                <value>org/zk101/model/pojo/Person.hbm.xml</value>                
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                  org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">
                  false
                </prop>
                <prop key="hibernate.transaction.factory_class">
                  org.hibernate.transaction.JDBCTransactionFactory
                </prop>
            </props>
        </property>
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    <bean id="personDao" class="org.zk101.dbaccess.dao.PersonDAO">
        <property name="sessionFactory">
            <ref bean="factory" />
        </property>
    </bean>
    <bean id="personManager" class="org.zk101.service.PersonManagerImpl">
        <property name="dao">
            <ref bean="personDao"/>
        </property> 
    </bean>

</beans>

 

4. Person.hbm.xml

 

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

<hibernate-mapping 
 package="org.zk101.model.pojo">
    <class name="Person" table="person" lazy="false">
        <id name="id" type="int">
               <generator class="increment"/>
        </id>
        <property name="name" />
        <property name="surname"/>        
    </class>
</hibernate-mapping>

 

5. Person.java

 

package org.zk101.model.pojo;

public class Person {

 private int id ;
    private String name;
    private String surname;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSurname() {
  return surname;
 }
 public void setSurname(String surname) {
  this.surname = surname;
 }

}

 

6 PersonDAO.java

 

package org.zk101.dbaccess.dao;

import java.util.List;

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

public class PersonDAO extends HibernateDaoSupport {

    public void saveOrUpdate(Object ob) {
        super.getHibernateTemplate().saveOrUpdate(ob);
        
    }
    
    public void delete(Object ob) {
        super.getHibernateTemplate().delete(ob);
    }
    
    @SuppressWarnings("unchecked")
 public Object find(Class clazz, Long id) {
        Object ob =  super.getHibernateTemplate().load(clazz,id);
        return ob;
    }

    @SuppressWarnings("unchecked")
 public List findAll(Class clazz) {
        List list = super.getHibernateTemplate().find(" from "+clazz.getName());
        return list;
    }


}

 

7. PersonManager Interface

 

/** PersonManager Interface */
package org.zk101.service;

import org.zk101.model.pojo.Person;

public interface PersonManager {

    public List getAllPersons();
    /** for simplicity we are only providing a simple business rule method **/
}

 

 

8. PersonManagerImpl.java

 

/** PersonManagerImpl.java */
package org.zk101.service;

import org.zk101.model.pojo.Person;
import java.util.*;

public class PersonManagerImpl implements PersonManager {

    private PersonDAO dao;

    public PersonDAO getDao() {
        return this.dao;
    }

    public void setDao(PersonDAO personDAO) {
        this.dao = personDAO;
    }

    public List getAllPersons() {
        return this.dao.findAll(Person.class);
    }
}

9. ServiceLocator.java

 

/** ServiceLocator.java */
package org.zk101.service;

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

import org.zk101.service.PersonManager;

public class ServiceLocator {
    
    private static ApplicationContext ctx;
    
    static {

//注意红色部分,此处最容易报错。因为我的这个配置文件是位于web-inf下的,所以相对于classes的位置就是这样。
        ctx = new ClassPathXmlApplicationContext("../applicationContext.xml");
    }
    
    private ServiceLocator() {
    }
    
    public static SessionFactory getSessionFactory() {
        return (SessionFactory) ctx.getBean("factory",SessionFactory.class);
    }

    public static PersonManager getPersonManager() {
        return (PersonManager) ctx.getBean("personManager",PersonManager.class);
    }
}

 

 

10. person.zul

 

<!-- person.zul -->
<vbox>
  <listbox id="personList" width="800px" rows="5"
   use="org.zk101.ui.ListPerson">
    <listhead>
      <listheader label="Name"/>
      <listheader label="Surname"/>
    </listhead>
  </listbox> 
</vbox>

 

 

11 ListPerson.java

 

/** ListPerson.java */
package org.zk101.ui;

import org.zk101.service.ServiceLocator;
import java.util.*;
import org.zk101.service.PersonManager;
import org.zk101.model.pojo.Person;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;

public class ListPerson extends Listbox {

    public void onCreate() {
        PersonManager manager = ServiceLocator.getPersonManager();
        Collection allPersons = manager.getAllPersons();
        Iterator it = allPersons.iterator();
        while(it.hasNext()) {
            Person person = (Person) it.next();
            Long id = person.getId();
            String name = person.getName();
            String surname = person.getSurname();       

            Listitem listitem = new Listitem();
            listitem.setValue(id);
            listitem.setParent(this);

            Listcell nameCell = new Listcell(name);
            nameCell.setParent(listitem);
            Listcell surnameCell = new Listcell(surname);
            surnameCell.setParent(listitem);
        }
     }
}

 总结:这个实例,就是通过整合,把数据库中存在的记录遍历并显示出来。其他的操作可以在其基础上实现。比如删除。

目录结构:zk+spring+hibernate项目配置实例,参考官网

你可能感兴趣的:(Hibernate)