1. 程式人生 > >SSM整合dubbo 進行分頁查詢

SSM整合dubbo 進行分頁查詢

pda 本地倉庫 偏移量 time record 操作 des esc @param

1.先書寫Mapper和sql語句

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.xml

 <!--測試,分頁查詢所有信息-->
  <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對象

傳入實體:

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

返回實體:

@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對象,用於對象之間的轉換:

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接口:

@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.書寫控制器

    // 查詢所有的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的實現類上打斷點是沒有作用的;一定要做好日誌的輸出,好容易確定錯誤的位置。

SSM整合dubbo 進行分頁查詢