Mybatis-Plus(簡稱MP)是一個 Mybatis 的加強工具,在 Mybatis 的基礎上只作加強不作改變,爲簡化開發、提升效率而生。數據庫
建立一個實體類app
@TableName("test_model")ide
public class TestClass extends Model<TestModel>{工具
}this
@TableName("test_model"),它是指定與數據庫表的關聯,這裏的註解意味着你的數據庫裏應該有一個名爲test_model的表與之對應spa
一、實體映射到表code
@TableId(value="id",type=IdType.AUTO) private Long id; @TableField("my_name") private String myName; @TableField(exist = false) private String yourName; /**該張表中不存在的字段*/ public String getMyName() { return remark; } public void setMyName(String myName) { this.myName= myName; } ..... /**指定主鍵*/ @Override protected Serializable pkVal() { return this.id; }
二、配置Mapperblog
public interface TumilyMapper extends BaseMapper<TuTask> { }
三、service開發
public interface ITumilyService extends IService<TuTask> { }
@Service public class TumilyServiceImpl extends ServiceImpl<TumilyMapper, TuTask> implements ITumilyService { }
* 查詢rem
TuTask task = iTumilyService.selectList(new EntityWrapper<TuTask>()...);/**後面接對應的條件*/
...
* 修改
TuTask task = new TuTask();
...
iTumilyService.update(task,new EntityWrapper<TuTask>()....);
* 新增
iTumilyService.insert(task);
...
* 刪除
iTumilyService.delete(new EntityWrapper<TuTask>());
...
eg:
iTumilyService.update(task,new EntityWrapper<TuTask>().eq("deptId",1).in(true,"userId",userIds).notIn("status",1));
TuTask = iTumilyService.selectOne(new EntityWrapper<TuTask>() .setSqlSelect("SUM(money) money").eq("id",vo.getId()));