src/main/java/com/moral/common/bean/PageBean.java
@@ -1,6 +1,7 @@ package com.moral.common.bean; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import com.github.pagehelper.Page; @@ -23,13 +24,13 @@ private Integer pageIndex; // 第几页 private Integer pageSize; // 每页记录数 private Integer pages; // 总页数 private String queryParams; private Integer size; // 当前页的数量 <= pageSize,该属性来自ArrayList的size属性 /** * 包装Page对象,因为直接返回Page对象,在JSON处理以及其他情况下会被当成List来处理, * 而出现一些问题。 * @param list page结果 * @param navigatePages 页码数量 */ public PageBean(List<T> list) { super(); @@ -39,7 +40,7 @@ this.pageSize = page.getPageSize(); this.total = page.getTotal(); this.pages = page.getPages(); this.data = page; this.data= new ArrayList<>(list); this.size = page.size(); } } src/main/java/com/moral/common/util/ExampleUtil.java
New file @@ -0,0 +1,115 @@ package com.moral.common.util; import lombok.extern.log4j.Log4j; import tk.mybatis.mapper.entity.Example; import java.beans.PropertyDescriptor; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Log4j public class ExampleUtil { private final static String OR_SPLIT = "or\\|"; private final static String CRITERIA_SPLIT = "\\|\\|\\|"; private final static String CONDITION_SPLIT = "\\|\\|"; private static Map<String, Method> criteriaMethodMap = null; public static Example generateExample(Class clzz, String params) { Example example = new Example(clzz); try { params = URLDecoder.decode(params, "UTF-8"); if (!StringUtils.isNullOrEmpty(params) && params.startsWith("or|")) { String[] criteria = params.trim().split(OR_SPLIT); Example.Criteria criteriaOfExample = example.or(); for (String criterion : criteria) { if (!StringUtils.isNullOrEmpty(criterion)) { String[] conditions = criterion.trim().split(CRITERIA_SPLIT); for (String condition : conditions) { if (!StringUtils.isNullOrEmpty(condition)) { String[] conditionItems = condition.split(CONDITION_SPLIT); if (conditionItems != null && conditionItems.length > 1) { //方法名 String methodName = conditionItems[0]; //属性名 String propertyName = conditionItems[1]; Method method = getMethod(methodName); if (method != null && isPropertyOfClass(clzz, propertyName)) { List values = new ArrayList<Object>(); if (conditionItems.length > 2) { //属性到类型 Class<?> valutType = new PropertyDescriptor(propertyName, clzz).getPropertyType(); for (int index = 2; index < conditionItems.length; index++) { values.add(StringUtils.stringToObject4Type(valutType, conditionItems[index])); } } invokeMethod(criteriaOfExample,method,propertyName,values); } } } } } } } } catch (Exception ex) { log.error(ex.getMessage()); ex.printStackTrace(); } return example; } private static void invokeMethod(Object obj, Method method,String propertyName,List values) throws InvocationTargetException, IllegalAccessException { int len = values.size(); switch (len) { case 0: if(method.getParameterCount()==1) { method.invoke(obj, propertyName); } break; case 1: if(method.getParameterCount()==2){ method.invoke(obj,propertyName,values.get(0)); } break; //between 查询 case 2: if(method.getParameterCount()==3){ method.invoke(obj,propertyName,values.toArray()); } break; //in 查询 default: if(method.getParameterCount()==2){ method.invoke(obj,propertyName,values); } break; } } ; private static boolean isPropertyOfClass(Class clzz, String propertyName) throws NoSuchFieldException { return clzz.getDeclaredField(propertyName) != null; } private static Method getMethod(String methodName) { if (criteriaMethodMap == null) { criteriaMethodMap = new HashMap<>(); Method[] methods = Example.Criteria.class.getMethods(); for (Method m : methods) { Method method = criteriaMethodMap.get(m.getName()); //重载函数存储参数最长的 if (method==null||m.getParameterTypes().length>method.getParameterTypes().length){ criteriaMethodMap.put(m.getName(),m); } } } return criteriaMethodMap.get(methodName); } } src/main/java/com/moral/common/util/MyBatisBaseMapUtil.java
New file @@ -0,0 +1,31 @@ package com.moral.common.util; import com.github.pagehelper.PageHelper; import com.moral.common.bean.PageBean; import com.moral.common.mapper.BaseMapper; import com.moral.entity.Account; import lombok.extern.log4j.Log4j; import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.mapperhelper.MapperHelper; import tk.mybatis.mapper.mapperhelper.MapperTemplate; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import java.util.List; @Log4j public class MyBatisBaseMapUtil { public static PageBean queryPage(BaseMapper baseMapper, PageBean pageBean,Class clazz){ List page = null; try { Example example = ExampleUtil.generateExample(clazz,pageBean.getQueryParams()); PageHelper.startPage(pageBean.getPageIndex(),pageBean.getPageSize()); page = baseMapper.selectByExample(example); }catch (Exception ex){ log.error(ex.getMessage()); ex.printStackTrace(); } return new PageBean(page); } } src/main/java/com/moral/common/util/StringUtils.java
New file @@ -0,0 +1,85 @@ package com.moral.common.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringUtils { public static final char UNDERLINE = '_'; /** * 驼峰格式字符串转换为下划线格式字符串 * * @param param * @return */ public static String camelToUnderline(String param) { if (param == null || "".equals(param.trim())) { return ""; } int len = param.length(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { char c = param.charAt(i); if (Character.isUpperCase(c)) { sb.append(UNDERLINE); sb.append(Character.toLowerCase(c)); } else { sb.append(c); } } return sb.toString(); } /** * //首字母转大写 * * @param s * @return */ public static String toUpperCaseFirstOne(String s){ StringBuilder sb = new StringBuilder(s); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); return sb.toString(); } public static boolean isNullOrEmpty(String toTest) { return toTest == null || toTest.length() == 0; } public static Object stringToObject4Type(Class<?> type,String value) throws Exception{ Object result = value; if (type == double.class || type == Double.class) { result = Double.parseDouble(value); } else if (type == float.class || type == Float.class) { result = Float.parseFloat(value); } else if (type == long.class || type == Long.class) { result = Long.parseLong(value); } else if (type == int.class || type == Integer.class) { result = Integer.parseInt(value); } else if (type == short.class || type == Short.class) { result = Short.parseShort(value); } else if (type == boolean.class || type == Boolean.class) { result = Boolean.parseBoolean(value); } else if (type == Date.class) { value = value.replace("T", " "); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { result = df.parse(value); } catch (Exception e) { df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); try { result = df.parse(value); } catch (ParseException e1) { df = new SimpleDateFormat("yyyy-MM-dd"); try { result = df.parse(value); } catch (ParseException e2) { } } } } return result; } } src/main/java/com/moral/controller/DeviceVersionController.java
@@ -1,12 +1,24 @@ package com.moral.controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.moral.common.bean.PageBean; import com.moral.common.bean.PageResult; import com.moral.entity.DeviceVersion; import com.moral.service.DeviceVersionService; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.UnsupportedEncodingException; @RestController @RequestMapping("device-version") @CrossOrigin(origins = "*", maxAge = 3600) public class DeviceVersionController { @Resource private DeviceVersionService deviceVersionService; @GetMapping("page-list") public PageBean<DeviceVersion> pageList(PageBean pageBean) throws NoSuchMethodException, UnsupportedEncodingException { return deviceVersionService.queryByPageBean(pageBean); } } src/main/java/com/moral/controller/MobileController.java
@@ -117,7 +117,7 @@ /** * Gets the monitor points by area name. * * @param areaName the area name * @param request the area name * @return the monitor points by area name */ @GetMapping("getMpointsByAreaName") @@ -130,7 +130,7 @@ /** * Gets the organizations by area name. * * @param areaName the area name * @param request the area name * @return the organizations by area name */ @GetMapping("getOrgsByAreaName") src/main/java/com/moral/entity/DeviceVersion.java
@@ -5,7 +5,6 @@ import javax.persistence.Id; import java.util.Date; @Data public class DeviceVersion { @Id src/main/java/com/moral/entity/DeviceVersion.java.1
New file @@ -0,0 +1,55 @@ package com.moral.entity; import java.util.Date; public class DeviceVersion { private Integer id; private String name; private Integer version; private Date createTime; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description == null ? null : description.trim(); } } src/main/java/com/moral/entity/DeviceVersion.java.2
New file @@ -0,0 +1,55 @@ package com.moral.entity; import java.util.Date; public class DeviceVersion { private Integer id; private String name; private Integer version; private Date createTime; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description == null ? null : description.trim(); } } src/main/java/com/moral/entity/DeviceVersion.java.3
New file @@ -0,0 +1,55 @@ package com.moral.entity; import java.util.Date; public class DeviceVersion { private Integer id; private String name; private Integer version; private Date createTime; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description == null ? null : description.trim(); } } src/main/java/com/moral/entity/DeviceVersion.java.4
New file @@ -0,0 +1,55 @@ package com.moral.entity; import java.util.Date; public class DeviceVersion { private Integer id; private String name; private Integer version; private Date createTime; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description == null ? null : description.trim(); } } src/main/java/com/moral/service/DeviceVersionService.java
@@ -1,4 +1,12 @@ package com.moral.service; import com.moral.common.bean.PageBean; import com.moral.entity.DeviceVersion; import java.io.UnsupportedEncodingException; public interface DeviceVersionService { public PageBean queryByPageBean(PageBean pageBean); } src/main/java/com/moral/service/impl/DeviceVersionServiceImpl.java
@@ -1,13 +1,23 @@ package com.moral.service.impl; import com.moral.common.bean.PageBean; import com.moral.common.util.ExampleUtil; import com.moral.common.util.MyBatisBaseMapUtil; import com.moral.entity.DeviceVersion; import com.moral.mapper.DeviceVersionMapper; import com.moral.service.DeviceVersionService; import org.springframework.stereotype.Service; import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; import java.io.UnsupportedEncodingException; @Service public class DeviceVersionServiceImpl implements DeviceVersionService { @Resource private DeviceVersionMapper deviceVersionMapper; private static Class ENTITY_CLASS = DeviceVersion.class; public PageBean queryByPageBean(PageBean pageBean){ return MyBatisBaseMapUtil.queryPage(deviceVersionMapper,pageBean,ENTITY_CLASS); } }