其作用是jackson 實體轉json 為NULL的字段不參加序列化(即不顯示)
如果在某字段前添加了此注解,如果此字段在輸出的時候為null時,系統將不顯示此字段。
一、沒加此注解時,執行一個簡單的查詢操作
1. User.java類內容如下:
package com.example.simple.model; /* * 用戶表 * */ import java.io.Serializable; import java.sql.Blob; import java.util.Date; import java.util.List; public class SysUser implements Serializable { private static final long serialVersionUID = 6320941908222932112L ; /* * 用戶ID * */ private Long id; /* * 用戶 * */ private String userName; /* * 密碼 * */ private String userPassword; /* * 郵箱 * */ private String userEmail; /* * 簡介 * */ private String userInfo; /* * 頭像 * */ private byte[] headImg; /* * 創建時間 * */ private Date createTime; //一個用戶擁有一個角色 為實現自動映射一對一關系 因此需要加上SysRole字段 private SysRole role; private List<SysRole> roleList; public List<SysRole> getRoleList() { return roleList; } public void setRoleList(List<SysRole> roleList) { this.roleList = roleList; } public SysRole getRole() { return role; } public void setRole(SysRole role) { this.role = role; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String getUserInfo() { return userInfo; } public void setUserInfo(String userInfo) { this.userInfo = userInfo; } public byte[] getHeadImg() { return headImg; } public void setHeadImg(byte[] headImg) { this.headImg = headImg; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
測試代碼如下:
@Test public void testSelectById(){ //獲取會話 SqlSession sqlSession = getSqlSession(); try{ //獲取UserMapper接口 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); SysUser user = userMapper.selectById(1020L); Assert.assertNotNull(user); Assert.assertEquals("test-selective",user.getUserName()); }finally { sqlSession.close(); } }
查詢結果如下:
DEBUG [main] - Cache Hit Ratio [com.example.simple.mapper.RoleMapper]: 0.0
DEBUG [main] - ==> Preparing: select * from sys_user where id = ?
DEBUG [main] - ==> Parameters: 1020(Long)
TRACE [main] - <== Columns: id, user_name, user_password, user_email, user_info, head_img, create_time
TRACE [main] - <== Row: 1020, test-selective, 123456, null, <<BLOB>>, <<BLOB>>, 2020-12-09 21:04:13
DEBUG [main] - <== Total: 1
二、如果加上此注解
1. User.java類內容如下:
package com.example.simple.model; /* * 用戶表 * */ import com.fasterxml.jackson.annotation.JsonInclude; import java.io.Serializable; import java.sql.Blob; import java.util.Date; import java.util.List; public class SysUser implements Serializable { private static final long serialVersionUID = 6320941908222932112L ; /* * 用戶ID * */ private Long id; /* * 用戶 * */ private String userName; /* * 密碼 * */ private String userPassword; /* * 郵箱 * */ @JsonInclude(JsonInclude.Include.NON_NULL) private String userEmail; /* * 簡介 * */ private String userInfo; /* * 頭像 * */ private byte[] headImg; /* * 創建時間 * */ private Date createTime; //一個用戶擁有一個角色 為實現自動映射一對一關系 因此需要加上SysRole字段 private SysRole role; private List<SysRole> roleList; public List<SysRole> getRoleList() { return roleList; } public void setRoleList(List<SysRole> roleList) { this.roleList = roleList; } public SysRole getRole() { return role; } public void setRole(SysRole role) { this.role = role; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String getUserInfo() { return userInfo; } public void setUserInfo(String userInfo) { this.userInfo = userInfo; } public byte[] getHeadImg() { return headImg; } public void setHeadImg(byte[] headImg) { this.headImg = headImg; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
測試代碼一樣,控制台執行結果也和上面一樣,只是在頁面上顯示時email為空的值不在頁面上顯示,這個部分暫時還沒實現,后面實現了再更新。