Mapper接口
@Mapper
public interface GoodsMapper extends BaseMapper {
/**
* 根据条件分页查询
*
* @param page
* @param goodsCondition
* @return
*/
IPage selectPage(IPage page, @Param("condition") GoodsCondition goodsCondition);
}
注意:方法的返回值为IPage对象,只有这样,在使用时,查询出来的结果才会自动赋值给IPage对象。参看下面的测试方法。
映射文件
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
id, `name`, barcode, price1, price2, amount, img, pics, category_id, country_id,
addr, sell_point, priority, `status`, create_time, update_time
select
from tb_goods
and id = #{condition.id,jdbcType=BIGINT}
and name = #{condition.name,jdbcType=VARCHAR}
and barcode = #{condition.barcode,jdbcType=VARCHAR}
and price1 = #{condition.price1,jdbcType=DECIMAL}
and price2 = #{condition.price2,jdbcType=DECIMAL}
and amount = #{condition.amount,jdbcType=INTEGER}
and category_id = #{condition.categoryId,jdbcType=INTEGER}
and country_id = #{condition.countryId,jdbcType=INTEGER}
and addr = #{condition.addr,jdbcType=VARCHAR}
and sell_point = #{condition.sellPoint,jdbcType=VARCHAR}
and status = #{condition.status,jdbcType=INTEGER}
and create_time between #{condition.start,jdbcType=TIMESTAMP} and #{condition.end,jdbcType=TIMESTAMP}
测试代码
@SpringBootTest
class GoodsMapperTest {
@Resource
private GoodsMapper goodsMapper;
@Test
void selectPage() {
IPage page = new Page(1, 10);
GoodsCondition goodsCondition = GoodsCondition.builder()
// .id(3L)
// .name("fdsa")
// .price1(BigDecimal.valueOf(324))
// .start(LocalDateTime.now())
// .end(LocalDateTime.now())
.build();
goodsMapper.selectPage(page, goodsCondition);
System.out.println(page);
}
}