您好,登錄后才能下訂單哦!
本篇文章為大家展示了mybatis-plus 出現(xiàn)僅返回部分字段的解決方法,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
mybatis-plus的代碼生成器會(huì)在實(shí)體類中生成數(shù)據(jù)庫(kù)所有字段,我們?nèi)ビ胢apper接口查詢時(shí),會(huì)返回?cái)?shù)據(jù)庫(kù)所有的字段。
但有些字段不是我們想要的,比如:deleted,所以我們可以在不需要的字段上面加@JsonIgnore注解,返回給前端的時(shí)候會(huì)自動(dòng)把這個(gè)字段去除。
補(bǔ)充知識(shí):Mybatis-Plus只查詢特定字段與創(chuàng)建子類方法
Mybatis-Plus查詢特定字段例子:
Seal seal = sealService.selectOne(
new EntityWrapper<Seal>().setSqlSelect("sealName").eq("sealId",auditProcess.getSealId()));
其中這里的seal是一個(gè)自定義類。"sealName"是數(shù)據(jù)庫(kù)表seal的一個(gè)字段,這句查詢只查到id為 auditProcess.getSealId() 的seal的名稱。其余字段為null
創(chuàng)建子類方法。
一般需要聯(lián)表的情況,又不想寫(xiě)sql語(yǔ)句,可以試下這種方法(至于效率,我也沒(méi)有測(cè)試過(guò))
上個(gè)例子
Manager類
/** * <p> * * </p> * * @author onee123 * @since 2019-03-03 */ @Data @EqualsAndHashCode(callSuper = true) @Accessors(chain = true) @TableName("_manager") public class Manager extends Model<Manager> { private static final long serialVersionUID = 1L; /** * 管理員id */ @TableId(value = "manager_id",type = IdType.UUID) private String managerId; /** * 賬號(hào) */ @TableField("manager_phone") private String managerPhone; /** * 密碼 */ @TableField("manager_pass") private String managerPass; /** * 姓名 */ @TableField("manager_name") private String managerName; /** * 郵箱 */ @TableField("manager_email") private String managerEmail; /** * 狀態(tài)(0:1-刪除:啟動(dòng)) */ @TableField("manager_status") private Integer managerStatus; /** * 權(quán)限id */ @TableField("role_id") private String roleId; /** * 部門(mén) */ @TableField("manager_department") private String managerDepartment; /** * 創(chuàng)建時(shí)間 */ @TableField("manager_create_time") private Date managerCreateTime; /** * 最近登陸時(shí)間 */ @TableField("manager_login_time") private Date managerLoginTime; @Override protected Serializable pkVal() { return this.managerId; } }
Seal類
/** * <p> * * </p> * * @author wihenne123 * @since 2020-04-24 */ @Data @EqualsAndHashCode(callSuper = true) @Accessors(chain = true) @TableName("_seal") public class Seal extends Model<Seal> { private static final long serialVersionUID = 1L; /** * 印章id */ @TableId(value="seal_id",type = IdType.UUID) private String sealId; /** * 印章名 */ @TableField("seal_name") private String sealName; /** * 圖片路徑 */ @TableField("picture_path") private String picturePath; /** * 創(chuàng)建時(shí)間 */ @TableField("create_time") private Date createTime; /** * 更新時(shí)間 */ @TableField("update_time") private Date updateTime; /** * (-1:0:1 - 刪除:停用:啟用) */ @TableField("seal_status") private Integer sealStatus; @Override protected Serializable pkVal() { return this.sealId; } }
AuditProcess類,其中managerId和sealId需要對(duì)應(yīng)上面兩個(gè)表。
/** * <p> * * </p> * * @author wihenne123 * @since 2020-04-24 */ @Data @EqualsAndHashCode(callSuper = true) @Accessors(chain = true) @TableName("_audit_process") public class AuditProcess extends Model<AuditProcess> { private static final long serialVersionUID = 1L; /** * 流程圖id */ @TableId(value = "audit_id", type = IdType.AUTO) private Integer auditId; /** * 流程名稱 */ @TableField("audit_name") private String auditName; /** * 審核順序(,隔開(kāi)-店長(zhǎng)用shopManager表示) */ @TableField("audit_sort") private String auditSort; /** * 狀態(tài)(-1:0:1-刪除:停用:啟用) */ @TableField("audit_status") private Integer auditStatus; /** * 創(chuàng)建時(shí)間 */ @TableField("create_time") private Date createTime; /** * 更新時(shí)間 */ @TableField("update_time") private Date updateTime; /** * 印章id(無(wú)則為0) */ @TableField("seal_id") private String sealId; /** * 創(chuàng)建人id */ @TableField("manager_id") private String managerId; /** * 印章x位置 */ @TableField("seal_x") private Double sealX; /** * 印章y位置 */ @TableField("seal_y") private Double sealY; /** * 印章大小 */ @TableField("seal_size") private Double sealSize; /** * 印章所在頁(yè)碼 */ @TableField("seal_page") private Integer sealPage; /** * 序號(hào)x位置 */ @TableField("num_x") private Double numX; /** * 序號(hào)y位置 */ @TableField("num_y") private Double numY; /** * 序號(hào)大小 */ @TableField("num_size") private Double numSize; /** * 序號(hào)所在頁(yè)碼 */ @TableField("num_page") private Integer numPage; /** * pdf文件demo路徑 */ @TableField("pdf_demo_path") private String pdfDemoPath; /** * 反饋文件路徑 */ @TableField("result_file_path") private String resultFilePath; /** * 最大打印次數(shù) */ @TableField("print_size") private Integer printSize; @Override protected Serializable pkVal() { return this.auditId; } }
這時(shí)候我只需要對(duì)應(yīng)id的類的名稱,所以我設(shè)置了vo類作為子類
然后在接口里面寫(xiě)轉(zhuǎn)換方法
/** * <p> * 服務(wù)實(shí)現(xiàn)類 * </p> * * @author wihenne123 * @since 2020-04-23 */ @Service public class AuditProcessServiceImpl extends ServiceImpl<AuditProcessMapper, AuditProcess> implements AuditProcessService { @Autowired ManagerService managerService; @Autowired SealService sealService; @Autowired AuditProcessService auditProcessService; @Override public List<AuditProcessVo> auditProcessToVo(List<AuditProcess> auditProcessList) { List<AuditProcessVo> auditProcessVos = new ArrayList<>(); for(AuditProcess auditProcess:auditProcessList){ //遍歷list auditProcessVos.add(auditProcessToVo(auditProcess)); } return auditProcessVos; } @Override public AuditProcessVo auditProcessToVo(AuditProcess auditProcess) { AuditProcessVo auditProcessVo = new AuditProcessVo(); BeanUtils.copyProperties(auditProcess,auditProcessVo); //復(fù)制進(jìn)vo類 Manager manager = managerService.selectOne( new EntityWrapper<Manager>().setSqlSelect("manager_name","manager_department").eq("manager_id",auditProcess.getManagerId())); //加入字段值 auditProcessVo.setManagerName(manager.getManagerName()); auditProcessVo.setManagerDepartment(manager.getManagerDepartment()); if(auditProcess.getSealId() != null){ Seal seal = sealService.selectOne( new EntityWrapper<Seal>().setSqlSelect("seal_name").eq("seal_id",auditProcess.getSealId())); if(seal != null){ auditProcessVo.setSealName(seal.getSealName()); } }else { auditProcessVo.setSealName("無(wú)"); } return auditProcessVo; } }
上述內(nèi)容就是mybatis-plus 出現(xiàn)僅返回部分字段的解決方法,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。