MyBatis-plus通过注解使用自定义SQL(Warpper)

DAO层
这里用到关联查询产品与产品标签表

通过@Param(Constants.WRAPPER) Wrapper wrapper
使用条件构造器

package com.myelephant.module.mall.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myelephant.module.mall.entity.MallProduct;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 产品实例(MallProduct)表数据库访问层
 *
 * @author makejava
 * @since 2020-11-25 11:56:12
 */
@Mapper
public interface MallProductDao extends BaseMapper<MallProduct> {

    /**
     * 分页查询标签商品集合
     *
     * @param page
     * @param wrapper
     * @return IPage<MallProduct>
     */
    @Select("select p.id,p.`name`,p.type,p.info,p.info_rich,p.publish,p.seq,p.img_urls,p.video_url,p.keywords,p.sales,p.total_sales " +
            "from mall_product p inner join mall_product_tag_prod tp " +
            "on p.id = tp.prod_id ${ew.customSqlSegment} ")
    IPage<MallProduct> pageJoinTagProds(Page<MallProduct> page, @Param(Constants.WRAPPER) Wrapper<MallProduct> wrapper);

}
Service
package com.myelephant.module.mall.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.myelephant.module.mall.entity.MallProduct;

/**
 * 产品实例(MallProduct)表服务接口
 *
 * @author makejava
 * @since 2020-11-25 11:56:13
 */
public interface MallProductService extends IService<MallProduct> {

    /**
     * 分页查询标签商品集合
     *
     * @param page
     * @param wrapper
     * @return IPage<MallProduct>
     */
    IPage<MallProduct> pageJoinTagProds(Page<MallProduct> page, QueryWrapper<MallProduct> wrapper);
}
ServiceImpl
package com.myelephant.module.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dandandog.framework.core.service.impl.BaseServiceImpl;
import com.myelephant.module.mall.dao.MallProductDao;
import com.myelephant.module.mall.entity.MallProduct;
import com.myelephant.module.mall.service.MallProductService;
import org.springframework.stereotype.Service;

/**
 * 产品实例(MallProduct)表服务实现类
 *
 * @author makejava
 * @since 2020-11-25 11:56:14
 */
@Service
public class MallProductServiceImpl extends BaseServiceImpl<MallProductDao, MallProduct> implements MallProductService {

    /**
     * 分页查询标签商品集合
     *
     * @param page
     * @param wrapper
     * @return IPage<MallProduct>
     */
    @Override
    public IPage<MallProduct> pageJoinTagProds(Page<MallProduct> page, QueryWrapper<MallProduct> wrapper) {
        wrapper.eq("p.publish", "1");
        wrapper.orderByAsc("p.seq");
        return baseMapper.pageJoinTagProds(page, wrapper);
    }
}
具体使用

根据tag_id筛选并且指定产品类型

QueryWrapper<MallProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("tp.tag_id", tagId);
        List<ProductType> types = new ArrayList<>(10);
        types.add(ProductType.V_PRODUCT);
        types.add(ProductType.P_PRODUCT);
        types.add(ProductType.C_PRODUCT);
        wrapper.in("p.type", types);
        IPage<MallProduct> iPage = mallProductService.pageJoinTagProds(new Page<>(page, size), wrapper);
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus提供了非常方便的分页查询功能,可以直接使用Page类来进行分页查询。 使用MyBatis-Plus的分页查询,需要进行以下步骤: 1. 引入MyBatis-Plus的依赖:在pom.xml中添加以下依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> ``` 2. 定义实体类:定义需要进行分页查询的实体类,并使用注解@TableField进行字段映射。 3. 定义Mapper接口:定义Mapper接口,并继承BaseMapper类,继承BaseMapper类后,MyBatis-Plus会自动提供一些基本的CRUD操作。 ``` public interface UserMapper extends BaseMapper<User> {} ``` 4. 分页查询:在Service层中调用分页查询方法,使用Page对象设置分页参数,然后调用selectPage方法进行分页查询。 ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Page<User> getUserList(int pageNum, int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); return userMapper.selectPage(page, null); } } ``` 如果需要自定义sql分页,可以在xml中使用MyBatis的分页插件进行分页查询。 1. 引入分页插件:在pom.xml中添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> ``` 2. 配置分页插件:在MyBatis的配置文件中配置分页插件。 ``` <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="dialect" value="mysql"/> </plugin> </plugins> ``` 3. 自定义sql分页查询:在xml中使用分页插件的PageHelper.startPage方法进行分页查询。 ``` <select id="getUserList" resultMap="userMap"> select * from user <where> <if test="name != null"> and name like concat('%',#{name},'%') </if> </where> order by id desc </select> ``` ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public PageInfo<User> getUserList(int pageNum, int pageSize, String name) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.getUserList(name); return new PageInfo<>(userList); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值