溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何理解springboot pojo對象日期屬性的問題

發(fā)布時(shí)間:2021-10-25 13:40:30 來源:億速云 閱讀:205 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“如何理解springboot pojo對象日期屬性的問題”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何理解springboot pojo對象日期屬性的問題”吧!

目錄
  • pojo 對象日期屬性

  • pojo 默認(rèn)值設(shè)置

    • pojo設(shè)置(推薦)

pojo 對象日期屬性

FeignClient 日期屬性與pojo保持一直,使用Date類型;

pojo 屬性值添加注解JsonFormat,前端拿到的屬性為格式化之后的值。

@JsonFormat(timezone = DateUtils.TIMEZONE, pattern = DateUtils.DATE_TIME_FORMATE)
private Date date;

pojo 默認(rèn)值設(shè)置

我們有時(shí)需要給POJO設(shè)置默認(rèn)值

pojo設(shè)置(推薦)

1、User

package com.xxx.firstboot.domain;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
    private int id;
    private String username = "";//設(shè)置默認(rèn)值
    private String password = "";//設(shè)置默認(rèn)值
}

2、UserController

@ApiOperation("添加用戶/測試POJO默認(rèn)值")
    @RequestMapping(value="/addUserWithNoParam",method=RequestMethod.POST)
    public boolean addUserWithNoParam() {
        return userService.addUserWithNoParam(new User());//只新建,不設(shè)值
    }

3、UserService

public boolean addUserWithNoParam(User user){
        return userDao.insertUserWithUserParam(user)>0?true:false;
    }

4、UserDao

public int insertUserWithUserParam(User user){
        return userMapper.insertUserWithUserParam(user);
    }

5、UserMapper

@Insert("INSERT INTO tb_user(username, password) VALUES(#{username},#{password})")
    public int insertUserWithUserParam(User user);

測試:查看數(shù)據(jù)庫

如何理解springboot pojo對象日期屬性的問題

如果數(shù)據(jù)庫也設(shè)置了默認(rèn)值,如下

如何理解springboot pojo對象日期屬性的問題

再次執(zhí)行上述程序,發(fā)現(xiàn)結(jié)果還是如上,因?yàn)閜ojo的username和password的值我們雖然沒有傳,但是默認(rèn)值在User類設(shè)為了"",這樣的話,傳到數(shù)據(jù)庫,實(shí)際上username并不為null,那么也不會采用mysql的默認(rèn)值了。

感謝各位的閱讀,以上就是“如何理解springboot pojo對象日期屬性的問題”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何理解springboot pojo對象日期屬性的問題這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI