ZK7+Spring4+Hibernate4框架整合并实现基本查询

框架环境组件版本:ZK 7.0.0 、Spring 4.0.6、Hibernate 4.2.2 。

第一次将这三个版本搭配到一起,组合的过程中,解决了很多次的jar包冲突,最终以下jar包可以完美契合。

ZK7所需jar包:点击下载ZK7所需jar包

Spring4所需jar包:点击下载Spring4所需jar包

Hibernate4所需jar包:点击下载Hibernate4所需jar包

下面开始整合,开发工具用的是Eclipse,数据库用的是MySQL:

1、新建ZK Project ,/WEB-INF/lib下会自动出现一些ZK项目所必需的jar包,不用管它。将下载的所有jar包都导入lib文件夹,然后Build Path。

2、在/WEB-INF下新建Spring的配置文件:applicationContext.xml,文件详细内容如下(注意修改数据库的参数):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
    <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/zsh_test</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>123456</value>
        </property>
    </bean>
    <bean id="factory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
                <value>com/zsh/model/User.hbm.xml</value>                
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                  org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">
                  true
                </prop>
                <prop key="hibernate.transaction.factory_class">
                  org.hibernate.transaction.JDBCTransactionFactory
                </prop>
            </props>
        </property>
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    <bean id="userDao" class="com.zsh.dao.UserDao">
        <property name="sessionFactory">
            <ref bean="factory" />
        </property>
    </bean>
    <bean id="userService" class="com.zsh.service.impl.UserServiceImpl">
        <property name="ud">
            <ref bean="userDao"/>
        </property> 
    </bean>
</beans>

3、修改/WEB-INF下,ZK项目的配置文件:web.xml,详细内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ZshTest</display-name>
  <listener>
  	<description>Used to cleanup when a session is destroyed</description>
  	<display-name>ZK Session cleaner</display-name>
  	<listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
  </listener>
  <servlet>
  	<description>
  	The ZK loader for ZUML pages</description>
  	<servlet-name>zkLoader</servlet-name>
  	<servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
  	<init-param>
  		<param-name>update-uri</param-name>
  		<param-value>/zkau</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet>
  	<description>
  	The asynchronous update engine for ZK</description>
  	<servlet-name>auEngine</servlet-name>
  	<servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>zkLoader</servlet-name>
  	<url-pattern>*.zul</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  	<servlet-name>zkLoader</servlet-name>
  	<url-pattern>*.zhtml</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  	<servlet-name>auEngine</servlet-name>
  	<url-pattern>/zkau/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>index.zul</welcome-file>
  </welcome-file-list>
</web-app>

4、新建Hibernate的配置文件:User.hbm.xml,详细内容如下:

<?xml version="1.0"?>
<!-- User.hbm.xml -->
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping 
 package="com.zsh.model">
    <class name="User" table="user" lazy="false">
        <id name="id" type="int">
               <generator class="increment"/>
        </id>
        <property name="name" />
        <property name="surname"/>        
    </class>
</hibernate-mapping>

5、实体类:User.java,详细代码如下:

package com.zsh.model;

public class User {
 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、业务层接口:UserService.java,详细代码如下:

package com.zsh.service;


import java.util.List;

import com.zsh.model.User;

public interface UserService {

    public List getAllUsers();
    
}

7、业务层的定位类:ServiceLocator.java,详细代码如下:

package com.zsh.service;

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

import com.zsh.service.UserService;

public class ServiceLocator {
    
    private static ApplicationContext ctx;
    
    static {
        ctx = new ClassPathXmlApplicationContext("../applicationContext.xml");
    }
    
    private ServiceLocator() {
    }
    
    public static SessionFactory getSessionFactory() {
        return (SessionFactory) ctx.getBean("factory",SessionFactory.class);
    }

    public static UserService getUserManager() {
        return (UserService) ctx.getBean("userService",UserService.class);
    }
}


8、业务层实现类:UserServiceImpl.java,详细代码如下:

package com.zsh.service.impl;

import com.zsh.dao.UserDao;
import com.zsh.model.User;
import com.zsh.service.UserService;

import java.util.*;

public class UserServiceImpl implements UserService {

    private UserDao ud;

    public UserDao getUd() {
		return ud;
	}

	public void setUd(UserDao ud) {
		this.ud = ud;
	}
	
	public List getAllUsers() {
        return this.ud.findAll(User.class);
    }
}

9、持久层文件:UserDao.java,详细代码如下:

package com.zsh.dao;

import java.util.List;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
public class UserDao 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;
    }

}

10、ZK的UI交互类:ListUser.java,详细代码如下:

package com.zsh.ui;

import com.zsh.service.ServiceLocator;
import java.util.*;
import com.zsh.service.UserService;
import com.zsh.model.User;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;

import com.zsh.model.User;
import com.zsh.service.UserService;

public class ListUser extends Listbox {

    public void onCreate() {
        UserService us = ServiceLocator.getUserManager();
        Collection allUsers = us.getAllUsers();
        Iterator it = allUsers.iterator();
        while(it.hasNext()) {
            User user = (User) it.next();
            Long id = (long) user.getId();
            String name = user.getName();
            String surname = user.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);
        }
     }
}

11、资源包根目录下的日志配置文件:log4j.properties,详细代码如下:

#  $Id: Action.java 502296 2007-02-01 17:33:39Z niallp $
# 
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.

log4j.rootLogger = WARN, stdout

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = WARN
log4j.appender.stdout.Target   = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ISO8601} %-5p [%F:%L] : %m%n

12、前台ZUL页面:index.zul,详细代码如下:

<!-- user.zul -->
<vbox>
  <listbox id="userList" width="800px" rows="5" use="com.zsh.ui.ListUser">
    <listhead>
      <listheader label="Name"/>
      <listheader label="Surname"/>
    </listhead>
  </listbox> 
</vbox>

搭好之后,整个项目的目录结构是这个样子的:




That`s all.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值