通用Mapper的使用

<!--通用mapper起步依赖-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.0.4</version>
</dependency>

在Dao的接口上继承tk包下的Mapper

import tk.mybatis.mapper.common.Mapper;
public interface BrandMapper extends Mapper<Brand> {}

在启动类上添加 开启通用Mapper的包扫描

import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan(basePackages = {"com.changgou.goods.dao"}) //开启通用Mapper的包扫描

 通用mapper的基础操作

//增加
brandMapper.insertSelective(brand);
//删除
brandMapper.deleteByPrimaryKey(id);
//修改
brandMapper.updateByPrimaryKeySelective(brand);
//查询
brandMapper.selectByPrimaryKey(id);;

根据条件查询

//自定义条件搜索对象Example
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();//条件构造器
        if (brand != null) {
            //brand.name!=null 根据名字模糊查询 where name like '%华%'
            if(StringUtil.isNotEmpty(brand.getName())){
                criteria.andLike("name","%"+brand.getName()+"%");
            }
            //brand.letter!=null 根据首字母进行查询
            if(StringUtil.isNotEmpty(brand.getLetter())){
                criteria.andEqualTo("letter",brand.getLetter());
            }
        }

        return brandMapper.selectByExample(example);

条件分页查询

 //分页
 PageHelper.startPage(page,size);
 //自定义条件搜索对象Example
 Example example = new Example(Brand.class);
 Example.Criteria criteria = example.createCriteria();//条件构造器
 if (brand != null) {
     //brand.name!=null 根据名字模糊查询 where name like '%华%'
     if(StringUtil.isNotEmpty(brand.getName())){
          criteria.andLike("name","%"+brand.getName()+"%");
      }
      //brand.letter!=null 根据首字母进行查询
      if(StringUtil.isNotEmpty(brand.getLetter())){
          criteria.andEqualTo("letter",brand.getLetter());
       }
  }
Page<Brand> page= (Page<Brand>)brandMapper.selectByExample(example);
return new PageResult(page.getTotal(), page.getResult());	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值