泛型作用: 定义数据转换类型
指定泛型需要的具体类型,约束对象处理数据的类型,进行数据类型转换
查询 UserQueryForm
UserQueryForm extends BaseQueryForm
BaseQueryForm
extends BaseForm
public class BaseForm
1)From转化为Po
2)由 QueryForm 转 QueryParam
public P toParam(Class
clazz)
///接收查询form表单数据
@ApiModel
@Data
public class UserQueryForm extends BaseQueryForm<UserQueryParam> {
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "用户姓名")
private String name;
@ApiModelProperty(value = "手机号")
private String mobile;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@Past(message = "查询开始时间必须小于当前日期")
@ApiModelProperty(value = "查询开始时间")
private Date createdTimeStart;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@Past(message = "查询结束时间必须小于当前日期")
@ApiModelProperty(value = "查询结束时间")
private Date createdTimeEnd;
}
//实现BaseQueryForm to泛型 BaseParam 转换
@ApiModel
@Slf4j
@Data
public class BaseQueryForm<P extends BaseParam> extends BaseForm {
/**
* 分页查询的参数,当前页数
*/
private long current = 1;
/**
* 分页查询的参数,当前页面每页显示的数量
*/
private long size = 10;
/**
* Form转化为Param
*
* @param clazz
* @return
*/
public P toParam(Class<P> clazz) {
P p = BeanUtils.instantiateClass(clazz);
BeanUtils.copyProperties(this, p);
return p;
}
/**
* 从form中获取page参数,用于分页查询参数
*
* @return
*/
public Page getPage() {
return new Page(this.getCurrent(), this.getSize());
}
}
//BaseForm 实现转Po
@ApiModel
@Slf4j
@Data
public class BaseForm<T extends BasePo> {
/**
* 用户名
*/
private String username;
/**
* From转化为Po,进行后续业务处理
*
* @param clazz
* @return
*/
public T toPo(Class<T> clazz) {
T t = BeanUtils.instantiateClass(clazz);
BeanUtils.copyProperties(this, t);
return t;
}
/**
* From转化为Po,进行后续业务处理
*
* @param id
* @param clazz
* @return
*/
public T toPo(String id, Class<T> clazz) {
T t = BeanUtils.instantiateClass(clazz);
t.setId(id);
BeanUtils.copyProperties(this, t);
return t;
}
}
queryParam 与po 转查询对象QueryWrapper
class BaseParam
由 BaseParam得到QueryWrapper
QueryWrapper queryWrapper = new QueryWrapper<>();
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserQueryParam extends BaseParam<User> {
private String name;
private String mobile;
private String username;
}
@Data
public class BaseParam<T extends BasePo> {
private Date createdTimeStart;
private Date createdTimeEnd;
public QueryWrapper<T> build() {
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
queryWrapper.ge(null != this.createdTimeStart, "created_time", this.createdTimeStart)
.le(null != this.createdTimeEnd, "created_time", this.createdTimeEnd);
return queryWrapper;
}
UserService 处理dao 与po
UserService 泛型约束 <UserMapper, User>
class UserService extends ServiceImpl<UserMapper, User> implements IUserService
UserMapper
dao 处理po, 泛型约束
UserMapper extends BaseMapper
// 通过QueryForm 转 QueryParam 转 queryWrapper 调用 ServiceImpl.page
得到IPage 数据,返回 Result给reset接口
public class ServiceImpl<M extends BaseMapper, T> implements IService
public IPage page(IPage page, Wrapper queryWrapper)
IPage iPageUser = this.page(page, queryWrapper)
@Service
@Slf4j
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private IUserRoleService userRoleService;
@Override
public IPage<UserVo> query(Page page, UserQueryParam userQueryParam) {
QueryWrapper<User> queryWrapper = userQueryParam.build();
queryWrapper.eq(StringUtils.isNotBlank(userQueryParam.getName()), "name", userQueryParam.getName());
queryWrapper.eq(StringUtils.isNotBlank(userQueryParam.getUsername()), "username", userQueryParam.getUsername());
queryWrapper.eq(StringUtils.isNotBlank(userQueryParam.getMobile()), "mobile", userQueryParam.getMobile());
// 转换成VO
IPage<User> iPageUser = this.page(page, queryWrapper);
return iPageUser.convert(UserVo::new);
}
}
@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {
}