如何实现资源检索?

来源:互联网 发布:阿里算法工程师 工资 编辑:程序博客网 时间:2024/06/04 18:31

在我的记忆中,在做一个系统的时候一定要做查询的功能,在一开始的时候我会这样,一个文本框然后后边跟一个下拉框,查询的时候会指定是按什么条件查询,比如按名称查询,按类别查询等等。而我们查询的时候会发现我们只能查询特定的几个表,而且不能实现不同类别的查询。

原来我们可以在数据库里定义一个检索资源表,只要是我们想加入检索的实体,都可以放进这个实体类中。那么如果实现在把不同实体类,也就是不同表中的数据插入这个检索资源表呢?

是这样的我们可以利用hibernate的拦截器,在配置sessionfactory的时候配置一个Hibernate Intercept.

<!-- Hibernate Intercept-->
        <property name="entityInterceptor">                 
            <bean id="resourceIntercept" class="com.ether.common.domain.ExecResIntercept" />
        </property>        
  
   当然这里面的ExecResintercept这个类可以直接实现Interceptor接口,也可以(最好)继承自EmptyInterceptor。 

下面把ExecResintercept类贴出来。

public class ExecResIntercept extends EmptyInterceptor {private static final long serialVersionUID = 5110217892906233933L;/** * 新增资源拦截器方法 */public boolean onSave(Object entity, Serializable id, Object[] state,String[] propertyNames, org.hibernate.type.Type[] types) {// 注入ServicesResSearchInfoServices resSearchInfoServices = (ResSearchInfoServices) SpringUtil.getBean("resSearchInfoServices");// ResSearchInfoServices resSearchInfoServices;// entity就是当前的实体对象 ,如果当前操作的是产品信息,则做处理if (entity instanceof ResourceEntity) {resSearchInfoServices.saveRes((ResourceEntity) entity);// 返回true则拦截本次操作return true;}return super.onSave(entity, id, state, propertyNames, types);}/** * 修改资源拦截器方法 */public void postFlush(Iterator entities) {// entity就是当前的实体对象 ,如果当前操作的是产品信息,则做处理while (entities.hasNext()) {Object entity = entities.next();// entity就是当前的实体对象 ,如果当前操作资源检索类接口,则做处理if (entity instanceof ResourceEntity) {// 注入ServicesResSearchInfoServices resSearchInfoServices = (ResSearchInfoServices) SpringUtil.getBean("resSearchInfoServices");resSearchInfoServices.updateRes((ResourceEntity) entity);} else {super.postFlush(entities);}}}/** * 删除资源拦截器方法 */public void onDelete(Object entity, Serializable id, Object[] state,String[] propertyNames, org.hibernate.type.Type[] types) {// entity就是当前的实体对象 ,如果当前操作的是产品信息,则做处理if (entity instanceof ResourceEntity) {if (((ResourceEntity) entity).getDataId() == null|| "".equals(entity)) {super.onDelete(entity, id, state, propertyNames, types);}// 注入ServicesResSearchInfoServices resSearchInfoServices = (ResSearchInfoServices) SpringUtil.getBean("resSearchInfoServices");resSearchInfoServices.deleteRes((ResourceEntity) entity);}super.onDelete(entity, id, state, propertyNames, types);}}
以及这个借口:

public interface ResourceEntity {

    // 原记录主键ID
    String getDataId();

    // 资源检索模块名称
    String getResSearchName();
    
    //关键字,分号分隔
    String getKey();

    //摘要,分号分隔
    String getSummary();

    // 原记录表名
    String getDataTableName();

    // 原记录名称
    String getDataTitle();

    // 原记录位置
    String getDataPath();

    // 原记录Action名称
    String GetActionName();

    // 登陆用户ID
    String getCreateUserId();

    // 登陆用户姓名
    String getCreateUserName();

    // 登陆用户所在部门ID
    String getCreateOrgId();

    // 登陆用户所在部门名称
    String getCreateOrgName();

    // 创建日期
    Date getCreaterTime();

    // 创建信息
    String getCreateMemo();

    // 登陆用户ID
    String getUpdateUserId();

    // 登陆用户姓名
    String getUpdateUserName();

    // 登陆用户所在部门ID
    String getUpdateOrgId();

    // 登陆用户所在部门名称
    String getUpdateOrgName();

    // 修改日期
    Date getUpdateTime();

    // 修改信息
    String getUpdateMemo();
    
    //是否删除到回收站0表示正常,1表示删除到回收站
    int getRecycleFlag();

}

这样就在我们每次对数据库做操作的时候,拦截器都会拦截,看看实体是不是继承自ResourceEntity借口,若果是就把这个实体中的信息存入检索信息表,这样我们以后查询就直接查询这个检索表就可以了。是不是很简单。。。。。。


原创粉丝点击