zoukankan      html  css  js  c++  java
  • 代码规范(以struts2+hibernate3+spring2.5为例) Mr

    实体类规范:

    package com.tkbs.domain.forum;

    import java.util.Date;

    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    import com.tkbs.domain.dict.DictSys;
    import com.tkbs.domain.user.UserAccounts;

    /**
    * ForumTopicComment entity.<br>
    * 回复/评论
    *
    *
    @author zxg
    */
    @Entity
    @Table(name
    ="FORUM_TOPIC_COMMENT")
    publicclass ForumTopicComment implements java.io.Serializable {

    privatestaticfinallong serialVersionUID =6878126252255579265L;
    private Long id; //评论id
    private String title; //标题
    private UserAccounts userAccounts;//用户
    private Date cdate; //创建时间
    private String content; //内容
    private Long supportCount;// 支持数
    private Long opposeCount;// 反对数
    private DictSys dictSysForState; //资源
    private Long topicId; //所属主题

    @Id
    @GeneratedValue
    @Column(name
    ="ID")
    public Long getId() {
    return id;
    }

    publicvoid setId(Long id) {
    this.id = id;
    }

    @Column(name
    ="TITLE")
    public String getTitle() {
    return title;
    }

    publicvoid setTitle(String title) {
    this.title = title;
    }

    @ManyToOne(fetch
    = FetchType.LAZY, targetEntity = UserAccounts.class, cascade = CascadeType.REFRESH)
    @JoinColumn(name
    ="USER_ACCOUNTS")
    public UserAccounts getUserAccounts() {
    return userAccounts;
    }

    publicvoid setUserAccounts(UserAccounts userAccounts) {
    this.userAccounts = userAccounts;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name
    ="cdate")
    public Date getCdate() {
    return cdate;
    }

    publicvoid setCdate(Date cdate) {
    this.cdate = cdate;
    }

    @Column(name
    ="CONTENT")
    public String getContent() {
    return content;
    }

    publicvoid setContent(String content) {
    this.content = content;
    }

    @Column(name
    ="support_count")
    public Long getSupportCount() {
    return supportCount;
    }

    publicvoid setSupportCount(Long supportCount) {
    this.supportCount = supportCount;
    }

    @Column(name
    ="oppose_count")
    public Long getOpposeCount() {
    return opposeCount;
    }

    publicvoid setOpposeCount(Long opposeCount) {
    this.opposeCount = opposeCount;
    }

    @ManyToOne(fetch
    = FetchType.LAZY, targetEntity = DictSys.class, cascade = CascadeType.REFRESH)
    @JoinColumn(name
    ="COMMENT_STATE")
    public DictSys getDictSysForState() {
    return dictSysForState;
    }

    publicvoid setDictSysForState(DictSys dictSysForState) {
    this.dictSysForState = dictSysForState;
    }

    @Column(name
    ="TOPIC_ID")
    public Long getTopicId() {
    return topicId;
    }

    publicvoid setTopicId(Long topicId) {
    this.topicId = topicId;
    System.out.println(
    "topicId"+topicId);
    }
    }

    Dao层:

    接口:

    package com.tkbs.persist.forum;

    import com.tkbs.domain.forum.ForumTopicComment;
    import com.tkbs.persist.base.BaseDao;

    /**
    * ForumTopicCommentDao interface<br>
    * 评论DAO接口 <br>
    * Creation date: 05-30-2011.
    *
    *
    @author zxg
    *
    @version 1.0
    */
    publicinterface ForumTopicCommentDao extends BaseDao<ForumTopicComment, Long> {

    }

    实现类:

    package com.tkbs.persist.forum;

    import org.springframework.stereotype.Repository;

    import com.tkbs.domain.forum.ForumTopicComment;
    import com.tkbs.persist.base.BaseDaoImpl;

    /**
    * ForumTopicCommentDaoImpl class<br>
    * 评论DAO实现类 <br>
    * Creation date: 05-30-2011.
    *
    *
    @author zxg
    *
    @version 1.0
    */
    @Repository(
    "forumTopicCommentDao")
    publicclass ForumTopicCommentDaoImpl extends
    BaseDaoImpl
    <ForumTopicComment, Long>implements ForumTopicCommentDao {

    privatestaticfinallong serialVersionUID =-5002836831999775354L;

    }

    Service层

    接口:

    package com.tkbs.service.forum;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import com.tkbs.domain.common.Page;
    import com.tkbs.domain.forum.ForumTopic;
    import com.tkbs.domain.forum.ForumTopicComment;
    /**
    * ForumTopicService interface<br>
    * 社区主题回复SERVICE接口 <br>
    * Creation date: 05-30-2011.
    *
    *
    @author zxg
    *
    @version 1.0
    */

    publicinterface ForumTopicCommentService {
    /**
    * 根据条件查询帖子评论主题
    *
    *
    @param String
    * keyword 关键字
    *
    @param Long
    * userId 用户
    *
    @param String
    * orderColumn 排序的列
    *
    @param String
    * orderType 排序方式
    *
    @param Integer
    * amount 返回结果数量
    *
    @return List<ForumTopic>
    */
    public Page queryForumTopicCommentByConditionsWithPage(Page page, String keyword,
    Long userId, Long topicId,String orderColumn, String orderType, Integer amount,String order);

    //增加评论
    publicvoid addForumTopicComment(ForumTopicComment forumTopicComment);

    }

    实现类:

    package com.tkbs.service.forum;
    import javax.annotation.Resource;

    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import com.tkbs.domain.common.Page;
    import com.tkbs.domain.forum.ForumTopicComment;
    import com.tkbs.persist.forum.ForumTopicCommentDao;

    /**
    * ForumTopicService interface<br>
    * 社区主题回复SERVICEIMPL实现类 <br>
    * Creation date: 05-30-2011.
    *
    *
    @author zxg
    *
    @version 1.0
    */
    @Service(
    "forumTopicCommentService")
    @Transactional(propagation
    = Propagation.REQUIRED)
    publicclass ForumTopicCommentServiceImpl implements ForumTopicCommentService {
    //注入DAO
    @Resource(name ="forumTopicCommentDao")
    private ForumTopicCommentDao forumTopicCommentDao;
    /**
    * 根据条件查询帖子评论主题
    *
    *
    @param String
    * keyword 关键字
    *
    @param Long
    * userId 用户
    *
    @param String
    * orderColumn 排序的列
    *
    @param String
    * orderType 排序方式
    *
    @param Integer
    * amount 返回结果数量
    *
    @return List<ForumTopic>
    */
    @Transactional(readOnly
    =true)
    public Page queryForumTopicCommentByConditionsWithPage(Page page,
    String keyword, Long userId, Long topicId, String orderColumn, String orderType,
    Integer amount, String order) {
    //表名
    String tableName=" FORUM_TOPIC_COMMENT a";
    //求和投影
    String projectionCount=" count(a.id)";
    //结果集投影
    String projection=" a.*";
    //语句条件
    StringBuilder sb=new StringBuilder();
    sb.append(
    " where 1=1");
    //关键字
    if (keyword !=null&& keyword.trim().length() >0) {
    sb.append(
    " and a.title like '%"+ keyword +"%' ");
    }
    //用户ID
    if (userId !=null&& userId >0) {
    sb.append(
    " and a.user_accounts = "+ userId +"");
    }
    //主题ID
    if (topicId !=null&& topicId >0) {
    sb.append(
    " and a.topic_id = "+ topicId +"");
    }
    //求和条件
    String wherecount=sb.toString();
    //结果集条件
    String where=wherecount;
    if(null!=order&&order.trim().length()>0&&null!=orderType){
    where
    +=" order by a."+order+""+orderType+"";
    }
    returnthis.forumTopicCommentDao.queryBySQLWithPage(projection,projectionCount, tableName, where, wherecount, page);
    }
    publicvoid addForumTopicComment(ForumTopicComment forumTopicComment) {

    this.forumTopicCommentDao.insert(forumTopicComment);

    }

    }

    baseDao:

    接口:

    package com.tkbs.persist.base;

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

    import org.hibernate.criterion.DetachedCriteria;

    import com.tkbs.domain.common.Page;

    /**
    * BaseDao interface<br>
    * 基本持久层DAO Creation date: 04-11-2011.
    *
    *
    @author zxg
    *
    @version 2.0
    */
    publicinterface BaseDao<T extends Object, PK extends Serializable>extends
    Serializable {
    /**
    * Method insert 添加、插入
    *
    *
    @param entity
    * 实体
    */
    publicvoid insert(T entity);

    /**
    * Method update 更新、修改
    *
    *
    @param entity
    * 实体
    */
    publicvoid update(T entity);

    /**
    * Method delete 删除
    *
    *
    @param entity
    * 实体
    */
    publicvoid delete(T entity);

    /**
    * Method deleteById 根据主键删除
    *
    *
    @param id
    * 主键
    */
    publicvoid deleteById(PK id);

    /**
    * Method queryById 根据主键查询单条信息
    *
    *
    @param
    * 主键
    *
    @return T 实体
    */
    public T queryById(PK id);

    /**
    * Method queryAll 查询所有
    *
    *
    @return List<T> 实体集
    */
    public List<T> queryAll();

    /**
    * Method queryAllWithPage 查询所有(分页)
    *
    *
    @param page
    * 分页实体
    *
    @return Page 分页实体
    */
    public Page queryAllWithPage(Page page);

    /**
    * Method queryByCriteriaWithPage Criteria分页
    *
    *
    @param page
    * 分页实体
    *
    @param dcCount
    * 求和条件DetachedCriteria
    *
    @param dc
    * 查询条件DetachedCriteria
    *
    @return Page 分页实体
    */
    public Page queryByCriteriaWithPage(DetachedCriteria dc,
    DetachedCriteria dcCount, Page page);

    /**
    * Method queryBySQLWithPage SQL分页
    *
    *
    @param projection
    * 投影
    *
    @param tableName
    * 表名
    *
    @param where
    * 选择
    *
    @param page
    * 分页实体
    *
    @return Page 分页实体
    */
    public Page queryBySQLWithPage(String projection, String projectionCount,
    String tableName, String where, String wherecount, Page page);

    /**
    * Method queryBySQLWithPage SQL分页(没有实体)
    *
    *
    @param projection
    * 投影
    *
    @param tableName
    * 表名
    *
    @param where
    * 选择
    *
    @param page
    * 分页实体
    *
    @return Page 分页实体
    */
    @SuppressWarnings(
    "unchecked")
    public Page queryBySQLWithPageWithoutEntity(String projection,
    String projectionCount, String tableName, String where,
    String wherecount, Page page);

    /**
    * Method queryBySQLWithList SQL列表查询
    *
    *
    @param String
    * sql 查询条件
    *
    @param Integer
    * amount 返回数量
    *
    @return List<T> 返回结果列表
    */
    public List<T> queryBySQLWithList(String sql, Integer amount);
    /**
    * Method queryBySQLWithList SQL列表查询
    *
    *
    @param String
    * sql 查询条件
    *
    @return List<T> 返回结果列表
    */
    public List<T> queryBySQLList(String sql);
    /**
    * Method queryBySQLWithListWithoutEntity <br>
    * SQL列表查询(没有实体)<br>
    * List<Object>
    *
    *
    @param String
    * sql 查询条件
    *
    @param Integer
    * amount 返回数量
    *
    @return List<T> 返回结果列表
    */
    public List<T> queryBySQLWithListWithoutEntity(String sql, Integer amount);

    /**
    * Method queryByCriteriaWithList Criteria列表查询
    *
    *
    @param DetachedCriteria
    * dc 查询条件
    *
    @param Integer
    * amount 返回数量
    *
    @return List<T> 返回结果列表
    */
    public List<T> queryByCriteriaWithList(DetachedCriteria dc, Integer amount);
    }

    实现类:

    package com.tkbs.persist.base;

    import java.io.Serializable;
    import java.sql.SQLException;
    import java.util.List;

    import javax.annotation.Resource;

    import org.hibernate.Criteria;
    import org.hibernate.HibernateException;
    import org.hibernate.SQLQuery;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.criterion.DetachedCriteria;
    import org.hibernate.criterion.Projections;
    import org.springframework.orm.hibernate3.HibernateCallback;
    import org.springframework.orm.hibernate3.HibernateTemplate;

    import com.tkbs.domain.common.Page;
    import com.tkbs.persist.base.BaseDao;

    /**
    * BaseDaoImpl class <br>
    * 基本DAO实现类<br>
    * Creation date: 04-11-2011.
    *
    *
    @see com.tkbs.persist.base.BaseDao
    *
    @author ouyang
    *
    @version 2.0
    */

    publicclass BaseDaoImpl<T extends Object, PK extends Serializable>implements
    BaseDao
    <T, PK> {

    privatestaticfinallong serialVersionUID =2476348749250540996L;

    // 1、实体class 由继承类实现 默认构造方法
    // protected Class entityClass;
    // 2.通过反射,获得指定类的父类的泛型参数的实际类型(反射,性能有待商榷)
    @SuppressWarnings("unchecked")
    protected Class entityClass = GenericsUtils.getSuperClassGenricType(this
    .getClass());
    /**
    * 注入 hibernateTemplate ,其定义在applicationContext.xml中
    */
    @Resource(name
    ="hibernateTemplate")
    protected HibernateTemplate ht;

    /**
    * Method insert 添加、插入
    *
    *
    @param entity
    * 实体
    */
    publicvoid insert(T entity) {
    ht.save(entity);
    }

    /**
    * Method insert 添加、插入
    *
    *
    @param entity
    * 实体
    */
    publicvoid insertWithFlushMode(T entity, boolean bl) {

    ht.execute(
    new HibernateCallback(){
    public Object doInHibernate(Session s)
    throws HibernateException, SQLException {
    // TODO Auto-generated method stub
    returnnull;
    }});

    // SessionFactory sf = c.buildSessionFactory();
    // Session s = sf.openSession();
    // Transaction tx = s.beginTransaction();
    // s.save(user);
    // tx.commit();
    // ht.save(entity);
    }

    /**
    * Method update 更新、修改
    *
    *
    @param entity
    * 实体
    */
    publicvoid update(T entity) {
    ht.update(entity);
    }

    /**
    * Method delete 删除
    *
    *
    @param entity
    * 实体
    */
    publicvoid delete(T entity) {
    ht.delete(entity);
    }

    /**
    * Method deleteById 根据主键删除
    *
    *
    @param id
    * 主键
    */
    publicvoid deleteById(PK id) {
    ht.delete(ht.get(entityClass, id));
    }

    /**
    * Method queryById 根据主键查询单条信息
    *
    *
    @param id
    * 主键
    *
    @return T 实体
    */
    @SuppressWarnings(
    "unchecked")
    public T queryById(PK id) {
    return (T) ht.get(entityClass, id);
    }

    /**
    * Method queryAll 查询所有
    *
    *
    @return List<T> 实体集
    */
    @SuppressWarnings(
    "unchecked")
    public List<T> queryAll() {
    List
    <T> lists = (List<T>) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    Criteria c
    = s.createCriteria(entityClass);
    // c.addOrder(Order.asc("id"));
    return c.list();
    }
    });
    return lists;
    }

    /**
    * Method queryAllWithPage 查询所有(分页)
    *
    *
    @param page
    * 分页实体
    *
    @return Page 分页实体
    */
    public Page queryAllWithPage(Page page) {
    DetachedCriteria dc
    = DetachedCriteria.forClass(entityClass);
    DetachedCriteria dcCount
    = DetachedCriteria.forClass(entityClass);
    returnthis.queryByCriteriaWithPage(dc, dcCount, page);
    }

    /**
    * Method queryByCriteriaWithPage Criteria分页
    *
    *
    @param page
    * 分页实体
    *
    @param dcCount
    * 求和条件DetachedCriteria
    *
    @param dc
    * 查询条件DetachedCriteria
    *
    @return Page 分页实体
    */
    @SuppressWarnings(
    "unchecked")
    public Page queryByCriteriaWithPage(final DetachedCriteria dc,
    final DetachedCriteria dcCount, Page page) {
    // 当前页数
    if (page.getCurrentPage() <=0) {
    page.setCurrentPage(
    1);
    }
    int totalContent = (Integer) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    // 设置投影
    dcCount.setProjection(Projections.rowCount());
    Criteria c
    = dcCount.getExecutableCriteria(s);
    return c.setMaxResults(1).uniqueResult();
    }
    });
    page.setTotalContent(totalContent);
    // 总条数
    // 页面属性
    page = getProperties(page);
    // 数字页数显示
    int[] showNumber =null;
    showNumber
    = getShowNumber(showNumber, page);
    page.setShowNumbers(showNumber);
    // 结果列表
    final Integer currentPage = page.getCurrentPage();
    final Integer pageResult = page.getPageResult();
    List
    <T> list = (List<T>) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    Criteria c
    = dc.getExecutableCriteria(s);
    int from = (currentPage -1) * pageResult;
    c.setFirstResult(from);
    c.setMaxResults(pageResult);
    return c.list();
    }
    });
    page.setList(list);
    return page;
    }

    /**
    * Method queryBySQLWithPage SQL分页 (select * from * where *)
    *
    *
    @param projection
    * 投影
    *
    @param tableName
    * 表名
    *
    @param where
    * 选择
    *
    @param page
    * 分页实体
    *
    @return Page 分页实体
    */
    @SuppressWarnings(
    "unchecked")
    public Page queryBySQLWithPage(final String projection,
    final String projectionCount, final String tableName,
    final String where, final String wherecount, Page page) {
    if (page.getCurrentPage() <=0) {
    page.setCurrentPage(
    1);
    }
    // 查询一共多少条记录
    int totalContent = (Integer) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    String countSQL
    =" select ";
    if (projectionCount !=null
    && projectionCount.trim().length() >0) {
    countSQL
    +=""+ projectionCount +"";
    }
    else {
    countSQL
    +=" count(*) ";
    }
    countSQL
    +=" from "+ tableName +""+ wherecount;
    SQLQuery sq
    = s.createSQLQuery(countSQL);
    return Integer.parseInt(sq.uniqueResult() +"");
    }
    });
    page.setTotalContent(totalContent);
    page
    = getProperties(page);
    int[] showNumber =null;
    showNumber
    = getShowNumber(showNumber, page);
    page.setShowNumbers(showNumber);
    // 数字页数显示
    // 结果列表
    final Integer currentPage = page.getCurrentPage();
    final Integer pageResult = page.getPageResult();
    List
    <T> list = (List<T>) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    String sql
    =" select ";
    if (projection !=null&& projection.length() >0) {
    sql
    = sql +""+ projection +"";
    }
    else {
    sql
    = sql +" * ";
    }
    sql
    +=" from "+ tableName +""+ where;
    SQLQuery sq
    = s.createSQLQuery(sql).addEntity(entityClass);
    int from = (currentPage -1) * pageResult;
    sq.setFirstResult(from);
    sq.setMaxResults(pageResult);
    return sq.list();
    }
    });
    page.setList(list);
    return page;
    }

    /**
    * Method queryBySQLWithPage SQL分页(没有实体)
    *
    *
    @param projection
    * 投影
    *
    @param tableName
    * 表名
    *
    @param where
    * 选择
    *
    @param page
    * 分页实体
    *
    @return Page 分页实体
    */
    @SuppressWarnings(
    "unchecked")
    public Page queryBySQLWithPageWithoutEntity(final String projection,
    final String projectionCount, final String tableName,
    final String where, final String wherecount, Page page) {
    if (page.getCurrentPage() <=0) {
    page.setCurrentPage(
    1);
    }
    // 查询一共多少条记录
    int totalContent = (Integer) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    String countSQL
    =" select ";
    if (projectionCount !=null
    && projectionCount.trim().length() >0) {
    countSQL
    +=""+ projectionCount +"";
    }
    else {
    countSQL
    +=" count(*) ";
    }
    countSQL
    +=" from "+ tableName +""+ wherecount;
    SQLQuery sq
    = s.createSQLQuery(countSQL);
    return Integer.parseInt(sq.uniqueResult() +"");
    }
    });
    page.setTotalContent(totalContent);
    page
    = getProperties(page);
    int[] showNumbers =null;
    showNumbers
    = getShowNumber(showNumbers, page);
    page.setShowNumbers(showNumbers);
    // 数字页数显示
    // 结果列表
    final Integer currentPage = page.getCurrentPage();
    final Integer pageResult = page.getPageResult();
    List
    <T> list = (List<T>) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    String sql
    =" select ";
    if (projection !=null&& projection.length() >0) {
    sql
    = sql +""+ projection +"";
    }
    else {
    sql
    = sql +" * ";
    }
    sql
    +=" from "+ tableName +""+ where;
    SQLQuery sq
    = s.createSQLQuery(sql);
    int from = (currentPage -1) * pageResult;
    sq.setFirstResult(from);
    sq.setMaxResults(pageResult);
    // System.out.println("queryBySQLWithPage 中的 sql 语句:" + sql);
    return sq.list();
    }
    });
    page.setList(list);
    return page;
    }
    // ***********************************************************
    // 分页参数
    /**
    * Method getProperties 获取分页参数
    *
    *
    @param page
    * 分页实体
    *
    @return Page 分页实体
    */
    publicstatic Page getProperties(Page page) {
    int totalPage =0;
    if (page.getTotalContent() % page.getPageResult() ==0) {
    totalPage
    = page.getTotalContent() / page.getPageResult();
    }
    else {
    totalPage
    = page.getTotalContent() / page.getPageResult() +1;
    }
    page.setTotalPage(totalPage);
    // 总页数
    page.setFirstPage(1); // 第一页
    if (totalPage ==0) {
    page.setLastPage(
    1);
    }
    else {
    page.setLastPage(totalPage);
    // 最后一页
    }
    if (totalPage > page.getCurrentPage()) {
    page.setNextPage(page.getCurrentPage()
    +1); // 下一页
    }
    if (page.getCurrentPage() >1) {
    page.setPreviousPage(page.getCurrentPage()
    -1); // 上一页
    }
    return page;
    }

    // 分页数字个数
    /**
    * Method getShowNumber 获取分页数字个数
    *
    *
    @param showNumber
    * int[]
    *
    @param page
    * 分页实体
    *
    @return int[] 页数数字
    */
    publicstaticint[] getShowNumber(int[] showNumber, Page page) {
    if (page.getTotalPage() >=0
    && page.getTotalPage() <= page.getShowNumberCount()) {
    showNumber
    =newint[page.getTotalPage()];
    for (int i =0; i < page.getTotalPage(); i++) {
    showNumber[i]
    = i +1;
    }
    return showNumber;
    }
    else {
    if (page.getTotalPage() - page.getCurrentPage() <= page
    .getShowNumberCount()
    /2) {
    showNumber
    =newint[page.getShowNumberCount()];
    for (int i =0; i < page.getShowNumberCount(); i++) {
    showNumber[i]
    = i + page.getTotalPage()
    - page.getShowNumberCount() +1;
    }
    return showNumber;

    }
    elseif (page.getCurrentPage() <= page.getShowNumberCount() /2) {
    showNumber
    =newint[page.getShowNumberCount()];
    for (int i =0; i < page.getShowNumberCount(); i++) {
    showNumber[i]
    = i +1;
    }
    return showNumber;
    }
    else {

    showNumber
    =newint[page.getShowNumberCount()];
    if (page.getShowNumberCount() %2==0) {
    // SHOW_NUMBER_PAGE为双数时,当前页数在左边
    for (int i =0; i < page.getShowNumberCount(); i++) {
    showNumber[i]
    = i + page.getCurrentPage()
    - page.getShowNumberCount() /2+1;
    }
    }
    else {
    // SHOW_NUMBER_PAGE为单数时,当前页数在中间
    for (int i =0; i < page.getShowNumberCount(); i++) {
    showNumber[i]
    = i + page.getCurrentPage()
    - page.getShowNumberCount() /2;
    }
    }
    return showNumber;
    }

    }

    }

    /**
    * Method queryBySQLWithList SQL列表查询
    *
    *
    @param String
    * sql 查询条件
    *
    @param Integer
    * amount 返回数量
    *
    @return List<T> 返回结果列表
    */
    public List<T> queryBySQLWithList(final String sql, final Integer amount) {
    List
    <T> list = (List<T>) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    SQLQuery sq
    = s.createSQLQuery(sql).addEntity(entityClass);
    if (amount !=null&& amount >0) {
    sq.setMaxResults(amount);
    }
    return sq.list();
    }
    });
    return list;
    }
    /**
    * Method queryBySQLWithList SQL列表查询
    *
    *
    @param String
    * sql 查询条件,查询所有满足条件的
    *
    @return List<T> 返回结果列表
    */
    public List<T> queryBySQLList(final String sql) {
    List
    <T> list = (List<T>) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    SQLQuery sq
    = s.createSQLQuery(sql).addEntity(entityClass);
    return sq.list();
    }
    });
    return list;
    }
    /**
    * Method queryBySQLWithListWithoutEntity <br>
    * SQL列表查询(没有实体)<br>
    * List<Object>
    *
    *
    @param String
    * sql 查询条件
    *
    @param Integer
    * amount 返回数量
    *
    @return List<T> 返回结果列表
    */
    public List<T> queryBySQLWithListWithoutEntity(final String sql,
    final Integer amount) {
    List
    <T> list = (List<T>) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    SQLQuery sq
    = s.createSQLQuery(sql);
    if (amount !=null&& amount >0) {
    sq.setMaxResults(amount);
    }
    return sq.list();
    }
    });
    return list;
    }

    /**
    * Method queryByCriteriaWithList Criteria列表查询
    *
    *
    @param DetachedCriteria
    * dc 查询条件
    *
    @param Integer
    * amount 返回数量
    *
    @return List<T> 返回结果列表
    */
    public List<T> queryByCriteriaWithList(final DetachedCriteria dc,
    final Integer amount) {
    List
    <T> list = (List<T>) ht.execute(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
    SQLException {
    Criteria c
    = dc.getExecutableCriteria(s);
    if (amount !=null&& amount >0) {
    c.setMaxResults(amount);
    }
    return c.list();
    }
    });
    return list;
    }
    /*
    * DetachedCriteria dcCount = DetachedCriteria
    * .forClass(KnowledgeQuestions.class); // System.out.println(content);
    * dcCount .add(Restrictions .like("questionTitle", "%" + content + "%"));
    */

    }
    
    
    
    
    
    Mr-sniper
    北京市海淀区
    邮箱:rafx_z@hotmail.com
  • 相关阅读:
    如果前面的IO操作出问题了,按照我们代码的意思,不就try catch 了吗,这样的话线程就没关闭了,就会造成线程泄露。 那怎么解决这个问题呢,其实也简单,把关闭线程的方法写到finally里就可以了。
    Dataeye计算任务架构
    Mercury:唯品会全链路应用监控系统解决方案详解(含PPT)
    app 爬虫
    唯品会HDFS性能挑战和优化实践
    构建Hadoop监控共同体
    消除单点故障 flume
    时间戳 Flume's Memory Consumption
    telnet nmap netstap
    hdfs ha
  • 原文地址:https://www.cnblogs.com/rafx/p/specification.html
Copyright © 2011-2022 走看看