`
kingsz1
  • 浏览: 155169 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

学学ZK - 从 derby 读记录 ZK + JPA

阅读更多
1. 参考文献和引用声明
本文主要参考:
Tutorial: Reading from the DB with Netbeans and ZK (Part 2)
http://javadude.wordpress.com/2009/03/24/tutorial-reading-from-the-db-with-netbeans-and-zk-part-2/

我使用了该文的代码,结合另一数据库的数据,稍加修改。原代码版权归原作者所有。

2. 数据库及 persistence.xml
我仍沿用 USERShttp://kingsz1.iteye.com/admin/blogs/356416 数据库。这是 persistence.xml 的内容:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="ZKJPA3PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>entity.Users</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="openjpa.ConnectionPassword" value="admin"/>
      <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="openjpa.ConnectionUserName" value="admin"/>
      <property name="openjpa.ConnectionURL" value="jdbc:derby://localhost:1527/USERS"/>
    </properties>
  </persistence-unit>
</persistence>




3. 实体类及其控制
实体类 Users.java ,从数据库生成,没有修改过。

package entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author admin
 */
@Entity
@Table(name = "USERS")
@NamedQueries({@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"), @NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"), @NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name = :name"), @NamedQuery(name = "Users.findByLoginid", query = "SELECT u FROM Users u WHERE u.loginid = :loginid"), @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"), @NamedQuery(name = "Users.findByAge", query = "SELECT u FROM Users u WHERE u.age = :age")})
public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "NAME")
    private String name;
    @Basic(optional = false)
    @Column(name = "LOGINID")
    private String loginid;
    @Basic(optional = false)
    @Column(name = "PASSWORD")
    private String password;
    @Basic(optional = false)
    @Column(name = "AGE")
    private int age;

    public Users() {
    }

    public Users(Integer id) {
        this.id = id;
    }

    public Users(Integer id, String name, String loginid, String password, int age) {
        this.id = id;
        this.name = name;
        this.loginid = loginid;
        this.password = password;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLoginid() {
        return loginid;
    }

    public void setLoginid(String loginid) {
        this.loginid = loginid;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Users[id=" + id + "]";
    }

}



实体类的控制类 Controller.java ,从数据库取数,放入 List


package test;

import entity.Users;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.zkoss.zul.Window;

public class Controller extends Window {

    List users = new ArrayList();
    Users selected = new Users();

    public Controller() {
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ZKJPA3PU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select u from Users as u");
        users = q.getResultList();
        em.close();
    }

    public List getUsers() {
        return users;
    }

    public Users getSelected() {
        return selected;
    }

    public void setSelected(Users selected) {
        this.selected = selected;
    }
}



4. ZK 文件 index.zul


<?xml version="1.0" encoding="UTF-8" ?>
<?page id="indexZUL" title="Sample ZK with JPA + Derby" cacheable="false"
	language="xul/html" zscriptLanguage="Java" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>

<zk xmlns="http://www.zkoss.org/2005/zul"
	xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">

<window id="win" title="Sample ZK with JPA + Derby " border="normal" width="97%" height="95%"
mode="overlapped" position="center" use="test.Controller">

    <listbox id="lb" model="@{win.users}" selectedItem="@{win.selected}" mold="paging" pageSize="4">
            <listhead sizable="true">
            <listheader label="Name 姓名" width="150px"/>
            <listheader label="LoginID 登录名" width="250px"/>
            <listheader label="Password 密码" width="150px"/>
            <listheader label="Age 年龄" width="50px"/>
        </listhead>
        <listitem self="@{each=users}">
            <listcell label="@{users.name}"/>
            <listcell label="@{users.loginid}"/>
            <listcell label="@{users.password}"/>
            <listcell label="@{users.age}"/>
        </listitem>
    </listbox>

  <separator bar="true"/>

    <hbox>
        <groupbox id="gb" mold="3d" width="100%">
            <caption label="Selected User"/>
            Name: <textbox id="name"  cols="30" value="@{win.selected.name}" />
            LoginID: <textbox id="loginid" cols="20" value="@{win.selected.loginid}" />
            Password: <textbox id="password" cols="20" value="@{win.selected.password}" />
            Age: <textbox id="age" cols="6" value="@{win.selected.age}" />
        </groupbox>
    </hbox>

</window>
</zk>



5. 试运行。
可分页显示数据库记录,每页4个记录,当然太少了,要改就该这里:
 pageSize="4" 


点击某个记录,在下面的 textbox 显示各字段内容。

如果加入更多的控制,就可以添加、修改、删除记录了。继续读书,继续模仿。

:-
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics