本文已参与「新人创作礼」活动,一起开启掘金创作之路。
package com.esint.gaplatform.sjxxdataxjclgs.util;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Obj2map {
public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new HashMap<String, Object>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
String field_name = humpToLine(fieldName);
Object value = field.get(obj);
map.put(field_name, value);
}
return map;
}
private static Pattern linePattern = Pattern.compile("_(\\w)");
private static Pattern humpPattern = Pattern.compile("[A-Z]");
/**
* 驼峰转下划线,最后转为大写
* @param str
* @return
*/
public static String humpToLine(String str) {
Matcher matcher = humpPattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString().toUpperCase();
}
/**
* 下划线转驼峰,正常输出
* @param str
* @return
*/
public static String lineToHump(String str) {
Matcher matcher = linePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
}
实例应用
package com.esint.gaplatform.sjxxdataxjclgs.controller;
import com.esint.gaplatform.sjxxdataxjclgs.entitymubiao.TEsintSjxxBpcclxxbEntity;
import com.esint.gaplatform.sjxxdataxjclgs.entitymubiao.TEsintSjxxHsryEntity;
import com.esint.gaplatform.sjxxdataxjclgs.service.IDataSendService;
import com.esint.gaplatform.sjxxdataxjclgs.service.impl.BaseUserHandle;
import com.esint.gaplatform.sjxxdataxjclgs.util.FileTxt;
import com.esint.gaplatform.sjxxdataxjclgs.util.Obj2map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.*;
@Api(value = "个人记录查询-测试")
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private IDataSendService iDataSendService;
@Autowired
private BaseUserHandle baseUserHandle;
@ApiOperation(value = "测试连通性", httpMethod = "GET")
@ResponseBody
@RequestMapping(value = "/ping", method = RequestMethod.GET)
public void testPing() {
System.out.println("hello");
}
//检验对象转换map 的键值
@ApiOperation(value = "checkMap", httpMethod = "GET")
@ResponseBody
@RequestMapping(value = "/checkMap", method = RequestMethod.GET)
public void checkMap() throws IllegalAccessException {
Obj2map obj2map = new Obj2map();
TEsintSjxxHsryEntity tEsintSjxxHsryEntity = new TEsintSjxxHsryEntity();
tEsintSjxxHsryEntity.setXm("sd");
tEsintSjxxHsryEntity.setZjhm("324");
Map<String, Object> fieldAnnotation = Obj2map.objectToMap(tEsintSjxxHsryEntity);
}
@ApiOperation(value = "test", httpMethod = "GET")
@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void test() {
// System.out.println(baseUserHandle.getTotal());
FileTxt fileTxt = new FileTxt();
Date date = new Date();
fileTxt.FileW("随手记" + date.toString(), "/runlog/baseuser/1000.txt");
}
@ApiOperation(value = "test1", httpMethod = "GET")
@ResponseBody
@RequestMapping(value = "/test1", method = RequestMethod.GET)
public void test1() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
double d1 = 17;
double d2 = 3;
double result = d1 / d2;
/**
* 四舍五入
*/
int roundNum = (int) Math.round(result);
/**
* 向上取整
*/
int ceilNum = (int) Math.ceil(result);
/**
* 向下取整
*/
int floorNum = (int) Math.floor(result);
System.out.println("除法商值:" + result);
System.out.println("四舍五入:" + roundNum);
System.out.println("向上取整:" + ceilNum);
System.out.println("向下取整:" + floorNum);
List<Integer> list = new ArrayList<>();
for(int i=1;i<11;i++){
list.add(i);
}
while (list.size()>0){
Integer a = list.get(0);
System.out.println("*****");
System.out.println("*");
System.out.println(a);
System.out.println("*");
for(Integer q:list){
System.out.print(Integer.toString(q)+"-");
};
System.out.println("*****");
list.remove(0);
}
System.out.println("-----");
}
}