Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
创建一个实体类
@TableName("test_model")
public class TestClass extends Model<TestModel>{
}
@TableName("test_model"),它是指定与数据库表的关联,这里的注解意味着你的数据库里应该有一个名为test_model的表与之对应
1、实体映射到表
@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; }
2、配置Mapper
public interface MyMapper extends BaseMapper<MyTask> { }
3、service
public interface IMyService extends IService<MyTask> { }
@Service public class MyServiceImpl extends ServiceImpl<MyMapper, MyTask> implements IMyService { }
* 查询
MyTask task = iMyService.selectList(new EntityWrapper<MyTask>()...);/**后面接对应的条件*/
...
* 修改
MyTask task = new MyTask();
...
iMyService.update(task,new EntityWrapper<MyTask>()....);
* 新增
iMyService.insert(task);
...
* 删除
iMyService.delete(new EntityWrapper<MyTask>());
...
eg:
iMyService.update(task,new EntityWrapper<TuTask>().eq("deptId",1).in(true,"userId",userIds).notIn("status",1));
MyTask = iMyService.selectOne(new EntityWrapper<MyTask>()
.setSqlSelect("SUM(money) money").eq("id",vo.getId()));