其他分享
首页 > 其他分享> > Json实现深克隆

Json实现深克隆

作者:互联网

1. 存在BO VO DO  DTO  ,  JPA操作风格, 面向对象落库 —— 值得深克隆

package clone;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * @Author: Ryan.Dou
 * @Date: 2022/5/23 21:53
 * @Description: VO、BO、DO、DTO 风格项目
 */
public class Demo {
    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 1.学生Ryan,课程Math
         *   假装这是一个BO, 被传到A系统-XXX方法
         */
        Student student = getStudent();
        /**
         * 2.A系统XXX方法要做什么
         *   (1) 对参数进行重新封装后调用B系统,比如Math改为  SH_Math
         *   (2)调用结束后,会把调用情况存到日志表(JPA操作风格,面向对象), 日志表存的是原始参数,不是封装后的参数
         *   (3)通过Json实现深拷贝,一份用来最后存日志表,一份用来重新封装调用B系统
         */
        String studentJsonString = getStudentJsonString(student,objectMapper);
        Student student1 = objectMapper.readValue(studentJsonString, Student.class);
        Student student2 = objectMapper.readValue(studentJsonString, Student.class);
        requestOtherSystem(student2);
        /**
         * 3.println
         * Student{name='Ryan', subject=Subject{subjectName='Math'}}——1525262377——1837760739
         * Student{name='StudentName_Ryan', subject=Subject{subjectName='SH_Math'}}——1418428263——2059904228
         */
        System.out.println(student1+"——"+student1.hashCode()+"——"+student1.getSubject().hashCode());
        System.out.println(student2+"——"+student2.hashCode()+"——"+student2.getSubject().hashCode());
        /**
         * 4.student1 存入日志表
         */

    }

    private static void requestOtherSystem(Student student2) {
        String subjectNameAfterBuild = buildParam(student2);
        student2.getSubject().setSubjectName(subjectNameAfterBuild);
    }

    private static String buildParam(Student student2) {
        student2.setName("StudentName_Ryan");
        return "SH_"+student2.getSubject().getSubjectName();
    }

    private static String getStudentJsonString(Student student,ObjectMapper objectMapper) throws JsonProcessingException {
        String s = objectMapper.writeValueAsString(student);
        return s;
    }

    private static Student getStudent() {
        Student student = new Student();
        Subject subject = new Subject();
        subject.setSubjectName("Math");
        student.setName("Ryan");
        student.setSubject(subject);
        return student;
    }
}

  

2.纯Map - Json , 面向SQL落库 —— new 个map就行

package clone;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Ryan.Dou
 * @Date: 2022/5/23 23:08
 * @Description: Map - Json 风格项目
 */
public class Demo2 {
    public static void main(String[] args) throws JsonProcessingException {
        /**
         * 1.上游系统的操作,直接对A系统XXX方法传来一个map
         *      key = student   字符串类型
         *      value =  {"name":"Ryan","subject":{"subjectName":"Math"}}   字符串类型
         */
        Map<String, Object> paramMap = getParamMap();
        /**
         * 2.A系统XXX方法要做什么
         *   (1) 对参数进行重新封装后调用B系统,比如Math改为  SH_Math
         *   (2)调用结束后,会把调用情况存到日志表(MAP操作风格,面向SQL), 日志表存的是原始参数,不是封装后的参数
         *   (3)通过Json实现深拷贝,一份用来最后存日志表,一份用来重新封装调用B系统
         */
        HashMap<Object, Object> reqMap = new HashMap<>();
        reqMap.putAll(paramMap);
        /**
         * 3.print
         * 1753447031
         * 1810132623
         * 打印真实hash地址,发现引用不同
         * 但是reqMap已经通过putall获取全部信息,拿去想怎么搞都行
         * 最后把paramMap拿去存日志表即可
         */
        System.out.println(System.identityHashCode(paramMap));
        System.out.println(System.identityHashCode(reqMap));
    }

    private static Map<String,Object> getParamMap() throws JsonProcessingException {
        Student student = getStudent();
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(student);
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("student",jsonString);
        System.out.println(jsonString);
        return paramMap;
    }

    private static Student getStudent() {
        Student student = new Student();
        Subject subject = new Subject();
        subject.setSubjectName("Math");
        student.setName("Ryan");
        student.setSubject(subject);
        return student;
    }
}

  

标签:克隆,student,实现,student2,Json,Student,new,Math,subject
来源: https://www.cnblogs.com/douziming/p/16304023.html