SpringBoot整合MybatisPlus

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

一、新建SpringBoot工程

二、添加MysqlBatisPlus相关依赖

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

三、配置相关信息

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

四、创建实体类和接口

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

五、测试

  • 测试类
@SpringBootTest
class Ch03MybatisplusApplicationTests {
    @Resource
    private StudentDao studentDao;
    @Test
    void contextLoads() {
        Student student = studentDao.selectById(2);
        System.out.println(student);
    }
}
  • 测试结果

BaseMapper源码

/** * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能 * <p>这个 Mapper 支持 id 泛型</p> * * @author hubin * @since 2016-01-23 */
public interface BaseMapper<T> extends Mapper<T> {

    /** * 插入一条记录 * * @param entity 实体对象 */
    int insert(T entity);

    /** * 根据 ID 删除 * * @param id 主键ID */
    int deleteById(Serializable id);

    /** * 根据 columnMap 条件,删除记录 * * @param columnMap 表字段 map 对象 */
    int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /** * 根据 entity 条件,删除记录 * * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * 删除(根据ID 批量删除) * * @param idList 主键ID列表(不能为 null 以及 empty) */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /** * 根据 ID 修改 * * @param entity 实体对象 */
    int updateById(@Param(Constants.ENTITY) T entity);

    /** * 根据 whereEntity 条件,更新记录 * * @param entity 实体对象 (set 条件值,可以为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

    /** * 根据 ID 查询 * * @param id 主键ID */
    T selectById(Serializable id);

    /** * 查询(根据ID 批量查询) * * @param idList 主键ID列表(不能为 null 以及 empty) */
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /** * 查询(根据 columnMap 条件) * * @param columnMap 表字段 map 对象 */
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /** * 根据 entity 条件,查询一条记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * 根据 Wrapper 条件,查询总记录数 * * @param queryWrapper 实体对象封装操作类(可以为 null) */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * 根据 entity 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * 根据 Wrapper 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * 根据 Wrapper 条件,查询全部记录 * <p>注意: 只返回第一个字段的值</p> * * @param queryWrapper 实体对象封装操作类(可以为 null) */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * 根据 entity 条件,查询全部记录(并翻页) * * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) */
    <E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * 根据 Wrapper 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 */
    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

相关文章