springboot pojo對象日期屬性的問題
pojo 對象日期屬性
FeignClient 日期屬性與pojo保持一直,使用Date類型;
pojo 屬性值添加注解JsonFormat,前端拿到的屬性為格式化之后的值。
@JsonFormat(timezone = DateUtils.TIMEZONE, pattern = DateUtils.DATE_TIME_FORMATE) private Date date;
pojo 默認值設(shè)置
我們有時需要給POJO設(shè)置默認值
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è)置默認值 private String password = "";//設(shè)置默認值 }
2、UserController
@ApiOperation("添加用戶/測試POJO默認值") @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ù)庫
如果數(shù)據(jù)庫也設(shè)置了默認值,如下
再次執(zhí)行上述程序,發(fā)現(xiàn)結(jié)果還是如上,因為pojo的username和password的值我們雖然沒有傳,但是默認值在User類設(shè)為了"",這樣的話,傳到數(shù)據(jù)庫,實際上username并不為null,那么也不會采用mysql的默認值了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot詳解如何進行整合Druid數(shù)據(jù)源
Druid是阿里開發(fā)的一款開源的數(shù)據(jù)源,被很多人認為是Java語言中最好的數(shù)據(jù)庫連接池,本文主要介紹了SpringBoot整合Druid數(shù)據(jù)源的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06Java實現(xiàn)BP神經(jīng)網(wǎng)絡(luò)MNIST手寫數(shù)字識別的示例詳解
這篇文章主要為大家詳細介紹了Java實現(xiàn)BP神經(jīng)網(wǎng)絡(luò)MNIST手寫數(shù)字識別的相關(guān)方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實現(xiàn)
本文主要介紹了SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04