SpringBoot整合MybatisPlus

x33g5p2x  于2021-11-26 转载在 Spring  
字(3.7k)|赞(0)|评价(0)|浏览(577)

一、新建SpringBoot工程

二、添加MysqlBatisPlus相关依赖

  • 方式一
    到maven仓库找MysqlBatisPlus的依赖
  1. <!--mybatisplus集成springboot依赖-->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>3.4.2</version>
  6. </dependency>
  7. <!--jdbc连接数据库驱动-->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. </dependency>
  • 方式二
    创建springboot工程的时候使用阿里云的地址

三、配置相关信息

  1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  2. spring.datasource.url=jdbc:mysql://localhost:3306/whynode?serverTimezone=UTC
  3. spring.datasource.username=root
  4. spring.datasource.password=why0417
  5. #表名前缀,如果表没有前缀可以不需要这条配置
  6. mybatis-plus.global-config.db-config.table-prefix=t_

四、创建实体类和接口

  • 实体类
  1. @Data
  2. public class Student {
  3. private Integer id;
  4. private String name;
  5. private String sex;
  6. private Integer age;
  7. }
  • 接口
  1. @Mapper
  2. public interface StudentDao extends BaseMapper<Student> {
  3. /* BaseMapper: MybatisPlus已经给我们提供了一些基本的增删改查语句 BaseMapper源码见文章末尾 */
  4. }

五、测试

  • 测试类
  1. @SpringBootTest
  2. class Ch03MybatisplusApplicationTests {
  3. @Resource
  4. private StudentDao studentDao;
  5. @Test
  6. void contextLoads() {
  7. Student student = studentDao.selectById(2);
  8. System.out.println(student);
  9. }
  10. }
  • 测试结果

BaseMapper源码

  1. /** * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能 * <p>这个 Mapper 支持 id 泛型</p> * * @author hubin * @since 2016-01-23 */
  2. public interface BaseMapper<T> extends Mapper<T> {
  3. /** * 插入一条记录 * * @param entity 实体对象 */
  4. int insert(T entity);
  5. /** * 根据 ID 删除 * * @param id 主键ID */
  6. int deleteById(Serializable id);
  7. /** * 根据 columnMap 条件,删除记录 * * @param columnMap 表字段 map 对象 */
  8. int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
  9. /** * 根据 entity 条件,删除记录 * * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */
  10. int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  11. /** * 删除(根据ID 批量删除) * * @param idList 主键ID列表(不能为 null 以及 empty) */
  12. int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
  13. /** * 根据 ID 修改 * * @param entity 实体对象 */
  14. int updateById(@Param(Constants.ENTITY) T entity);
  15. /** * 根据 whereEntity 条件,更新记录 * * @param entity 实体对象 (set 条件值,可以为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */
  16. int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
  17. /** * 根据 ID 查询 * * @param id 主键ID */
  18. T selectById(Serializable id);
  19. /** * 查询(根据ID 批量查询) * * @param idList 主键ID列表(不能为 null 以及 empty) */
  20. List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
  21. /** * 查询(根据 columnMap 条件) * * @param columnMap 表字段 map 对象 */
  22. List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
  23. /** * 根据 entity 条件,查询一条记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */
  24. T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  25. /** * 根据 Wrapper 条件,查询总记录数 * * @param queryWrapper 实体对象封装操作类(可以为 null) */
  26. Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  27. /** * 根据 entity 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */
  28. List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  29. /** * 根据 Wrapper 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */
  30. List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  31. /** * 根据 Wrapper 条件,查询全部记录 * <p>注意: 只返回第一个字段的值</p> * * @param queryWrapper 实体对象封装操作类(可以为 null) */
  32. List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  33. /** * 根据 entity 条件,查询全部记录(并翻页) * * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) */
  34. <E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  35. /** * 根据 Wrapper 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 */
  36. <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

相关文章