下面所有用到的代码基于项目云图
依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
配置文件:
mybatis-plus.mapper-locations=classpath:mappers/*xml
mybatis-plus.type-aliases-package=com.nju.yuntu.domain
mybatis-plus.mapper-constant-mapper-prefix=mapper
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.mapper-locations=classpath:mappers/*xml:这个配置指定了 MyBatis Plus Mapper XML 文件的位置。classpath:mappers/*xml表示从类路径中查找名为*.xml的文件。这通常用于指定 Mapper 接口对应的 XML 文件的位置。mybatis-plus.type-aliases-package=com.nju.yuntu.domain:这个配置指定了 MyBatis Plus 要扫描的实体类别名包。在这个包中的实体类可以在 SQL 映射文件中使用别名而不是完整的类名。mybatis-plus.mapper-constant-mapper-prefix=mapper:这个配置指定了生成的 Mapper 接口的前缀。如果指定为mapper,则生成的 Mapper 接口的名称将以mapper为前缀。mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl:这个配置指定了 MyBatis 使用的日志记录实现。在这里,使用了org.apache.ibatis.logging.stdout.StdOutImpl,这意味着 MyBatis 将使用标准输出来记录日志信息。
Image 实体类设计
@TableName("image")
public class Image {
@TableId
private int uid; // 属于的用户id,主键
@TableId
@TableField(value = "imageId")
private int imageId; // 图片的id,主键
@TableField(value = "imageName")
private String imageName; // 图片的名字
private int size; // 图片的大小
@TableField(value = "uploadTime")
private String uploadTime; // 图片的上传时间
@TableField(value = "contentType")
private String contentType; // 图片的类型
private String path; // 图片在磁盘存储的路径
private String md5; // 防止相同图片存储的检验码
}
mapper层
public interface ImageDao extends BaseMapper<Image> {}
在业务逻辑层,我们采用Dao层接口,Service层接口,Service层实现类组成。Service实现类通过继承Dao层以及实现Service接口,来实现后端业务逻辑。
首先,Dao层通过继承BaseMapper抽象类来实现Mybatis-plus层框架提供的通用Mapper接口。下面是BaseMapper中一些主要的API:
1. 插入记录:
-
int insert(T entity):插入一条记录,返回影响的行数。
-
int insertAllColumn(T entity):插入一条记录,包括所有字段,返回影响的行数。
- 根据 ID 更新记录:
- int updateById(T entity):根据主键 ID 更新记录,返回影响的行数。
- 根据 ID 删除记录:
- int deleteById(Serializable id):根据主键 ID 删除记录,返回影响的行数。
- 根据条件删除记录:
- int delete(Wrapper wrapper):根据条件删除记录,返回影响的行数。
- 根据 ID 查询记录:
- T selectById(Serializable id):根据主键 ID 查询一条记录。
- 根据条件查询记录列表:
l List selectList(Wrapper wrapper):根据条件查询记录列表。
Ø 根据条件查询记录总数:
- int selectCount(Wrapper wrapper):根据条件查询记录总数。
- 根据条件查询一条记录,多条抛出异常:
- T selectOne(Wrapper wrapper):根据条件查询一条记录,如果匹配多条会抛出异常。
- 根据 ID 列表查询多条记录:
- List selectBatchIds(Collection<? extends Serializable> idList):根据 ID 列表查询多条记录,如果 ID 列表为空则返回空列表。
- 根据 ID 列表查询多条记录,支持分批查询:
- List selectBatchIds(Collection<? extends Serializable> idList, int batchSize):根据 ID 列表查询多条记录,可以指定批量查询的批次大小。
Service层
public interface ImageService extends IService<Image> {
}
public class ImageServiceImpl extends ServiceImpl<ImageDao, Image> implements ImageService{}
在Service接口中每个Service接口继承了IService接口,定义了业务逻辑的方法,下面是IService中一些主要的API:
-
T insert(T entity):创建一条记录。
-
boolean updateById(T entity):根据 ID 更新记录。
-
boolean deleteById(Serializable id):根据 ID 删除记录。
-
T selectById(Serializable id):根据 ID 查询记录。
-
List selectList(Wrapper wrapper):根据条件查询记录列表。
-
int selectCount(Wrapper wrapper):根据条件查询记录总数。
-
T selectOne(Wrapper wrapper):根据条件查询一条记录,如果匹配多条会抛出异常。
-
List selectBatchIds(Collection<? extends Serializable> idList):根据 ID 列表查询多条记录,如果 ID 列表为空则返回空列表。
-
List selectBatchIds(Collection<? extends Serializable> idList, int batchSize):根据 ID 列表查询多条记录,可以指定批量查询的批次大小。
Controller层
@PostMapping("/findImageByPage")
@ResponseBody
public PageBean<Image> findImageByPage(@RequestBody Map<String, Object> requestBody) throws ServletException, IOException {
String username = (String) requestBody.get("username");
System.out.println(username);
Integer pageNum = (Integer) requestBody.get("pageNum");
Integer pageSize = (Integer) requestBody.get("pageSize");
Integer currentPage = (Integer) requestBody.get("currentPage");
PageBean<Image> pb = imageService.findImageByPage(username, pageNum, pageSize, currentPage);
return pb;
}