关于java:EasyUI实现带搜索框的列表页面二后台实现

42次阅读

共计 4340 个字符,预计需要花费 11 分钟才能阅读完成。

此篇接上一篇,持续实现后盾性能的实现,后盾是基于 SSM 框架进行性能实现的:

基本思路

  1. 引入 EasyUI 资源
  2. Datagrid 组件实现初始化列表分页数据加载
  3. 用 form 将搜寻条件收集后转成 json 对象,向后盾发送申请从新加载数据
  4. 后盾 Controller 层:定义搜寻条件 pojo 类对申请数据进行封装接管
  5. 后盾 Service 层:调用 mapper 查问数据和总数,封装成后果对象返回
  6. 后盾 Mapper 层:依据查问条件对象进行数据查问、查问数据总数

具体实现

后盾实现:

1 定义搜寻条件 pojo 类和后果集

依据前端搜寻的条件表单,定义对应的搜寻条件 pojo 类:
此处我用了 Lombok 来编译 pojo 类,防止手工敲 getter、setter 和结构器

@Data
@NoArgsConstructor
@AllArgsConstructor
public class HostSearchCondition {
    // page 和 rows 是 easyUI 默认带的参数
    private Integer page;
    private Integer rows;
    // 搜寻条件
    private String hname;
    private String status;
    private String strong;
    private String hpstart;
    private Integer hpdiscount;
}

后果集:EasyUI 的分页要求后盾回传的 json 数据必须有 rows 和 total 两个字段

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult<T> {
    private List<T> rows;
    private Integer total;

}

2 Controller 层

比较简单,就是利用条件搜寻实体类进行数据的接管,并调用 service 层对象进行查找返回

@Controller
@RequestMapping("hostController")
public class HostController {

    @Autowired
    private HostService hostService;
    
    @RequestMapping("findHostPage")
    @ResponseBody
    public PageResult<Host> findHostPage(HostSearchCondition condition){return hostService.selectHosts(condition);
    }
    
}

3 Service 层

service 层:接管 Controller 层传来的数据后,调用 mapper 层对象查问数据并封装后果集回传
Service 接口:

public interface HostService {public PageResult<Host> selectHosts(HostSearchCondition condition);
}

实现类:

@Service
public class HostServiceImpl implements HostService {
    @Autowired
    private HostMapper hostMapper;
    @Override
    public PageResult<Host> selectHosts(HostSearchCondition condition) {
        // 设置 limit 起始数据
        int page = condition.getPage();
        int rows = condition.getRows();
        page = page*rows-rows;
        condition.setPage(page);
        // 调用 mapper 进行数据查问
        List<Host> hosts = hostMapper.selectHosts(condition);
        Integer total = hostMapper.selectCountHosts(condition);
        PageResult<Host> pageResult = new PageResult<>();
        pageResult.setRows(hosts);
        pageResult.setTotal(total);
        return pageResult;
    }
}

4 Mapper 层:

Mapper 接口:

public interface HostMapper {
    // 查问分页数据
    public List<Host> selectHosts(HostSearchCondition condition);
    // 查问对立条件下,数据总量
    public Integer selectCountHosts(HostSearchCondition condition);

}

因为波及多表联查,不方便使用注解,须要应用 xml 配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gcp.mapper.HostMapper">
    <resultMap id="hostMap" type="com.gcp.pojo.Host">
        <result property="hid" column="hid" ></result>
        <result property="hname" column="hname" ></result>
        <result property="hpwd" column="hpwd" ></result>
        <result property="hphone" column="hphone" ></result>
        <result property="starttime" column="starttime" ></result>
        <result property="status" column="status" ></result>
        <result property="strong" column="strong" ></result>
        <result property="num" column="num" ></result>
        <association property="hostPower" javaType="com.gcp.pojo.HostPower">
            <result property="hpid" column="hpid"></result>
            <result property="hpstart" column="hpstart"></result>
            <result property="hpstartBeigindate" column="hpstart_beigindate"></result>
            <result property="hpstartEnddate" column="hpstart_enddate"></result>
            <result property="hpOrderPower" column="hp_order_power"></result>
            <result property="hpstartBegintime" column="hpstart_begintime"></result>
            <result property="hpstartEndtime" column="hpstart_endtime"></result>
            <result property="hpdiscount" column="hpdiscount"></result>
            <result property="hpDisStarttime" column="hp_dis_starttime"></result>
            <result property="hpDisEndtime" column="hp_dis_endtime"></result>
            <result property="hpprice" column="hpprice"></result>
            <result property="hcosts" column="hcosts"></result>
            <result property="hostid" column="hostid"></result>
        </association>
    </resultMap>
    <select id="selectHosts" resultMap="hostMap">
        select * from t_host h
        left join t_host_power p on h.hid = p.hostid

        <where>
            <if test="hname!='' and hname!=null">
                and h.hname like concat('%',#{hname},'%')
            </if>
            <if test="status!='' and status!=null">
                and h.status = #{status}
            </if>

            <if test="hpstart!='' and hpstart!=null">
                and p.hpstart = #{hpstart}
            </if>
            <if test="hpdiscount!='' and hpdiscount!=null">
                and p.hpdiscount = #{hpdiscount}
            </if>
        </where>
        <if test="strong!='' and strong!=null">
            order by h.strong ${strong}
        </if>
        limit #{page},#{rows}
    </select>
    <select id="selectCountHosts" resultType="int">
        select count(*) from t_host h
        left join t_host_power p on h.hid = p.hostid

        <where>
            <if test="hname!='' and hname!=null">
                and h.hname like concat('%',#{hname},'%')
            </if>
            <if test="status!='' and status!=null">
                and h.status = #{status}
            </if>

            <if test="hpstart!='' and hpstart!=null">
                and p.hpstart = #{hpstart}
            </if>
            <if test="hpdiscount!='' and hpdiscount!=null">
                and p.hpdiscount = #{hpdiscount}
            </if>
        </where>
    </select>
</mapper>

至此,EasyUI 实现带搜寻框的列表页面的前后端性能即已全副实现,次要代码也列出。

正文完
 0