此篇接上一篇,持续实现后盾性能的实现,后盾是基于SSM框架进行性能实现的:
基本思路
- 引入EasyUI资源
- Datagrid组件实现初始化列表分页数据加载
- 用form将搜寻条件收集后转成json对象,向后盾发送申请从新加载数据
- 后盾Controller层:定义搜寻条件pojo类对申请数据进行封装接管
- 后盾Service层:调用mapper查问数据和总数,封装成后果对象返回
- 后盾Mapper层:依据查问条件对象进行数据查问、查问数据总数
具体实现
后盾实现:
1 定义搜寻条件pojo类和后果集
依据前端搜寻的条件表单,定义对应的搜寻条件pojo类:
此处我用了Lombok来编译pojo类,防止手工敲getter、setter和结构器
@Data@NoArgsConstructor@AllArgsConstructorpublic 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@NoArgsConstructorpublic 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);}
实现类:
@Servicepublic 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实现带搜寻框的列表页面的前后端性能即已全副实现,次要代码也列出。