1. Data in json format
{
"createtime": "2021-01-09 15:38:31.322",
"data": [{
"idno": "121",
"field_code": "MVR_5_1_2",
"submitname": "MVR-5-1-2",
"fllevel2showclass": " dl_118 dl_120",
"xh": 0,
"id": "055d6afd1006f66d5e8fb3a97501e75d",
"fieldlink": "",
"fllevel2xh": 0
}, {
"idno": "119",
"field_code": "MVR_5_1_1",
"submitname": "MVR-5-1-1",
"fllevel2showclass": " dl_118",
"xh": 0,
"id": "05d3b2574ee78ab788afd094d4a47c84",
"fieldlink": "{\"values\":[{\"hide\":\"\",\"show\":\"120\",\"rule\":\"==::y\",\"oper\":\"==\",\"type\":\"hide\",\"maxOper\":\"\",\"maxRule\":\"\"}],\"type\":\"hide\"}",
"fllevel2xh": 0
}]
}
Note: The incoming data in json format to the background needs to be encoded encodeURIComponent(json)
function dlJson(){
dLong.layeropen(w-50, h-50, "字段校验", "${ctx}/Tbz4FieldInfoEntityController/fieldJson?data="+encodeURIComponent(json)+"&type=fieldcheckdlJson");
}
2,dbtojson
Read the data of the database and convert it to a json file, the
core code:
FileUtils.writeStringToFile(new File(ApplicationHome.getApplicationConfig()+"/fieldjson/"+filename),json.toString(), "UTF-8");
Where
ApplicationHome.getApplicationConfig()+"/fieldjson/"+filename
is the location and file name of the file to be stored
Note: If you don’t pay attention to UTF-8 , it is easy to make mistakes, it is best to copy and paste
dbtojson thought steps:
1. Query the desired data from the data
2. Convert the data into a json string
3. Read the json string and write it into a file
Method 1: Use Map<String, Object> conversion here
引入文件:
import net.sf.json.JSONObject;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
@RequestMapping("/tojson")
@ResponseBody
public Map tojson(@RequestParam HashMap<String,String> paraMap) {
try {
String sql = "select * from table";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
Map<String, Object> objectMap=new HashMap<>();
objectMap.put("data", list);
objectMap.put("createtime", new Timestamp(System.currentTimeMillis()).toString());
JSONObject json = JSONObject.fromObject(objectMap);
String filename=paraMap.get("bzcode")+".json";
FileUtils.writeStringToFile(new File(ApplicationHome.getApplicationConfig()+"/fieldjson/"+filename)
,json.toJSONString(objectMap,
SerializerFeature.PrettyFormat
,SerializerFeature.SortField
,SerializerFeature.WriteNullListAsEmpty
,SerializerFeature.WriteMapNullValue
,SerializerFeature.WriteNonStringKeyAsString
), "UTF-8");//格式化文件数据
HashMap<String, Object> result = createResult(true, "操作成功。");
return result;
} catch (Exception e) {
log.error("系统错误!" + e.getMessage(), e);
return createResult(Boolean.FALSE, "操作失败。");
}
}
SerializerFeature attributes:
For more details, please refer to the detailed explanation of fastjson SerializerFeature
Method 2: Use the entity class tableEntity conversion here
引入文件:
import com.alibaba.fastjson.JSON;
@RequestMapping("/tojson")
@ResponseBody
public Map tojson(@RequestParam HashMap<String,String> paraMap) {
try {
String sql = "select * from table";
List<tableEntity> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper(tableEntity.class));
Map<String, Object> objectMap=new HashMap<>();
objectMap.put("data", list);
objectMap.put("createtime", new Timestamp(System.currentTimeMillis()).toString());
String filename=paraMap.get("bzcode")+".json";
FileUtils.writeStringToFile(new File(ApplicationHome.getApplicationConfig()+"/fieldjson/"+filename)
,JSON.toJSONString(objectMap,true), "UTF-8");//一般处理,没有格式化文件数据
HashMap<String, Object> result = createResult(true, "操作成功。");
return result;
} catch (Exception e) {
log.error("系统错误!" + e.getMessage(), e);
return createResult(Boolean.FALSE, "操作失败。");
}
}
3 , jsontodb
jsontodb thought steps:
1. Determine whether there is a file of json string format data. If it does not exist, it will generate one by default.
2. Read the file of json string format data.
3. Convert it to the specified entity class.
4. Save the data into the database.
Note : The json string must be in the format of [{}], it cannot be parsed without []
Method 1:
引入文件:
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.JSONObject;
//想要调用其他类的方法,使用注解
@Autowired
Tbz4FieldInfoEntityController tbz4FieldInfoEntityController;
//复制,清空,导入 如果找不到文件,自动生成一个
@RequestMapping("/jsontodb")
@ResponseBody
public Map jsontodb(@RequestParam HashMap<String,String> paraMap, Model model) {
try {
//如果找不到文件,自动生成一个
String filename=paraMap.get("bzcode")+".json";
if(!new File(ApplicationHome.getApplicationConfig()+"/fieldjson/"+filename).exists()){
tbz4FieldInfoEntityController.tojson(paraMap);
}
/*String tablenew="table"+String.valueOf(System.currentTimeMillis());
String sqlcy="select * into "+tablenew+" from table";
jdbcTemplate.execute(sqlcy);
String sqldel="delete from table where bid='"+paraMap.get("bid")+"'";
jdbcTemplate.execute(sqldel);*/
String data= FileUtils.readFileToString(new File(ApplicationHome.getApplicationConfig()+"/fieldjson/"+filename),"UTF-8");
String json="["+data+"]";
List<Tbz4FieldInfoEntity> list =new LinkedList<>();
JSONArray picArray = new JSONArray(json);
list = JSONUtil.toList(new JSONArray(((JSONObject) picArray.get(0)).get("data").toString()), Tbz4FieldInfoEntity.class);
tbz4FieldInfoEntityDao.saveAll(list);
HashMap<String, Object> result = createResult(true, "操作成功。");
return result;
} catch (Exception e) {
log.error("系统错误!" + e.getMessage(), e);
return createResult(Boolean.FALSE, "操作失败。");
}
}
Method 2:
引入文件:
import com.alibaba.fastjson.JSON;
@RequestMapping("/jsontodb")
@ResponseBody
public Map jsontodb(@RequestParam HashMap<String,String> paraMap, Model model) {
try {
//如果找不到文件,自动生成一个
String filename=paraMap.get("bzcode")+".json";
if(!new File(ApplicationHome.getApplicationConfig()+"/fieldjson/"+filename).exists()){
tbz4FieldInfoEntityController.tojson(paraMap);
}
/*String tablenew="table"+String.valueOf(System.currentTimeMillis());
String sqlcy="select * into "+tablenew+" from table ";
jdbcTemplate.execute(sqlcy);
String sqldel="delete from table where bid='"+paraMap.get("bid")+"'";
jdbcTemplate.execute(sqldel);*/
String data= FileUtils.readFileToString(new File(ApplicationHome.getApplicationConfig()+"/fieldjson/"+filename),"UTF-8");
List<tableEntity> list =new LinkedList<>();
//Tbz4FieldInfoEntityController.FieldJson为类中静态类
Tbz4FieldInfoEntityController.FieldJson fieldJson = JSON.parseObject(data, Tbz4FieldInfoEntityController.FieldJson.class);
tbz4FieldInfoEntityDao.saveAll(fieldJson.data);
HashMap<String, Object> result = createResult(true, "操作成功。");
return result;
} catch (Exception e) {
log.error("系统错误!" + e.getMessage(), e);
return createResult(Boolean.FALSE, "操作失败。");
}
}
@Slf4j
@Controller
@RequestMapping(value = "/Tbz4FieldInfoEntityController")
public class Tbz4FieldInfoEntityController extends BaseControllerZfpt {
//字段命名一定要和json格式数据的标签一致
//@Data:注解在类上;提供类所有属性的 getting 和 setting 方法,
@Data
public static class FieldJson{
String createtime;
List<tableEntity> data;
}
}
**tips: Use when saving data in batches
commonApiDao.batchInsert(list);
higher efficiency
List<tableEntity> list =new LinkedList<>();
commonApiDao.batchInsert(list);
4. Related knowledge:
1. Annotation
@Data: Annotation on the class; provides the getting and setting methods of all the properties of the class, and also provides equals, canEqual, hashCode, and toString methods
@Setter: Annotate on the property; provide a setting method for the property
@Getter: Annotate on the attribute; provide a getting method for the attribute
@Log4j: Annotate on the class; provide a log4j log object with an attribute named log for the class
2. How can a class in Java call a method in another class?
java class has two methods
One is the class method, which is modified with static,
One is the instance method, which is the method without static modification.
Class methods can be called at the same time as [class name. method name].
The instance method must be an instance of [new a class] first, and then called by way of [instance. method name]. E.g:
public class MethodCall
{
public static void main(String[] args)
{
Test.sayStatic();
Test test = new Test();
test.sayInstance();
}
}
class Test
{
public static void sayStatic()
{
System.out.println("这是一个静态方法。");
}
public void sayInstance()
{
System.out.println("这是一个实例方法。");
}
}