<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
import org.apache.ibatis.type.Alias;
@Alias(value="UserDo")
public class UserDo {
private Integer id;
private String name;
private String password;
// 下面是 getter 和 setter 方法。。。
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;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Test
public void mytest(){
Gson gson = new Gson();
UserDo user = new UserDo();
user.setId(1);
user.setPassword("111");
user.setName("1");
String jsonObject = gson.toJson(user);
System.out.println(jsonObject);
UserDo userb = gson.fromJson(jsonObject, UserDo.class);
System.out.println(userb);
}
参考资料