mybatis-plus的使用
一、SpringBoot项目中引入依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.1.4</version>
</dependency>
二、Dao层中继承类BaseMapper<T>
@Repository
public interface ActivityRecordInfoDao extends BaseMapper<ActivityRecordInfo> {
}
三、BaseMapper<T>
介绍几种常用的方法:
1、插入数据:Integer insert(T var1);
2、更新数据 :Integer updateById(@Param("et") T var1); 传入一个实体类,对更新实体类中不为null的字段。
3、查询:T selectById(Serializable var1);
4、删除:Integer deleteById(Serializable id); 根据主建id进行删除,但公司开发中一般不会涉及到删除数据。
批量查询的方法:
1、List<T> selectList(
String sql = " "; EntityWrapper ew = new EntityWrapper(); // 自定义条件 where (sql) ew.where(sql); List<T> lists = activityRecordInfoDao.selectList(ew);
2、List<Map<String, Object>> selectMaps(
Map map = new HashMap();
map.put("字段1","对应值");
map.put("字段2","对应的值");
// where 字段1 = x and 字段2 = x
List<T> lists= activityRecordInfoDao.selectByMap(map);
3、List<T> selectPage(RowBounds rowBounds,
EntityWrapper ew = new EntityWrapper();
// 构建查询条件 {}占位符
ew.where("字段1={0}","值");
// 构建分页信息 (new RowBounds(offset, limit), ew)
List<T> activityRecordInfosPage = activityRecordInfoDao.selectPage(new RowBounds(0, 100),ew);
三、看到一篇写的比较全的博客
https://www.jianshu.com/p/5df97ea42978

浙公网安备 33010602011771号