SSM整合dubbo 進行分頁查詢

1.先書寫Mapper和sql語句java

public interface ActEntityMapper { int deleteByPrimaryKey(String actId); int insert(ActEntity record); int insertSelective(ActEntity record); ActEntity selectByPrimaryKey(String actId); int updateByPrimaryKeySelective(ActEntity record); int updateByPrimaryKey(ActEntity record); // 測試查詢
 ActEntity selectOneById(String actId) ; /** * * @param size 查詢數量 * @param from 偏移量 * @return
     */ List<ActEntity> selectAll(@Param("size") int size, @Param("from") int from) ; // 查詢總記錄數
 Integer selectListTotal(); }

ActEntityMapper.xmlweb

<!--測試,分頁查詢全部信息-->
  <select id="selectAll" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from hx_act limit #{from},#{size} </select>
  <!--查詢表中的總記錄數-->
  <select id="selectListTotal" resultType="java.lang.Integer"> select count(*) from hx_act </select>
  <!--測試查詢-->
  <select id="selectOneById" resultMap="BaseResultMap" parameterType="java.lang.String"> select act_id, act_name, act_desc from hx_act where act_id = #{actId} </select>

2.傳入的實體類、返回的實體類以及bean對象sql

傳入實體:json

@Api("查詢h_act全部的信息") public class SelectAllReq extends BaseListReq { }

返回實體:app

@Data @Api("查詢hx_act表中的全部信息") public class SelectAllResp extends ResponseEntity { @ApiModelProperty("返回的數據") private List<ActBean> list ; @ApiModelProperty("總記錄數") private int total ; public SelectAllResp(){ super(SUCCESS); } public SelectAllResp(String errCode) { super(errCode); } }

bean對象,用於對象之間的轉換:ide

public class ActBean extends BaseEntity { private String actId; private String actName; private String actDesc; private Integer actType; private Integer actModel; private Date startTime; private Date endTime; private String repeatType; private Integer status; private String shareContext; private String extInfo; private String joinUrl; private Date createTime; }

3. 書寫Service接口,和具體的實現類測試

Service接口:spa

@Api(value = "楊連軍測試service",produces = "application/json", description = "楊連軍測試service") public interface YangTestServiceI { @ApiOperation("根據actId查詢這條記錄") YangTestResp getActInfoById (String actId) ; @ApiOperation("分頁查詢全部記錄") SelectAllResp getListInfo (SelectAllReq selectAllReq) ; }

實現類和所須要的方法:日誌

@ApiOperation("傳入偏移量和記錄數,分頁查詢全部記錄") @Override public SelectAllResp getListInfo(SelectAllReq selectAllReq) { SelectAllResp selectAllResp = new SelectAllResp() ; List<ActBean> beanList = new ArrayList<>() ; Integer total = actEntityMapper.selectListTotal() ; System.out.println("總記錄數:"+total); if (null == total||total==0){ // 沒有信息
 selectAllResp.setErrCode(ErrorCodeConst.DATA_NOT_EXISTED); return selectAllResp ; } // 調用dao,得到返回的記錄
        List<ActEntity> list = actEntityMapper.selectAll(selectAllReq.getSize(),selectAllReq.getFrom()); // 轉換類型
        beanList = converUserList(list) ; selectAllResp.setList(beanList); selectAllResp.setTotal(total); return selectAllResp ; } /** * @desc 不一樣泛型List之間的轉換 * @param list * @return
     */
    public List<ActBean> converUserList(List<ActEntity> list){ List<ActBean> beanList = new ArrayList<>() ; if (Lists.isEmpty(list)){ // 若是傳入的是空,直接返回空的List<ActBean>
            return beanList ; } for (ActEntity actEntity : list){ // 便利
            ActBean actBean = new ActBean() ; // 對象的複製
 BeanUtils.copyProperties(actEntity,actBean); beanList.add(actBean) ; } return beanList ; }

4.書寫控制器code

// 查詢全部的act信息
    @RequestMapping("/getListActInfo") @ResponseBody public void getListActInfo (HttpServletRequest request, HttpServletResponse response,SelectAllReq selectAllReq){ System.out.println("傳入參數:"+selectAllReq.getSize()); ; SelectAllResp selectAllResp = yangTestServiceI.getListInfo(selectAllReq) ; System.out.println("返回的狀態碼:"+selectAllResp.getErrCode()); resultString(selectAllResp.toJsonStr(),response,false); }

6.須要注意的點

(1) 在消費者上書寫完成接口,要進行clean,而後install,放到本地的倉庫中,這樣真正的消費者纔可以找獲得。同時,書寫完成DAO的模塊也要進行一樣的操做。

(2) 由於真正的web端的消費者是調用的本地倉庫中的包,因此在service的實現類上打斷點是沒有做用的;必定要作好日誌的輸出,好容易肯定錯誤的位置。

相關文章
相關標籤/搜索