This article continues to complete the realization of the background function, which is based on the SSM framework:

The basic idea

  1. Introduce the EasyUI resource
  2. The DataGrid component implements the initialization list paging data load
  3. Form will be used to collect the search conditions into a JSON object, and send a request to the background to reload the data
  4. Background Controller layer: define search criteria POJO classes to encapsulate and receive request data
  5. Background Service layer: call Mapper to query data and total amount, encapsulate the result object and return
  6. Background Mapper layer: data query and total data query according to the query condition object

The specific implementation

Background implementation:

Define search criteria POJO classes and result sets

Define the POJO class for the search criteria based on the criteria sheet in the front end: Here I use Lombok to compile the POJO class, avoiding manually typing getters, setters, and constructors

Public class HostSearchCondition {// Pages and Rows are private parameters for easyUI Integer page; private Integer rows; Private String hName; private String hName; private String status; private String strong; private String hpstart; private Integer hpdiscount; }

Result Set: Paging in EasyUI requires that the JSON data sent back in the background must have two fields: Rows and Total

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

}

The Controller layer 2

It is simple, which is to use the conditional search entity class to receive the data, and call the Service layer object to search and return the data

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

3 the Service layer

Service layer: after receiving the data from Controller layer, the object of Mapper layer is called to query the data and encapsulate the result set and send back the Service interface:

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

Implementation class:

@Service public class HostServiceImpl implements HostService { @Autowired private HostMapper hostMapper; Override public PageResult<Host> selectHosts(hostSearchCondition condition) condition.getPage(); int rows = condition.getRows(); page = page*rows-rows; condition.setPage(page); 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 layer:

Mapper interfaces:

Public List<Host> selectHosts(hostSearchCondition condition); public List<Host> selectHosts(hostSearchCondition condition); Public Integer selectCountStosts (HostSearchCondition condition); }

It is not convenient to use annotations because it involves multi-table lookup, so we need to use XML configuration file:

<? The 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>

So far, EasyUI implementation with search box list page before and after the end of the function that has been all realized, the main code is also listed.