菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
500
0

Mybatis-Plus

原创
05/13 14:22
阅读数 56256

Springboot使用Mybatis-Plus

引入依赖

pom.xml

<!--        mybatisplus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.4.1</version>
        </dependency>
<!--        mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

配置信息

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.211.132:3306/changgou_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456

mybatis-plus:
  type-aliases-package: com.xxx.pojo # 实体类包名
  mapper-locations: classpath:/mapper/*Mapper.xml # mapper文件地址
  global-config:
    db-config:
      id-type: input # 选择主键为自己输入
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 输出sql语句日志
    map-underscore-to-camel-case: true #开启小驼峰式命名匹配,就不用自已写resultMap了

实体类注解

@TableName("tb_xxx")声明该实体类对应表

@TableId(type = IdType.INPUT)声明对应主键

添加上注解方便使用其自生成的增删改查方法

继承通用Mapper

在Dao包下继承BaseMapper

public interface BrandMapper extends BaseMapper<Brand> {
}

注意在启动类添加上Mapper扫描注解,这样也就不用在Mapper接口上添加任何注解让spring扫描了

@MapperScan("com.xxx.dao")

简单增删改查

直接使用继承了BaseMapper接口的方法进行单条件增删改查

查询全部:xxxMapper.selectList(null)

根据id查询:xxxMapper.selectById(id)

新增数据:xxxMapper.insert(xxx)

·······

多条件查询(条件构造器的使用)

条件构造器:QueryWrapper<Brand>

常用条件参数:

clip_image001.png

简单使用,例如多条件查询(多条件模糊查询):

QueryWrapper<Brand> wrapper = new QueryWrapper<>();
if(brand!=null) {
    // 品牌名称
    if (!StringUtils.isEmpty(brand.getName())) {
        wrapper.like("name", brand.getName());
    }
    // 品牌图片地址
    if (!StringUtils.isEmpty(brand.getImage())) {
        wrapper.like("image", brand.getImage());
    }
    // 品牌的首字母
    if (!StringUtils.isEmpty(brand.getLetter())) {
        wrapper.like("letter", brand.getLetter());
    }
    // 品牌id
    if (brand.getId()!=null) {
        wrapper.like("id", brand.getId());
    }
    // 排序
    if (brand.getSeq()!=null) {
        wrapper.like("seq", brand.getSeq());
    }
}
List<Brand> list = brandMapper.selectList(wrapper);
return list;

会自动添加上满足条件的and Like,并且会自动拼接上%%

分页查询

这里不再使用pagehelper作为分页插件,而是使用mybatis-plus自带的分页插件

先在启动类添加上分页插件:

@SpringBootApplication
@MapperScan("com.xxx.dao")
public class GoodsApplication {
    public static void main(String[] args) {
        SpringApplication.run(GoodsApplication.class,args);
    }
	//分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

之前使用的分页结果集是PageInfo,此插件使用IPage<?>接受分页结果

还有分页条件的构造器Page<?>注意不要导错包

使用分页:

@Override
public IPage<Brand> findPage(Integer page, Integer size,Brand brand) {
    QueryWrapper<Brand> wrapper = new QueryWrapper<>();
	//省略条件构造器的使用
    
    //这里的page对应当前页码,size对应页面大小
    Page<Brand> brandPage = new Page<>(page,size);
   	//使用接口中的方法时选择带有page的,将分页构造器穿进去,条件构造器视情况修改
    IPage<Brand> ipage = brandMapper.selectPage(brandPage,wrapper);
    //直接使用IPage接收
    return ipage;
}

发表评论

0/200
500 点赞
0 评论
收藏