public class JSONStringHelp {
public static ObjectMapper mapper = new ObjectMapper();
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
}
@ApiModel(value = "分页")
public class PageBean<T> implements Serializable {
/** 当前页数 **/
@ApiModelProperty(value = "当前页数")
private int pager=1;
/** 总页数 **/
@ApiModelProperty(value = "总页数")
private int pages=0;
/** 每页条数 **/
@ApiModelProperty(value = "每页条数")
private int size=15;
/** 总条数 **/
@ApiModelProperty(value = "总条数")
private long total=0;
/** 忽略数据条数 **/
@ApiModelProperty(value = "忽略数据条数")
private int offset;
/** 列表数据 **/
@ApiModelProperty(value = "列表数据")
private List<T> list = new ArrayList<T>();
public Pageable getPageable() {
return PageRequest.of(pager-1, size);
}
public Pageable getPageable(Sort sort) {
return PageRequest.of(pager-1, size,sort);
}
public void ResultPageBean(Page<T> page){
this.list=page.getContent();
this.total=page.getTotalElements();
this.pages=page.getTotalPages();
}
public PageBean(int pager, int size) {
if (pager >= 0 && size >= 1) {
this.pager = pager;
this.size = size;
} else {
throw new RuntimeException("invalid pager: " + pager + " or size: " + size);
}
}
public static PageBean create(int pager, int size) {
return new PageBean(pager, size);
}
public void setTotal(long total) {
this.total = total;
}
public void setList(List<T> list) {
this.list = list;
}
public int getSize() {
return size;
}
public int getPager() {
return pager == 0 ? 1 : pager;
}
public int getOffset() {
return size * (getPager() - 1);
}
public int getPages() {
return Double.valueOf(Math.ceil((double) total / (double) size)).intValue();
}
public long getTotal() {
return total;
}
public List<T> getList() {
return list;
}
public void setPager(int pager) {
this.pager = pager;
}
public void setPages(int pages) {
this.pages = pages;
}
public void setSize(int size) {
this.size = size;
}
public void setOffset(int offset) {
this.offset = offset;
}
}
@Component
public class QueryUtil<T> {
@PersistenceContext
private EntityManager entityManager;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "新增、修改、删除返回对象")
public class ResultBean implements Serializable {
/**
* 状态
*/
@ApiModelProperty(value = "状态code")
private int code = 200;
/**
* 消息
*/
@ApiModelProperty(value = "消息")
private String message;
/**
* 返回对象,数据
*/
@ApiModelProperty(value = "对象")
private Object data;
@ApiModelProperty(value = "状态true与false")
private boolean succerss;
public ResultBean(int status, String message,Object data){
this.code = status;
this.message = message;
this.data = data;
if (status != 200){
this.succerss = false;
}else {
this.succerss = true;
}
}
public static ResultBean error(){
return new ResultBean(500,"操作失败",null);
}
public static ResultBean sucess(){
return new ResultBean(200,"操作成功",null);
}
public static ResultBean ok(String msg,Object obj){
return new ResultBean(200,msg,obj);
}
public static ResultBean sucess(Object obj){
return new ResultBean(200,"操作成功",obj);
}
public static ResultBean error(String message){
return new ResultBean(500,message,null);
}
public static ResultBean tokenError(Integer status,String msg){
return new ResultBean(status,msg,null);
}
}
public class MapHelper {
public static Map<String , Object> queryCondition(Object param){
Map<String , Object> map = new HashMap<String , Object>();
try{
if (param instanceof Map){
map.putAll((Map<? extends String,?> )param);
}else {
map.putAll(beanToMap(param));
}
}catch(Exception e){
e.printStackTrace();
}
return map;
}
public static <T> Map<String , Object> beanToMap(T bean){
Map<String ,Object> map = Maps.newHashMap();
if (bean != null){
BeanMap beanMap = BeanMap.create(bean);
for (Object key : beanMap.keySet()) {
map.put(String.valueOf(key),beanMap.get(key));
}
}
return map;
}
}