通过json动态的给转换后的对象添加属性

博客讲述了在web开发中遇到的一个问题:数据库user表无年龄字段,但前端需要显示用户年龄。通过计算生日和当前时间得到年龄,并探讨了如何将动态计算的年龄与用户对象关联。最终,作者利用JSON转换时动态添加属性的方法解决了这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题的需求如下:

数据库中的表的属性如下user_tb

 

前端的显示效果如下

 

在做这个项目的时候遇到了一个问题,数据库中的user的属性没有年龄的这个字段,只有一个date的日期属性(即生日),而前端的页面显示的生日所对应的当前的年龄,是一个动态值,要根据生日和当前的时间计算出,然后传到前台页面。这就出现了几个问题,如何对应的存储计算出来的年龄,计算方法容易办,写一个计算的工具类就可以实现,传入一个出生年月的Date,根据当前的时间计算出一个年龄返回。关键是如何存储和对应的取出:

因为user没有这个年龄的属性,循环的取出计算后没办法对应的保存,另外存储,取又是一个问题,曾想过动态的给user加一个属性,也没发现解决办法,后来在使用json的时候看到了json在转换的时候动态给json对象添加属性的方法,参照这个方法,算是解决了这个问题,在此分享出来,希望可以帮助像我这个菜鸟,O(_)O哈哈哈~

主要代码如下:

计算年龄的工具

package com.util;

 

import java.util.Date;

import java.text.SimpleDateFormat;

 

public class Calc {

public static int jisuannianling(Date date2){

//通过输入日期来获取年龄的

Date date = new Date(System.currentTimeMillis());

SimpleDateFormat sfYear = new SimpleDateFormat("yyyy");

SimpleDateFormat sfMonth = new SimpleDateFormat("MM");

SimpleDateFormat sfDat = new SimpleDateFormat("dd");

//取出生日Date中的年月日

String byear = sfYear.format(date2);

String bmonth = sfMonth.format(date2);

String bday = sfDat.format(date2);

System.out.println("年"+byear+"月"+bmonth+"日"+bday);

//取出当前系统的年、月、

String year = sfYear.format(date);

String month = sfMonth.format(date);

String day = sfDat.format(date);

System.out.println("年"+year+"月"+month+"日"+day);

//年龄

 int sui=0;

//要计算的数据

//生日的信息

int birthyear =Integer.parseInt(byear);

int birthMonth = Integer.parseInt(bmonth);

int birthday = Integer.parseInt(bday);

//今天的时间

int nowyear = Integer.parseInt(year);

int nowMonth = Integer.parseInt(month);

int nowday = Integer.parseInt(day);

//待返回的生日

sui = nowyear - birthyear;

//比较年份

if(nowyear >=birthyear){

//如果年份大于生日年份,比较月份

if(nowMonth>=birthMonth){

//比较日

if(nowday>=birthday){//如果今天日期大于生日日期

return sui;

}else if(nowday<birthday){//今日小于生日

return sui-1;

}

}else if(nowMonth<birthMonth){

//如果年份小于生日月份

return sui-1;

}

}

 

return sui;

 

}

}

 

User类的代码

package com.entity;

import java.sql.Timestamp;

 

import com.util.Calc;

/**

 * User entity. @author MyEclipse Persistence Tools

 */

public class User  implements java.io.Serializable {

    // Fields    

     private Integer id;

     private String name;

     private String pwd;

     private Timestamp birth;

/*

这个是关键的地方,在user里面加一个getAge()的方法,在方法中

*/

     public int getAge(){

     return Calc.jisuannianling(birth);

     }

    // Constructors

    /** default constructor */

    public User() {

    }

    /** full constructor */

    public User(String name, String pwd, Timestamp birth) {

        this.name = name;

        this.pwd = pwd;

        this.birth = birth;

    }

    // Property accessors

 

    public Integer getId() {

        return this.id;

    }

    public void setId(Integer id) {

        this.id = id;

    }

    public String getName() {

        return this.name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getPwd() {

        return this.pwd;

    }

    public void setPwd(String pwd) {

        this.pwd = pwd;

    }

 

    public Timestamp getBirth() {

        return this.birth;

    }

    public void setBirth(Timestamp birth) {

        this.birth = birth;

    }

}

User.hbm.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="com.entity.User" table="user_tb" catalog="test">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator class="identity" />

        </id>

        <property name="name" type="java.lang.String">

            <column name="name" length="100" />

        </property>

        <property name="pwd" type="java.lang.String">

            <column name="pwd" length="100" />

        </property>

        <property name="birth" type="java.sql.Timestamp">

            <column name="birth" length="19" />

        </property>

    </class>

</hibernate-mapping>

 

 

转换后的json如下

 

运行后的效果如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值