MyBatisPlus的ActiveRecord(活动记录)简介以及怎样使用
生活随笔
收集整理的這篇文章主要介紹了
MyBatisPlus的ActiveRecord(活动记录)简介以及怎样使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
Active Record(活動記錄),是一種領域模型模式,特點是一個模型類對應關系型數據庫中的一個表,而模型類的一個實例對應表中的一行記錄。
ActiveRecord 一直受動態語言(PHP 、Ruby等)的喜愛,而java作為準靜態語言,對于ActiveRecord只能感嘆其優雅,所以MP在AR道路上進行了一定的探索。
項目搭建專欄:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37194
基礎搭建:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89407994
實現
如何使用AR模式
讓實體類繼承Model類并實現主鍵指定方法。
找到我們的實體類Employee
使其繼承Model<Model>,然后重寫其方法
/**** 必須指定當前實體類的主鍵屬性*/@Overrideprotected Serializable pkVal() {// TODO Auto-generated method stubreturn id;}并將當前實體類的主鍵返回。
完整Employee代碼:
package com.badao.beans;import java.io.Serializable;import com.baomidou.mybatisplus.activerecord.Model; import com.baomidou.mybatisplus.annotations.TableField; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.enums.IdType; import com.mchange.util.FailSuppressedMessageLogger;@TableName(value="employee") public class Employee? extends Model<Model>{@TableId(value="id",type=IdType.AUTO)private Integer id;//@TableField(value="last_name")private String name;private String email;private Integer gender;private Integer age;@TableField(exist=false)private String remark;public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getGender() {return gender;}public void setGender(Integer gender) {this.gender = gender;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}/**** 必須指定當前實體類的主鍵屬性*/@Overrideprotected Serializable pkVal() {// TODO Auto-generated method stubreturn id;}}?
總結
以上是生活随笔為你收集整理的MyBatisPlus的ActiveRecord(活动记录)简介以及怎样使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatisPlus条件构造器Cond
- 下一篇: Python中通过PyPDF2实现PDF