在api模块下service包,创建一个产品接口ProductService:(目前方法为分页查询queryByTypeLimit(Integer pType,Integer pageNo,Integer pageSize))
package com.bjpowernode.api.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import java.util.List;
/**
* 产品接口
*/
public interface ProductService {
/*根据产品类型,查询产品,支持分页*/
List<ProductInfo> queryByTypeLimit(Integer pType,Integer pageNo,Integer pageSize);
/*某个产品类型的记录总数*/
Integer queryRecordNumsByType(Integer pType);
/*首页的多个产品数据*/
MultiProduct queryIndexPageProducts();
/** 根据产品id ,查询产品信息 */
ProductInfo queryById(Integer id);
}
在dataservice模块service包,实现ProductService接口,创建ProductServiceImpl实现类:(这里实现分页查询queryByTypeLimit)
步骤:
1、暴露dubbo服务
2、使用@Resource,注入需要的Mapper对象
3、实现接口的方法(分页查询queryByTypeLimit(Integer pType, Integer pageNo, Integer pageSize))
4、对页数返回的参数做校验处理(比如:产品类型符合;页数大于0且为1开始)
5、产品类型符合if( pType == 0 || pType == 1 || pType == 2)
6、页数校验
在common模块util包,创建一个工具类CommonUtil类:(这里设计处理pageNo和处理pageSize,不存在或值小于1,默认等于1)
package com.bjpowernode.common.util;
import java.math.BigDecimal;
import java.util.regex.Pattern;
public class CommonUtil {
/*处理pageNo*/
public static int defaultPageNo(Integer pageNo) {
int pNo = pageNo;
if (pageNo == null || pageNo < 1) {
pNo = 1;
}
return pNo;
}
/*处理pageSize*/
public static int defaultPageSize(Integer pageSize) {
int pSize = pageSize;
if (pageSize == null || pageSize < 1) {
pSize = 1;
}
return pSize;
}
/*手机号脱敏*/
public static String tuoMinPhone(String phone) {
String result = "***********";
if (phone != null && phone.trim().length() == 11) {
result = phone.substring(0,3) + "******" + phone.substring(9,11);
}
return result;
}
/*手机号格式 true:格式正确;false不正确*/
public static boolean checkPhone(String phone){
boolean flag = false;
if( phone != null && phone.length() == 11 ){
//^1[1-9]\\d{9}$
flag = Pattern.matches("^1[1-9]\\d{9}$",phone);
}
return flag;
}
/*比较BigDecimal n1 >=n2 :true ,false*/
public static boolean ge(BigDecimal n1, BigDecimal n2){
if( n1 == null || n2 == null){
throw new RuntimeException("参数BigDecimal是null");
}
return n1.compareTo(n2) >= 0;
}
}
7、生成并返回三个值的集中对象:(return productInfos)
class ProductServiceImpl:
package com.bjpowernode.dataservice.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import com.bjpowernode.api.service.ProductService;
import com.bjpowernode.common.constants.YLBConstant;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.dataservice.mapper.ProductInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@DubboService(interfaceClass = ProductService.class,version = "1.0")
public class ProductServiceImpl implements ProductService {
@Resource
private ProductInfoMapper productInfoMapper;
/*按类型分页查询产品*/
@Override
public List<ProductInfo> queryByTypeLimit(Integer pType, Integer pageNo, Integer pageSize) {
List<ProductInfo> productInfos = new ArrayList<>();
if( pType == 0 || pType == 1 || pType == 2){
pageNo = CommonUtil.defaultPageNo(pageNo);
pageSize = CommonUtil.defaultPageSize(pageSize);
int offset = (pageNo - 1) * pageSize;
productInfos = productInfoMapper.selectByTypeLimit(pType, offset, pageSize);
}
return productInfos;
}
/*某个产品类型的记录总数*/
@Override
public Integer queryRecordNumsByType(Integer pType) {
Integer counts = 0;
if( pType == 0 || pType == 1 || pType == 2){
counts = productInfoMapper.selectCountByType(pType);
}
return counts;
}
/*首页的多个产品数据*/
@Override
public MultiProduct queryIndexPageProducts() {
MultiProduct result = new MultiProduct();
//查询新手宝
List<ProductInfo> xinShouBaoList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_XINSHOUBAO,0,1);
//查询优选
List<ProductInfo> youXuanList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_YOUXUAN,0,3 );
//散标
List<ProductInfo> sanBiaoList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_SANBIAO,0,3 );
result.setXinShouBao(xinShouBaoList);
result.setYouXuan(youXuanList);
result.setSanBiao(sanBiaoList);
return result;
}
/** 根据产品id ,查询产品信息 */
@Override
public ProductInfo queryById(Integer id) {
ProductInfo productInfo = null;
if( id != null && id > 0 ){
productInfo = productInfoMapper.selectByPrimaryKey(id);
}
return productInfo;
}
}
其中:
按产品类型分页查询:(需要在dataservice模块mapper包下的ProductInfoMapper接口添加方法,并在resources/mappers/ProductInfoMapper.xml编写SQL语句):
/*按产品类型分页查询*/
List<ProductInfo> selectByTypeLimit(@Param("ptype") Integer ptype,
@Param("offset") Integer offset,
@Param("rows") Integer rows);
<select id="selectByTypeLimit" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from b_product_info
where product_type = #{ptype}
order by release_time desc
limit #{offset},#{rows}
select>
在api模块下pojo包,创建一个多个产品数据MultiProduct类:
package com.bjpowernode.api.pojo;
import com.bjpowernode.api.model.ProductInfo;
import java.io.Serializable;
import java.util.List;
/**
* 多个产品数据
*/
public class MultiProduct implements Serializable {
private List<ProductInfo> xinShouBao;
private List<ProductInfo> youXuan;
private List<ProductInfo> sanBiao;
public List<ProductInfo> getXinShouBao() {
return xinShouBao;
}
public void setXinShouBao(List<ProductInfo> xinShouBao) {
this.xinShouBao = xinShouBao;
}
public List<ProductInfo> getYouXuan() {
return youXuan;
}
public void setYouXuan(List<ProductInfo> youXuan) {
this.youXuan = youXuan;
}
public List<ProductInfo> getSanBiao() {
return sanBiao;
}
public void setSanBiao(List<ProductInfo> sanBiao) {
this.sanBiao = sanBiao;
}
}
在api模块下service包,产品接口ProductService添加方法:(目前方法:首页的多个产品数据queryIndexPageProducts())
package com.bjpowernode.api.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import java.util.List;
/**
* 产品接口
*/
public interface ProductService {
/*根据产品类型,查询产品,支持分页*/
List<ProductInfo> queryByTypeLimit(Integer pType,Integer pageNo,Integer pageSize);
/*某个产品类型的记录总数*/
Integer queryRecordNumsByType(Integer pType);
/*首页的多个产品数据*/
MultiProduct queryIndexPageProducts();
/** 根据产品id ,查询产品信息 */
ProductInfo queryById(Integer id);
}
1、暴露dubbo服务
2、使用@Resource,注入需要的Mapper对象
3、实现接口的方法(queryIndexPageProducts())
4、生成并返回三个值的集中对象:(return result)
package com.bjpowernode.dataservice.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import com.bjpowernode.api.service.ProductService;
import com.bjpowernode.common.constants.YLBConstant;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.dataservice.mapper.ProductInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@DubboService(interfaceClass = ProductService.class,version = "1.0")
public class ProductServiceImpl implements ProductService {
@Resource
private ProductInfoMapper productInfoMapper;
/*按类型分页查询产品*/
@Override
public List<ProductInfo> queryByTypeLimit(Integer pType, Integer pageNo, Integer pageSize) {
List<ProductInfo> productInfos = new ArrayList<>();
if( pType == 0 || pType == 1 || pType == 2){
pageNo = CommonUtil.defaultPageNo(pageNo);
pageSize = CommonUtil.defaultPageSize(pageSize);
int offset = (pageNo - 1) * pageSize;
productInfos = productInfoMapper.selectByTypeLimit(pType, offset, pageSize);
}
return productInfos;
}
/*某个产品类型的记录总数*/
@Override
public Integer queryRecordNumsByType(Integer pType) {
Integer counts = 0;
if( pType == 0 || pType == 1 || pType == 2){
counts = productInfoMapper.selectCountByType(pType);
}
return counts;
}
/*首页的多个产品数据*/
@Override
public MultiProduct queryIndexPageProducts() {
MultiProduct result = new MultiProduct();
//查询新手宝
List<ProductInfo> xinShouBaoList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_XINSHOUBAO,0,1);
//查询优选
List<ProductInfo> youXuanList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_YOUXUAN,0,3 );
//散标
List<ProductInfo> sanBiaoList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_SANBIAO,0,3 );
result.setXinShouBao(xinShouBaoList);
result.setYouXuan(youXuanList);
result.setSanBiao(sanBiaoList);
return result;
}
/** 根据产品id ,查询产品信息 */
@Override
public ProductInfo queryById(Integer id) {
ProductInfo productInfo = null;
if( id != null && id > 0 ){
productInfo = productInfoMapper.selectByPrimaryKey(id);
}
return productInfo;
}
}
其中:
1、按产品类型分页查询:(需要在dataservice模块mapper包下的ProductInfoMapper接口添加方法,并在resources/mappers/ProductInfoMapper.xml编写SQL语句):
/*按产品类型分页查询*/
List<ProductInfo> selectByTypeLimit(@Param("ptype") Integer ptype,
@Param("offset") Integer offset,
@Param("rows") Integer rows);
<select id="selectByTypeLimit" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from b_product_info
where product_type = #{ptype}
order by release_time desc
limit #{offset},#{rows}
select>
2、在common模块constants包,创建一个常量类YLBConstant:(这里:产品类型)
package com.bjpowernode.common.constants;
/**
* 常量类
*/
public class YLBConstant {
/*****产品类型*********/
//新手宝
public static final int PRODUCT_TYPE_XINSHOUBAO = 0;
//优选
public static final int PRODUCT_TYPE_YOUXUAN = 1;
//散标
public static final int PRODUCT_TYPE_SANBIAO = 2;
/*****产品状态*****/
//未满标
public static final int PRODUCT_STATUS_SELLING = 0;
//满标
public static final int PRODUCT_STATUS_SELLED = 1;
//收益计划
public static final int PRODUCT_STATUS_PLAN = 2;
/*****投资状态*****/
//投资成功
public static final int INVEST_STATUS_SUCC = 1;
//投资失败
public static final int INVEST_STATUS_FAIL = 2;
/*****收益状态*****/
//生成收益计划
public static final int INCOME_STATUS_PLAN = 0;
//收益返还
public static final int INCOME_STATUS_BACK = 1;
/*充值状态*/
/*充值中*/
public static final int RECHARGE_STATUS_PROCESSING = 0;
//成功
public static final int RECHARGE_STATUS_SUCCESS = 1;
//失败
public static final int RECHARGE_STATUS_FAIL = 2;
}
在api模块下service包,产品接口ProductService添加方法:(目前方法:某个产品类型的记录总数queryRecordNumsByType(Integer pType))
package com.bjpowernode.api.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import java.util.List;
/**
* 产品接口
*/
public interface ProductService {
/*根据产品类型,查询产品,支持分页*/
List<ProductInfo> queryByTypeLimit(Integer pType,Integer pageNo,Integer pageSize);
/*某个产品类型的记录总数*/
Integer queryRecordNumsByType(Integer pType);
/*首页的多个产品数据*/
MultiProduct queryIndexPageProducts();
/** 根据产品id ,查询产品信息 */
ProductInfo queryById(Integer id);
}
1、暴露dubbo服务
2、使用@Resource,注入需要的Mapper对象
3、实现接口的方法(queryRecordNumsByType(Integer pType),产品类型检验)
4、生成并返回三个值的集中对象:(return counts)
package com.bjpowernode.dataservice.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import com.bjpowernode.api.service.ProductService;
import com.bjpowernode.common.constants.YLBConstant;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.dataservice.mapper.ProductInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@DubboService(interfaceClass = ProductService.class,version = "1.0")
public class ProductServiceImpl implements ProductService {
@Resource
private ProductInfoMapper productInfoMapper;
/*按类型分页查询产品*/
@Override
public List<ProductInfo> queryByTypeLimit(Integer pType, Integer pageNo, Integer pageSize) {
List<ProductInfo> productInfos = new ArrayList<>();
if( pType == 0 || pType == 1 || pType == 2){
pageNo = CommonUtil.defaultPageNo(pageNo);
pageSize = CommonUtil.defaultPageSize(pageSize);
int offset = (pageNo - 1) * pageSize;
productInfos = productInfoMapper.selectByTypeLimit(pType, offset, pageSize);
}
return productInfos;
}
/*某个产品类型的记录总数*/
@Override
public Integer queryRecordNumsByType(Integer pType) {
Integer counts = 0;
if( pType == 0 || pType == 1 || pType == 2){
counts = productInfoMapper.selectCountByType(pType);
}
return counts;
}
/*首页的多个产品数据*/
@Override
public MultiProduct queryIndexPageProducts() {
MultiProduct result = new MultiProduct();
//查询新手宝
List<ProductInfo> xinShouBaoList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_XINSHOUBAO,0,1);
//查询优选
List<ProductInfo> youXuanList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_YOUXUAN,0,3 );
//散标
List<ProductInfo> sanBiaoList = productInfoMapper.selectByTypeLimit(
YLBConstant.PRODUCT_TYPE_SANBIAO,0,3 );
result.setXinShouBao(xinShouBaoList);
result.setYouXuan(youXuanList);
result.setSanBiao(sanBiaoList);
return result;
}
/** 根据产品id ,查询产品信息 */
@Override
public ProductInfo queryById(Integer id) {
ProductInfo productInfo = null;
if( id != null && id > 0 ){
productInfo = productInfoMapper.selectByPrimaryKey(id);
}
return productInfo;
}
}
其中:
1、某个产品类型的记录总数:(需要在dataservice模块mapper包下的ProductInfoMapper接口添加方法,并在resources/mappers/ProductInfoMapper.xml编写SQL语句):
/*某个产品类型的记录总数*/
Integer selectCountByType(@Param("ptype") Integer pType);
<select id="selectCountByType" resultType="java.lang.Integer">
select count(id) as nums from b_product_info where product_type = #{ptype}
select>
在web模块下controller包,公用类BaseController添加对象(产品服务):
package com.bjpowernode.front.controller;
import com.bjpowernode.api.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
/**
* 公用controller类
*/
public class BaseController {
//声明公共的方法,属性的等
@Resource
protected StringRedisTemplate stringRedisTemplate;
//平台信息服务
@DubboReference(interfaceClass = PlatBaseInfoService.class,version = "1.0")
protected PlatBaseInfoService platBaseInfoService;
//产品服务
@DubboReference(interfaceClass = ProductService.class,version = "1.0")
protected ProductService productService;
//投资服务
@DubboReference(interfaceClass = InvestService.class,version = "1.0")
protected InvestService investService;
//用户服务
@DubboReference(interfaceClass = UserService.class,version = "1.0")
protected UserService userService;
//充值服务
@DubboReference(interfaceClass = RechargeService.class,version = "1.0")
protected RechargeService rechargeService;
}
在web模块下controller包,创建一个ProductController类:
package com.bjpowernode.front.controller;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.BidInfoProduct;
import com.bjpowernode.api.pojo.MultiProduct;
import com.bjpowernode.common.enums.RCode;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.front.view.PageInfo;
import com.bjpowernode.front.view.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags = "理财产品功能")
@RestController
@RequestMapping("/v1")
public class ProductController extends BaseController {
/*首页三类产品列表*/
@ApiOperation(value = "首页三类产品列表",notes = "一个新手宝,三个优选,三个散标产品")
@GetMapping("/product/index")
public RespResult queryProductIndex(){
RespResult result = RespResult.ok();
MultiProduct multiProduct = productService.queryIndexPageProducts();
result.setData(multiProduct);
return result;
}
/*按产品类型分页查询*/
@ApiOperation(value = "产品分页查询",notes = "按产品类型分页查询")
@GetMapping("/product/list")
public RespResult queryProductByType(@RequestParam("ptype") Integer pType,
@RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
@RequestParam(value = "pageSize",required = false,defaultValue = "9") Integer pageSize){
RespResult result = RespResult.fail();
if(pType != null && (pType == 0 || pType == 1 || pType == 2)){
pageNo = CommonUtil.defaultPageNo(pageNo);
pageSize = CommonUtil.defaultPageSize(pageSize);
//分页处理,记录总数
Integer recordNums = productService.queryRecordNumsByType(pType);
if( recordNums > 0 ){
//产品集合
List<ProductInfo> productInfos = productService.queryByTypeLimit(pType,pageNo,pageSize);
//构建PageInfo
PageInfo page = new PageInfo(pageNo,pageSize,recordNums);
result = RespResult.ok();
result.setList(productInfos);
result.setPage(page);
}
} else {
//请求参数有误
result.setRCode(RCode.REQUEST_PRODUCT_TYPE_ERR);
}
return result;
}
/*查询某个产品的详情和投资记录*/
@ApiOperation(value = "产品详情",notes = "查询某个产品的详细信息和投资5条记录")
@GetMapping("/product/info")
public RespResult queryProductDetail(@RequestParam("productId") Integer id){
RespResult result = RespResult.fail();
if( id != null && id > 0 ){
//调用产品查询
ProductInfo productInfo = productService.queryById(id);
if( productInfo != null){
//查询投资记录
List<BidInfoProduct> bidInfoList = investService.queryBidListByProductId(id,1,5);
//查询成功
result = RespResult.ok();
result.setData(productInfo);
result.setList(bidInfoList);
} else {
result.setRCode(RCode.PRODUCT_OFFLINE);
}
}
return result;
}
}
其中:
1、在web模块下view包,应答结果类RespResult添加一些对象:(成功和失败的RespResult对象、集合list对象、分页对象pageInfo)
package com.bjpowernode.front.view;
import com.bjpowernode.common.enums.RCode;
import com.sun.javaws.jnl.RContentDesc;
import java.sql.ResultSet;
import java.util.List;
/**
* 同一的应答结果。 controller方法的返回值都是它
*/
public class RespResult {
//应答码,自定义的数字
private int code;
//code的文字说明,一般做提示给用户看
private String msg;
//访问token
private String accessToken;
//单个数据
private Object data;
//集合数据
private List list;
//分页
private PageInfo page;
//表示成功的RespResult对象
public static RespResult ok(){
RespResult result = new RespResult();
result.setRCode(RCode.SUCC);
return result;
}
//表示失败的RespResult对象
public static RespResult fail(){
RespResult result = new RespResult();
result.setRCode(RCode.UNKOWN);
return result;
}
public void setRCode(RCode rcode){
this.code = rcode.getCode();
this.msg = rcode.getText();
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public PageInfo getPage() {
return page;
}
public void setPage(PageInfo page) {
this.page = page;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
2、在web模块view包,创建一个分页数据类PageInfo:(除了构造页号、每页大小,还能计算总页数)
// 建议使用分页插件
package com.bjpowernode.front.view;
/**
* 分页数据类
*/
public class PageInfo {
//页号
private Integer pageNo;
//每页大小
private Integer pageSize;
//总页数
private Integer totalPage;
//总记录数
private Integer totalRecord;
public PageInfo() {
}
public PageInfo(Integer pageNo, Integer pageSize, Integer totalRecord) {
this.pageNo = pageNo;
this.pageSize = pageSize;
this.totalRecord = totalRecord;
//计算总页数
if( this.totalRecord % this.pageSize == 0 ){
this.totalPage = this.totalRecord / this.pageSize;
} else {
this.totalPage = this.totalRecord / this.pageSize + 1;
}
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(Integer totalRecord) {
this.totalRecord = totalRecord;
}
}
3、在common模块enums包,创建一个枚举应答码RCode:
package com.bjpowernode.common.enums;
/**
* 枚举应答码
*/
public enum RCode {
UNKOWN(0,"请稍候重试"),
SUCC(1000,"请求成功"),
REQUEST_PARAM_ERR(1001,"请求参数有误"),
REQUEST_PRODUCT_TYPE_ERR(1002,"产品类型有误"),
PRODUCT_OFFLINE(1003,"产品已经下线"),
PHONE_FORMAT_ERR(1004,"手机号格式不正确"),
PHONE_EXISTS(1005,"手机号已经注册过"),
SMS_CODE_CAN_USE(1006,"验证码可以继续使用"),
SMS_CODE_INVALID(1007,"验证码无效"),
PHONE_LOGIN_PASSWORD_INVALID(1008,"手机号或者密码无效"),
REALNAME_FAIL(1009,"实名认证无效"),
REALNAME_RETRY(1010,"已经通过实名认证"),
TOKEN_INVALID(3000,"token无效"),
;
RCode(int c, String t){
this.code = c;
this.text = t;
}
/**应答码
* 0:默认
* 1000-2000是请求参数有误,逻辑的问题
* 2000-3000是服务器请求错误。
* 3000-4000是访问dubbo的应答结果
*/
private int code;
private String text;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}