SSH集成的核心代码

Result.java
package cn.accp.adon.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class Result<T> implements Serializable {
    /*
     * 分页属性。
     */
    Page page;
    /*
     * 结果集。
     */
    List<T> items = new ArrayList<T>();

    public Result(List<T> items, Page page) {
        super();
        this.items = items;
        this.page = page;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

}

Page.java
package cn.accp.adon.core;

import java.io.Serializable;

@SuppressWarnings("serial")
public class Page implements Serializable {
    public Page() {
        page = 1;
        pagerows = 5;
    }

    public Page(int page) {
        super();
        this.page = page;
    }

    public Page(int page, int pagerows) {
        super();
        this.page = page;
        this.pagerows = pagerows;
    }

    // 总共有几页,
    int pagecount;
    // 当前页,
    int page;
    // 总共行数。
    int rows;
    // 每页行数。
    int pagerows;
    // 从哪一行开始查询。
    int pagestart;

    public int getPagestart() {
        return pagestart;
    }

    public void setPagestart(int pagestart) {
        this.pagestart = pagestart;
    }

    public boolean getHasnext() {
        if (page == pagecount) {
            return false;
        }
        return true;
    }

    public boolean getHasbefore() {
        if (page < 2) {
            return false;
        }
        return true;
    }

    public int getPagecount() {
        return pagecount;
    }

    public void setPagecount(int pagecount) {
        this.pagecount = pagecount;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
        // 当总行数发生改变,自动计算总页数和开始行数.
        if (this.pagerows < 1) {
            this.pagerows = 5;
        }
        // 计算总页数
        int mod = this.rows % this.pagerows;
        this.pagecount = this.rows / this.pagerows;
        if (mod != 0) {
            pagecount++;
        }
        // 开始行数
        if (page > pagecount) {
            page = 1;
        }
        this.pagestart = (page - 1) * this.pagerows;
        if (pagestart >= rows) {
            pagestart = 0;
        }
        if (page == mod) {
            return;
        }
    }

    public int getPagerows() {
        return pagerows;
    }

    public void setPagerows(int pagerows) {
        this.pagerows = pagerows;
    }

}

BaseManager.java
package cn.accp.adon.core;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

/*
 * 作者:陈习文
 * 最后修改时间:2009年3月7日
 * 说明:通用的服务类。已经实现了根据编号或属性查找,无条件查找所有记录,
 * 模糊查询,分页查询,结果的包装等功能。
 */
@SuppressWarnings("unchecked")
public abstract class BaseManager<T extends Base> extends HibernateTemplate {
    /*
     * 每个字类必须要实现的方法。 返回值必须是服务类对应的实体BEAN的类名。
     */
    protected abstract Class getEntityClass();

    /*
     * 根据编号查找实体类。 各子类不必再去重写或重载。
     */
    public T findById(Serializable id) {
        return (T) this.get(getEntityClass(), id);
    }

    /*
     * 定义了一个公共的日志对象。其它子类不必再调用system.out方法了。
     */
    protected static Log log = LogFactory.getLog(BaseManager.class);

    /*
     * 根据编号删除实体。
     */
    public void deleteById(Serializable id) {

        T t = this.findById(id);
        log.info("查询出数据" + t.getClass().getName());
        if (t != null) {
            this.delete(t);
        }
    }

    /*
     * 无条件无分页查找所有记录。 注意:仅限于数据量比较小的实体,比如下拉框需要填充值时。
     */
    public List<T> findAll() {
        return this.find("from " + getEntityClass().getSimpleName() + " e");
    }

    /*
     * 根据例子类自动生成hibernate 查询条件。
     */
    protected Criteria buildCriteria(T t, Session session) {
        T e = t;
        Criteria c = session.createCriteria(t.getClass());
        // 第一步设空字段.
        e = clear(e);
        // 设置模糊查询,
        c.add(Example.create(e).enableLike(MatchMode.ANYWHERE));
        // 第二步再设置多对一查询.
        c = addManyToOne(e, c);
        // 设置ID查询
        c = addId(e, c);
        return c;
    }

    /*
     * 根据例子查询,返回所有记录(无分页。).
     */
    public List<T> findAllByExample(final T t) throws Exception {
        return (List<T>) executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Criteria c = buildCriteria(t, session);
                return c.list();
            }
        });
    }

    /*
     * 根据例子查询,并且分页返回.
     */
    public Result<T> findAllByExample(final T t, final Page page)
            throws Exception {
        return (Result<T>) execute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Result<T> result = new Result<T>(new ArrayList<T>(), page);
                // 得到查询条件
                Criteria c = buildCriteria(t, session);
                Page p = page;
                Integer rows = 0;
                try {
                    // 先算出总行数。
                    rows = countByExample(t);
                    if (rows == null) {
                        rows = 0;
                    }
                    log.info("查询总行数:" + rows);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    rows = 0;
                    log.error(e);
                }
                // 让它自动计算从哪一行开始,有多少页。
                p.setRows(rows);
                // 没有记录 返回Null
                if (p.getRows() > 0) {
                    // 执行分页.
                    c.setMaxResults(p.getPagerows());
                    c.setFirstResult(p.getPagestart());
                    result.setItems(c.list());
                    result.setPage(p);

                }
                return result;
            }
        });
    }

    /*
     * 根据例子查询,返回总行数.
     */
    public Integer countByExample(final T t) throws Exception {
        return (Integer) execute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                // 调用通过的生成查询条件方法。
                Criteria c = buildCriteria(t, session);
                // 设置返回类型为求总行数.
                c.setProjection(Projections.rowCount());
                return c.uniqueResult();
            }
        });
    }

    /*
     * 清空数字和布尔型.
     */
    public T clear(T e) {
        Field[] fields = e.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                if (field.getType().getName().equals("java.lang.Integer")) {
                    try {
                        String tmp = BeanUtils.getProperty(e, field.getName());
                        if (tmp != null) {
                            Integer value = Integer.parseInt(tmp);
                            if (value.intValue() == 0) {
                                BeanUtils.setProperty(e, field.getName(), null);
                            }
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else if (field.getType().getName().equals("java.lang.Long")) {
                    try {
                        String tmp = BeanUtils.getProperty(e, field.getName());
                        if (tmp != null) {
                            Long value = Long.parseLong(tmp);
                            if (value.intValue() == 0) {
                                BeanUtils.setProperty(e, field.getName(), null);
                            }
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                } else if (field.getType().getName().equals("java.lang.Float")) {
                    try {
                        String tmp = BeanUtils.getProperty(e, field.getName());
                        if (tmp != null) {
                            Float value = Float.parseFloat(tmp);
                            if (value.intValue() == 0) {
                                BeanUtils.setProperty(e, field.getName(), null);
                            }
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else if (field.getType().getName().equals("java.lang.Double")) {
                    try {
                        String tmp = BeanUtils.getProperty(e, field.getName());
                        if (tmp != null) {
                            Double value = Double.parseDouble(tmp);
                            if (value.intValue() == 0) {
                                BeanUtils.setProperty(e, field.getName(), null);
                            }
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else if (field.getType().getName()
                        .equals("java.lang.Boolean")) {

                } else if (field.getType().getName().equals("java.lang.String")) {
                    try {
                        String value = BeanUtils
                                .getProperty(e, field.getName());
                        if (value != null && value.trim().equals("")) {
                            BeanUtils.setProperty(e, field.getName(), null);
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                }

            } catch (IllegalArgumentException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        return e;
    }

    /*
     * 设置级联查询
     */
    public Criteria addManyToOne(T e, Criteria c) {
        if (e == null) {
            return c;
        }
        Field[] fields = e.getClass().getDeclaredFields();
        for (Field field : fields) {

            // 现我们专门针对多对一进行关联查询.
            ManyToOne manytoone = field.getAnnotation(ManyToOne.class);
            if (manytoone != null) {
                try {
                    Object value = field.get(e);
                    if (value != null) {
                        c.add(Expression.eq(field.getName(), value));
                    }

                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        }
        return c;
    }

    /*
     * 设置主键模糊查询.
     */
    public Criteria addId(T e, Criteria c) {
        if (e == null) {
            return c;
        }
        Field[] fields = e.getClass().getDeclaredFields();
        for (Field field : fields) {

            // 现我们专门针对ID查询.
            Id id = field.getAnnotation(Id.class);
            if (id != null) {
                try {
                    if (field.getType().getName().equals("java.lang.String")) {
                        try {
                            // ---------2009-03-25-----------------
                            String value = BeanUtils.getProperty(e, "id");

                            if (value != null) {

                                c.add(Expression.like("id", value.trim(),
                                        MatchMode.ANYWHERE));
                            }
                            // ---------2009-03-25-----------------
                        } catch (InvocationTargetException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (NoSuchMethodException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                    }
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
        return c;
    }

}

BaseManager1.java
package cn.accp.adon.core;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

/*
 * 作者:陈习文
 * 最后修改时间:2009年3月7日
 * 说明:通用的服务类。已经实现了根据编号或属性查找,无条件查找所有记录,
 * 模糊查询,分页查询,结果的包装等功能。
 */
@SuppressWarnings("unchecked")
public abstract class BaseManager1<T extends Base> extends HibernateTemplate {
    /*
     * 每个字类必须要实现的方法。 返回值必须是服务类对应的实体BEAN的类名。
     */
    protected abstract Class getEntityClass();

    /*
     * 根据编号查找实体类。 各子类不必再去重写或重载。
     */
    public T findById(Serializable id) {
        return (T) this.get(getEntityClass(), id);
    }

    /*
     * 定义了一个公共的日志对象。其它子类不必再调用system.out方法了。
     */
    protected   Log log = LogFactory.getLog(BaseManager.class);

    /*
     * 根据编号删除实体。
     */
    public void deleteById(Serializable id) {

        T t = this.findById(id);
        log.info("查询出数据" + t.getClass().getName());
        if (t != null) {
            this.delete(t);
        }
    }

    /*
     * 无条件无分页查找所有记录。 注意:仅限于数据量比较小的实体,比如下拉框需要填充值时。
     */
    public List<T> findAll() {
        return this.find("from " + getEntityClass().getSimpleName() + " e");
    }

    /*
     * 根据例子类自动生成hibernate 查询条件。
     */
    protected Criteria buildCriteria(T t, Session session) {
        T e = t;
        Criteria c = session.createCriteria(t.getClass());
        // 第一步设空字段.
        e = clear(e);
        // 设置模糊查询,
        c.add(Example.create(e).enableLike(MatchMode.ANYWHERE));
        // 第二步再设置多对一查询.
        c = addManyToOne(e, c);
        // 设置ID查询
        c = addId(e, c);
        return c;
    }

    /*
     * 根据例子查询,返回所有记录(无分页。).
     */
    public List<T> findAllByExample(final T t) throws Exception {
        return (List<T>) executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Criteria c = buildCriteria(t, session);
                return c.list();
            }
        });
    }

    /*
     * 根据例子查询,并且分页返回.
     */
    public Result<T> findAllByExample(final T t, final Page page)
            throws Exception {
        return (Result<T>) execute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Result<T> result = new Result<T>(new ArrayList<T>(), page);
                // 得到查询条件
                Criteria c = buildCriteria(t, session);
                Page p = page;
                Integer rows = 0;
                try {
                    // 先算出总行数。
                    rows = countByExample(t);
                    if (rows == null) {
                        rows = 0;
                    }
                    log.info("查询总行数:" + rows);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    rows = 0;
                    log.error(e);
                }
                // 让它自动计算从哪一行开始,有多少页。
                p.setRows(rows);
                // 没有记录 返回Null
                if (p.getRows() > 0) {
                    // 执行分页.
                    c.setMaxResults(p.getPagerows());
                    c.setFirstResult(p.getPagestart());
                    result.setItems(c.list());
                    result.setPage(p);

                }
                return result;
            }
        });
    }

    /*
     * 根据例子查询,返回总行数.
     */
    public Integer countByExample(final T t) throws Exception {
        return (Integer) execute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                // 调用通过的生成查询条件方法。
                Criteria c = buildCriteria(t, session);
                // 设置返回类型为求总行数.
                c.setProjection(Projections.rowCount());
                return c.uniqueResult();
            }
        });
    }

    /*
     * 清空数字和布尔型.
     */
    public T clear(T e) {
        Field[] fields = e.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                if (field.getType().getName().equals("java.lang.Integer")) {
                    try {
                        String tmp = BeanUtils.getProperty(e, field.getName());
                        if (tmp != null) {
                            Integer value = Integer.parseInt(tmp);
                            if (value.intValue() == 0) {
                                BeanUtils.setProperty(e, field.getName(), null);
                            }
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else if (field.getType().getName().equals("java.lang.Long")) {
                    try {
                        String tmp = BeanUtils.getProperty(e, field.getName());
                        if (tmp != null) {
                            Long value = Long.parseLong(tmp);
                            if (value.intValue() == 0) {
                                BeanUtils.setProperty(e, field.getName(), null);
                            }
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                } else if (field.getType().getName().equals("java.lang.Float")) {
                    try {
                        String tmp = BeanUtils.getProperty(e, field.getName());
                        if (tmp != null) {
                            Float value = Float.parseFloat(tmp);
                            if (value.intValue() == 0) {
                                BeanUtils.setProperty(e, field.getName(), null);
                            }
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else if (field.getType().getName().equals("java.lang.Double")) {
                    try {
                        String tmp = BeanUtils.getProperty(e, field.getName());
                        if (tmp != null) {
                            Double value = Double.parseDouble(tmp);
                            if (value.intValue() == 0) {
                                BeanUtils.setProperty(e, field.getName(), null);
                            }
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else if (field.getType().getName()
                        .equals("java.lang.Boolean")) {

                } else if (field.getType().getName().equals("java.lang.String")) {
                    try {
                        String value = BeanUtils
                                .getProperty(e, field.getName());
                        if (value != null && value.trim().equals("")) {
                            BeanUtils.setProperty(e, field.getName(), null);
                        }
                    } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                }

            } catch (IllegalArgumentException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        return e;
    }

    /*
     * 设置级联查询
     */
    public Criteria addManyToOne(T e, Criteria c) {
        if (e == null) {
            return c;
        }
        Field[] fields = e.getClass().getDeclaredFields();
        for (Field field : fields) {

            // 现我们专门针对多对一进行关联查询.
            ManyToOne manytoone = field.getAnnotation(ManyToOne.class);
            if (manytoone != null) {
                try {
                    Object value = field.get(e);
                    if (value != null) {
                        c.add(Expression.eq(field.getName(), value));
                    }

                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        }
        return c;
    }

    /*
     * 设置主键模糊查询.
     */
    public Criteria addId(T e, Criteria c) {
        if (e == null) {
            return c;
        }
        Field[] fields = e.getClass().getDeclaredFields();
        for (Field field : fields) {

            // 现我们专门针对ID查询.
            Id id = field.getAnnotation(Id.class);
            if (id != null) {
                try {
                    if (field.getType().getName().equals("java.lang.String")) {
                        try {
                            // ---------2009-03-25-----------------
                            String value = BeanUtils.getProperty(e, "id");

                            if (value != null) {

                                c.add(Expression.like("id", value.trim(),
                                        MatchMode.ANYWHERE));
                            }
                            // ---------2009-03-25-----------------
                        } catch (InvocationTargetException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (NoSuchMethodException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                    }
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
        return c;
    }

}

Base.java
package cn.accp.adon.core;

import java.io.Serializable;

public interface Base extends Serializable {
    // 每个实体BEAN都把主键命名为id
    public Serializable getId();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值