springBoot整合mybatis-plus

一、mybatis-plus的使用

此版本用的是3.x
一个表,对应一个实体,一个mapp.java,一个mapper.xml

1、jar包依赖


<!--postgresql数据库-->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

<!-- mybatis-plus begin start-->
<!-- 核心包-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-annotation</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.1.0</version>
</dependency>
<!-- mybatis-plus begin end-->

2、实体类

@Setter
@Getter
@TableName("dog")
public class Dog {

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField("name")
    private String name;

    @TableField("description")
    private String description;
}

3.Mapper接口


@Repository
public interface DogMapper extends BaseMapper<Dog> {

}

4、Mapper.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.financeserver.financeserver.wzj.finance.mapper.DogMapper">

    <resultMap id="BaseResultMap" type="com.financeserver.financeserver.wzj.finance.model.Dog">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, name, description
    </sql>
</mapper>

5、例子

// 主键查询
Dog dog2 = dogMapper.selectById(1);

// 查询
QueryWrapper<Dog> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "旺财");
List<Dog> dogs = dogMapper.selectList(queryWrapper);

// 主键修改
dog2.setDescription("修改");
Integer integer = dogMapper.updateById(dog2);

// 修改
Dog dog1 = new Dog();
dog1.setDescription("修改");
UpdateWrapper updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "旺财2");
integer = dogMapper.update(dog1, updateWrapper);

此处条件构造器的讲解,看大佬的博客
springboot + mybatis plus强大的条件构造器queryWrapper、updateWrapper

二、mybatis-plus 分页的使用

1、添加分页配置文件(若没有此配置文件,分页将不起效果)


@Configuration
public class MybatisPlusConfig {

  /**
   * @Description : mybatis-plus分页插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }
}

2、例子
使用queryWrapper查询:

@PostMapping(value = "/list-page")
public void selectListByPage() {
    QueryWrapper<Dog> queryWrapper = new QueryWrapper<>();
    queryWrapper.ne("id", 1);
    // 第几页
    Integer current = 1;
    // 每页条数
    Integer size = 1;
    Page page = new Page<>(current, size);
    IPage<Dog> iPage = dogMapper.selectPage(page, queryWrapper);
    List<Dog> list = iPage.getRecords();
    System.out.println("list.size " + list.size());
    System.out.println("Current " + iPage.getCurrent());// 当前页
    System.out.println("Page " + iPage.getPages());// 总页数
    System.out.println("Size " + iPage.getSize());// 每一页个数
    System.out.println("Total " + iPage.getTotal());// 总条数
}

优化为:

@PostMapping(value = "/list-page-user")
public PageResponse selectListByPage(@RequestBody PageRequest<User> request) {
    User user = request.getContent();
    QueryWrapper<Dog> queryWrapper = new QueryWrapper<>();
    queryWrapper.ne("id", 1);
    Page page = new Page<>(request.getCurrent(), request.getSize());
    IPage<Dog> iPage = dogMapper.selectPage(page, queryWrapper);
    PageResponse response = new PageResponse<User>();
    response.setData(page.getRecords());
    response.setCurrent((int) iPage.getCurrent());
    response.setSize((int) iPage.getSize());
    response.setPages((int) iPage.getPages());
    response.setTotal((int) iPage.getTotal());
    return response;
}

3、使用xml配置sql语句查询
此处使用xml配置,我遇到一个问题,由于我将mapper.xml放在了resources下,然后直接用分页查询导致报错:
org.apache.ibatis.binding.BindingException: Parameter ‘ew’ not found. Available parameters are [page, ea, param1, param2]
很奇怪为什么会是这样的报错。
然后我将分页去除后发现报错:Invalid bound statement (not found) 无效的绑定,排查后发现是少了配置声明。

yml配置:

#mybatis
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml

为了方便controller和service合起来写了

@PostMapping(value = "/list-page-dog-sql")
public PageResponse selectListByPageBySql(@RequestBody PageRequest<Dog> request) {
    PageResponse response = new PageResponse<>();
    Dog dog = request.getContent();
    if (dog == null) {
        dog = new Dog();
    }
    // 不进行分页
    if (request.getCurrent() == null || request.getSize() == null ||
            request.getCurrent() < 1 || request.getSize() < 1) {
        List<Dog> list = dogMapper.selectListAll(dog);
        response.setData(list);
        return response;
    }
    // 分页查询
    Page page = new Page<>(request.getCurrent(), request.getSize());
    IPage<Dog> iPage = dogMapper.selectListAll(page, dog);
    response.setData(page.getRecords());
    response.setCurrent((int) iPage.getCurrent());
    response.setSize((int) iPage.getSize());
    response.setPages((int) iPage.getPages());
    response.setTotal((int) iPage.getTotal());
    return response;
}

mapper.java

/**
 * 3.x 的 page 可以进行取值
 * 如果入参是有多个,需要加注解@Param指定参数名才能在xml中取值
 * 自定义 page 类必须放在入参第一位
 * 返回值可以用 IPage<T> 接收 也可以使用入参的 MyPage<T> 接收
 *
 * @param  page
 * @return 分页数据
 */
IPage<Dog> selectListAll(Page page, @Param("ea") Dog dog);

List<Dog> selectListAll(@Param("ea") Dog dog);

mapper.xml


<select id="selectListAll" parameterType="com.financeserver.wzj.finance.model.Dog" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from dog
    where 1 = 1
    <if test="ea.description != null">
        and description = #{ea.description}
    </if>
</select>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值