菜鸟怎么用Wrapper写多表自定义sql?


前言

本人最近刚在一家上海的公司实习,最近做了一个小项目,来分享一下。使用了mybatis-plus之后, 自定义SQL的同时也想使用Wrapper的便利


mybatis-plus的依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.4.0</version>
</dependency>

一、Wrapper 的使用

示例:我们通过一个简单的selectById来进行说明

二、使用步骤

1.首先给出一段sql语句

代码如下(示例):

<select id="selectById" resultType="com.backstage.pdfanalysis.table.CamelotTableCount">
    select guid,page,order_by,rows,cols,width,height,table_x0,table_x1,table_y0,table_y1,table_texts,table_all_info,entry_date
    from reportdb.[dbo].[t_pdf_tables_camelot]
    where guid = @guid
</select>

平时我们传入一个参数写法:

<select id="selectById" resultType="com.backstage.pdfanalysis.table.CamelotTableCount">
    select guid,page,order_by,rows,cols,width,height,table_x0,table_x1,table_y0,table_y1,table_texts,table_all_info,entry_date
    from reportdb.[dbo].[t_pdf_tables_camelot]
    where guid = #{guid}
</select>

#{}可以有效的防止sql注入
但是本次参数传参是用一个实体类里的属性写好的

于是我们可以使用Wrapper里的条件构造器进行拼接

首先改xml文件里的sql:

<select id="selectById" resultType="com.backstage.pdfanalysis.table.CamelotTableCount">
    select guid,page,order_by,rows,cols,width,height,table_x0,table_x1,table_y0,table_y1,table_texts,table_all_info,entry_date
    from reportdb.[dbo].[t_pdf_tables_camelot]
    ${ew.customSqlSegment}
</select>

${ew.customSqlSegment} 等于是用来拼接where后面的条件

Mapper层

public interface CamelotTableCountMapper {

    IPage<CamelotTableCount> selectById(Page<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);
}

里面做了一个分页page里包括(page和limit),wrapper是拼接后的sql中加入的参数

Service层

逻辑主要写在了service层

@Service
public class CamelotTableCountService {

    @Resource
    private CamelotTableCountMapper camelotTableCountMapper;

    public IPage<CamelotTableCount> selectById(Parameter parameter){
    	//new一个QueryWrapper对象
    	//QueryWrapper对象继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件  	
        QueryWrapper<CamelotTableCount> queryWrapper = new QueryWrapper<>();
		//getSearch_field内部通过“,”分割,我们可以通过url中search_field=大娃,二娃,三娃&search_word=大娃力大无穷,二娃千里眼,三娃刀枪不入
		//这样的形式来实现传入多个参数
        for(int i = 0 ; i< parameter.getSearch_field().size(); i++){
            String name = parameter.getSearch_field().get(i);
            String value = parameter.getSearch_word().get(i);
		//这里只先实现传入一个参数,如果获取到的参数等于guid
            if(name.equals("guid")){
            	//eq("name", "老王")--->where (name = '老王')
                queryWrapper.eq(name,value);
            }
        }
        Page<CamelotTableCount> page = new Page<>(parameter.getPage(),parameter.getLimit());
        //返回拼接后的page和querywrapper
        return  camelotTableCountMapper.selectById(page,queryWrapper);

    }

}

时间可以用between查同一天的

if (name.equals("rec_date_time")) {
                name = "rlp." + name;
                queryWrapper.between(name, value,value+" 23:59:59");
 }


这里是拼接后的sql

Controller层

@RestController
public class CamelotTableCountController {

    @Resource
    private final CamelotTableCountService camelotTableCountService;

    public CamelotTableCountController(CamelotTableCountService camelotTableCountService) {
        this.camelotTableCountService = camelotTableCountService;
    }

    @RequestMapping(value = "/v1/pdfanalysis/selectList/CamelotTableCount")
    public Object selectById(Parameter parameter){
        return Resp.success(this.camelotTableCountService.selectById(parameter));
}

url

该处是url网络请求的数据。

http://192.168.152.166:8686/v1/pdfanalysis/selectList/CamelotTable?search_field=guid&search_word=51F4B150-F5B7-11EA-A4CA-A4138BB83B34&limit=15&page=0&search_type=0&order=-rec_date_time&response_field=0


总结

本文主要是针对的多表需要自定义sql的写法。单表可以使用官方给的接口,只需简单配置,即可快速进行单表 CRUD 操作,从而节省大量时间。

Mybatis-plus的官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值