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的实现类上打断点是没有作用的;一定要做好日志的输出,好容易确定错误的位置。
原文:https://www.cnblogs.com/theskying/p/10273571.html