1. <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
  2. <dependency>
  3. <groupId>com.google.code.gson</groupId>
  4. <artifactId>gson</artifactId>
  5. <version>2.8.1</version>
  6. </dependency>
  1. import org.apache.ibatis.type.Alias;
  2. @Alias(value="UserDo")
  3. public class UserDo {
  4. private Integer id;
  5. private String name;
  6. private String password;
  7. // 下面是 getter 和 setter 方法。。。
  8. public Integer getId() {
  9. return id;
  10. }
  11. public void setId(Integer id) {
  12. this.id = id;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getPassword() {
  21. return password;
  22. }
  23. public void setPassword(String password) {
  24. this.password = password;
  25. }
  26. }
  1. @Test
  2. public void mytest(){
  3. Gson gson = new Gson();
  4. UserDo user = new UserDo();
  5. user.setId(1);
  6. user.setPassword("111");
  7. user.setName("1");
  8. String jsonObject = gson.toJson(user);
  9. System.out.println(jsonObject);
  10. UserDo userb = gson.fromJson(jsonObject, UserDo.class);
  11. System.out.println(userb);
  12. }

参考资料