JR 精品文章 - 我的持久层规范
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 数据库相关 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
我的持久层规范
cherami 原创   更新:2007-01-12 20:42:17  版本: 1.0   

我的持久层规范

昨天说了我的这个大轮子的大致情况,就是尽量保持简单,而配置最多的可能就是持久层和Action的配置,我的框架会完全取消这两个的配置,先说说持久层的情况。

  1. 类名和表名直接关联,关系是类名前带 t_
  2. 类的成员和表字段直接关联,名称完全一致,全部的表都需要一个名字为ID的主键,类型为long,由系统自动在插入的时候产生
  3. 表关联根据类之间的关联决定,如果两个类对对方都是直接引用,就是一对一,如果一个是直接引用,例外一个是集合,则是一对多,如果两边都是集合,就是多对多,对于集合的成员,其成员名是类名加s。多对多的表名是t_加两个类名,中间用_分隔,字段名就是类名加_id,如果是一对多,则外键名就是类名加_id
  4. 类型现在是自动匹配,也就是数据库的类型和类中的对应字段的类型是匹配的
  5. 主键的值在系统初始化的时候会从数据库表中读取最大值,然后缓存在系统中,每次加一
  6. 缓存的大小根据系统启动时数据库对应表的记录数决定,记录数小于一千的缓存100%,一千到一万的缓存50%,一万以上的缓存10%,十万以上缓存1%
  7. 类名可以分布在多个包中,配置文件配置都有那些包可以存放这些持久化对应的类,类名不可重复

根据这个规则,我完全取消了持久层的配置,对于新项目,这个规则的要求并不高,符合绝大部分的需要。至于查询,目前还没有完全想好,但是我考虑的是大部分的简单情况,只提供一个简单的对象表达式语法,复杂的查询还是使用SQL。

具体的例子:
数据库:
create table t_sampleprimary(id BIGINT PRIMARY KEY ,name VARCHAR(10),account BIGINT,birthday DATE,married BOOLEAN,age INT,income DOUBLE,saving BIGINT,city INT,working BOOLEAN,outgo DOUBLE)
create table t_samplechild(id BIGINT PRIMARY KEY ,name VARCHAR(10),sampleprimary_id BIGINT)
create table t_samplemany(id BIGINT PRIMARY KEY ,name VARCHAR(10))
create table t_samplemany_sampleprimary(sampleprimary_id BIGINT ,samplemany_id BIGINT)
类:
package com.jiehoo.model;

import java.util.Collection;
import java.util.Date;

import com.jiehoo.persistence.BasePersistencableBean;

public class SamplePrimary extends BasePersistencableBean {
    private String name;

    private Long account;

    private Date birthday;

    private Boolean married;

    private Integer age;

    private Double income;

    private long saving;

    private int city;

    private boolean working;

    private double outgo;

    private Collection sampleChilds;
   
    private Collection sampleManys;

    /**
     * @return the age
     */
    public Integer getAge() {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * @return the birthday
     */
    public Date getBirthday() {
        return birthday;
    }

    /**
     * @param birthday the birthday to set
     */
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    /**
     * @return the city
     */
    public int getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(int city) {
        this.city = city;
    }

    /**
     * @return the count
     */
    public Long getAccount() {
        return account;
    }

    /**
     * @param count the count to set
     */
    public void setAccount(Long count) {
        this.account = count;
    }

    /**
     * @return the income
     */
    public Double getIncome() {
        return income;
    }

    /**
     * @param income the income to set
     */
    public void setIncome(Double income) {
        this.income = income;
    }

    /**
     * @return the married
     */
    public Boolean getMarried() {
        return married;
    }

    /**
     * @param married the married to set
     */
    public void setMarried(Boolean married) {
        this.married = married;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the outgo
     */
    public double getOutgo() {
        return outgo;
    }

    /**
     * @param outgo the outgo to set
     */
    public void setOutgo(double outgo) {
        this.outgo = outgo;
    }

    /**
     * @return the saving
     */
    public long getSaving() {
        return saving;
    }

    /**
     * @param saving the saving to set
     */
    public void setSaving(long saving) {
        this.saving = saving;
    }

    /**
     * @return the working
     */
    public boolean isWorking() {
        return working;
    }

    /**
     * @param working the working to set
     */
    public void setWorking(boolean working) {
        this.working = working;
    }

    /**
     * @return the sampleChilds
     */
    public Collection getSampleChilds() {
        return sampleChilds;
    }

    /**
     * @param sampleChilds the sampleChilds to set
     */
    public void setSampleChilds(Collection sampleChilds) {
        this.sampleChilds = sampleChilds;
    }

    /**
     * @return the sampleManys
     */
    public Collection getSampleManys() {
        return sampleManys;
    }

    /**
     * @param sampleManys the sampleManys to set
     */
    public void setSampleManys(Collection sampleManys) {
        this.sampleManys = sampleManys;
    }

}

package com.jiehoo.model;

import com.jiehoo.persistence.BasePersistencableBean;

public class SampleChild extends BasePersistencableBean {
    private String name;

    private SamplePrimary samplePrimary;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the samplePrimary
     */
    public SamplePrimary getSamplePrimary() {
        return samplePrimary;
    }

    /**
     * @param samplePrimary the samplePrimary to set
     */
    public void setSamplePrimary(SamplePrimary samplePrimary) {
        this.samplePrimary = samplePrimary;
    }

}

package com.jiehoo.model;

import java.util.Collection;

import com.jiehoo.persistence.BasePersistencableBean;

public class SampleMany extends BasePersistencableBean {
    private String name;

    private Collection samplePrimarys;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the samplePrimarys
     */
    public Collection getSamplePrimarys() {
        return samplePrimarys;
    }

    /**
     * @param samplePrimarys the samplePrimarys to set
     */
    public void setSamplePrimarys(Collection samplePrimarys) {
        this.samplePrimarys = samplePrimarys;
    }

}


作者: Cherami
原载: 我的持久层规范
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。

更多技术文章,请访问我的BLOG:解惑

版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     0       0
作者其它文章: 作者全部文章     查看作者的Blog

这个文章共有 0 条评论
主题: instanceof和回调概念 上一篇文章
返回文章列表 返回〔数据库相关〕
下一篇文章 主题: 如何在Jini,RMI和Applet中实现代码签名


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号