@jsonignore的作用

作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。

如下:

package com.hzboy.orm;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonManagedReference;

/**
 * 系统用户实体类
 * @author tinner
 *
 */
@Entity(name = "com.hzboy.orm.User")
@Table(name = "sys_user")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User extends BaseEntity {

    /**
     *
     */
    private static final long serialVersionUID = -1343842065506227012L;

    public User() {

    }

    public User(Integer id) {
        this.id = id;
    }

    // 登录名
    private String loginName;
    // 密码123456-b594510740d2ac4261c1b2fe87850d08
    private String pswd;
    // 姓名
    private String nickName;
    // 性别
    private Short sex;
    // 年龄段
    private String ageStage;
    // 是否系统管理员
    private boolean systemAdmin;
    //联系电话
    private  String  tel;
    //Email
    private  String  email;
    //是否工程师
    private  Boolean  isEnginner;
    //是否前端客户    是则显示前台html,但同时保留后台相关模块
    private  Boolean  isFrontUser;
    //相关部门
    private  Department  department;
    //相关部门前台描述
    private  String   departmentName;
    private  Integer  departmentId;
    //用户编号
    private  String  userCode;
    //附加用于导入
    private  String  idCode;
    //附加的部门信息  用于计算
    private  String  departmentCode;
    @Column(name = "login_name")
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    @Column(name = "pswd")
    public String getPswd() {
        return pswd;
    }

    public void setPswd(String pswd) {
        this.pswd = pswd;
    }

    @Column(name = "nick_name")
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Column(name = "sex")
    public Short getSex() {
        return sex;
    }

    public void setSex(Short sex) {
        this.sex = sex;
    }

    @Column(name = "age_stage")
    public String getAgeStage() {
        return ageStage;
    }

    public void setAgeStage(String ageStage) {
        this.ageStage = ageStage;
    }

    @Column(name = "system_admin")
    public boolean isSystemAdmin() {
        return systemAdmin;
    }

    public void setSystemAdmin(boolean systemAdmin) {
        this.systemAdmin = systemAdmin;
    }

    private List<Role> roles;

    @ManyToMany(fetch = FetchType.LAZY, cascade = (CascadeType.REMOVE))
    @JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    private Integer[] roleIds;

    @Transient
    public Integer[] getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(Integer[] roleIds) {
        this.roleIds = roleIds;
    }
    @Column(name="email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    @Column(name="is_enginner")
    public Boolean getIsEnginner() {
        return isEnginner;
    }

    public void setIsEnginner(Boolean isEnginner) {
        this.isEnginner = isEnginner;
    }
    @Column(name="is_front_user")
    public Boolean getIsFrontUser() {
        return isFrontUser;
    }

    public void setIsFrontUser(Boolean isFrontUser) {
        this.isFrontUser = isFrontUser;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
    @JsonIgnore
    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinColumn(name="department_id")
    public Department getDepartment() {
        return department;
    }
    @JsonManagedReference
    public void setDepartment(Department department) {
        this.department = department;
    }

    @Transient
    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    @Transient
    public Integer getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Integer departmentId) {
        this.departmentId = departmentId;
    }
    @Column(name="user_code")
    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }
    @Column(name="id_from_import")
    public String getIdCode() {
        return idCode;
    }

    public void setIdCode(String idCode) {
        this.idCode = idCode;
    }
    public String getDepartmentCode() {
        return departmentCode;
    }

    public void setDepartmentCode(String departmentCode) {
        this.departmentCode = departmentCode;
    }
}

上个实体User中的  department属性就在getDepartment属性上加了  @JsonIgnore,这个时候从后台推数据到前台的时候,就会把department这个引用属性给忽略掉。

时间: 2024-12-28 06:25:03

@jsonignore的作用的相关文章

Hibernate JPA中@Transient、@JsonIgnoreProperties、@JsonIgnore、@JsonFormat、@JsonSerialize等注解解释

@jsonignore的作用作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响. http://www.cnblogs.com/toSeeMyDream/p/4437858.html 当表间有One2Many或Many2One时,会发生无限循环的场景,如何破? 只要在Set方法前增加以下注解即可: @JsonIgnore public Set xxxs() { return this.xxxYyyy; } http://www.cnblogs.com/tomp

spring/spring boot/spring mvc中用到的注解

在spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了解决EJB等大型企业框架对应用程序的侵入性,因此大量依靠配置文件来"非侵入式"得给POJO增加功能,然而,从Spring 3.x开始,Spring被外界最为诟病的一点就是配置繁多,号称"配置地狱",各种xml文件,出了问题非常难排查.从Spring 4.x开始,Spring.io提供了三种方式编织Bean: 利用注解:隐式配置,例如:@Autowired.

SpringBoot注解大全(转)

原文链接:[springBoot系列]--springBoot注解大全 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration注解.其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文. @Configuration 等同于spring的XML配置文件:使用Java代码可以检查类型安全. @E

spring boot 注解大全

[springBoot系列]--springBoot注解大全 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration注解.其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文. @Configuration 等同于spring的XML配置文件:使用Java代码可以检查类型安全. @Enable

springboot注释详解

1.@SpringBootApplication 为复合注解,包含: * @Configuration:表示将该类作用springboot配置文件类. *@EnableAutoConfiguration:表示程序启动时,自动加载springboot默认的配置.例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库".你可以将@EnableAutoConfiguration或者@SpringBootA

SpringBoot注解大全 转

一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration注解.其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文. @Configuration 等同于spring的XML配置文件:使用Java代码可以检查类型安全. @EnableAutoConfiguration 自动配置. @Compon

jsonFiled与jsonproperty

@JSONField注解的使用<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.28</version></dependency> FastJson中的注解@JSONField,一般作用在get/set方法上面,常用的使用场景有下面三个:修改和json字符串的字段映射[name]格式化数

常用注解

1.@SuppressWarnings("all"):用于抑制编译器产生警告信息 每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了,我们可以在方法前添加 @SuppressWarnings("unused") 去除这些“感叹号”. 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration注解.其中@Compone

springBoot注解大全

? 一.注解(annotations)列表 @SpringBootApplication: 包含了@ComponentScan.@Configuration和@EnableAutoConfiguration注解.其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文. @Configuration 等同于spring的XML配置文件:使用Java代码可以检查类型安全. @EnableAutoConfiguration 自动配置. @Com