经过了Compass 更智能的搜索引擎(1)–入门的学习,想必对于Compass的使用有了更深的认识了吧。下面谈点更加切合实际开发的东西。那就是CRUD.



面向对象的分页

分页获得的一个个的页面本身就是一个对象,所以我这样设计页面Page.java

/**
 * @Date 2016年8月2日
 *
 * @author Administrator
 */
package domain;

import java.util.List;

/**
 * @author 郭瑞彪
 *
 */
public class Page<T> {

    private List<T> lists;

    private int totalResults;

    public List<T> getLists() {
        return lists;
    }

    public void setLists(List<T> lists) {
        this.lists = lists;
    }

    public int getTotalResults() {
        return totalResults;
    }

    public void setTotalResults(int totalResults) {
        this.totalResults = totalResults;
    }

    @Override
    public String toString() {
        return "Page [lists=" + lists + ", totalResults=" + totalResults + "]";
    }

}

dao层实现

/**
 * @Date 2016年8月2日
 *
 * @author Administrator
 */
package dao;

import java.util.ArrayList;
import java.util.List;

import org.compass.core.CompassHits;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;

import domain.Article;
import domain.Page;
import util.CompassUtils;

/**
 * @author 郭瑞彪
 *
 */
public class ArticleIndexDao {

    /**
     * 保存到索引库
     *
     * @param a
     */
    public void save(Article a) {

        CompassSession session = CompassUtils.getCompassSessionFactory().openSession();
        CompassTransaction tx = null;
        try {
            tx = session.beginTransaction();
            session.create(a);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            session.close();
        }

    }

    /**
     * 删除索引库中对应的索引
     *
     * @param id
     */
    public void delete(Integer id) {
        CompassSession session = CompassUtils.getCompassSessionFactory().openSession();
        CompassTransaction tx = null;
        try {
            tx = session.beginTransaction();
            session.delete(Article.class, id);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            session.close();
        }

    }

    /**
     * 更新索引
     *
     * @param a
     */
    public void update(Article a) {
        CompassSession session = CompassUtils.getCompassSessionFactory().openSession();
        CompassTransaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(a);// 添加或者更新,照应了优化以减少代价
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            session.close();
        }

    }

    /**
     * 支持分页的查询
     *
     * @param queryString
     *            查询字符串
     * @param firstResult
     *            开始查询页码
     * @param maxResult
     *            每页的数据量
     * @return
     */
    public Page search(String queryString, int firstResult, int maxResult) {
        CompassSession session = CompassUtils.getCompassSessionFactory().openSession();
        CompassTransaction tx = null;
        try {
            tx = session.beginTransaction();
            // 查询,得到结果
            CompassHits hits = session.find(queryString);
            Page page = new Page();
            page.setTotalResults(hits.getLength());
            List<Article> articles = new ArrayList<Article>();
            // 分页机制,获取一段数据
            firstResult = firstResult >= 0 ? firstResult : 0;
            int endResult = Math.min(firstResult + maxResult, hits.getLength());
            for (int i = firstResult; i < endResult; i++) {
                Article a = (Article) hits.data(i);
                articles.add(a);
            }
            page.setLists(articles);
            tx.commit();
            return page;
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            session.close();
        }
    }

}

代码释义

优点一

DAO层代码的设计必须有很强的容错性及弹性。

public void save(Article a) {

        CompassSession session = CompassUtils.getCompassSessionFactory().openSession();
        CompassTransaction tx = null;
        try {
            tx = session.beginTransaction();
            session.create(a);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            session.close();
        }

    }

从这段代码中,就能略见一斑了。

优点二

// 分页机制,获取一段数据
            firstResult = firstResult >= 0 ? firstResult : 0;
            int endResult = Math.min(firstResult + maxResult, hits.getLength());
            for (int i = firstResult; i < endResult; i++) {
                Article a = (Article) hits.data(i);
                articles.add(a);
            }

我们应该学习的就是这样的敏捷开发的经验,充分的考虑到可能发生的状况。来保证代码的正确的运行。

05-08 15:14