Mapper接口
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
/**
* 根据条件分页查询
*
* @param page
* @param goodsCondition
* @return
*/
IPage<GoodsItem> selectPage(IPage<GoodsItem> page, @Param("condition") GoodsCondition goodsCondition);
}
注意:方法的返回值为IPage对象,只有这样,在使用时,查询出来的结果才会自动赋值给IPage对象。参看下面的测试方法。
映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hc.mapper.GoodsMapper">
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, `name`, barcode, price1, price2, amount, img, pics, category_id, country_id,
addr, sell_point, priority, `status`, create_time, update_time
</sql>
<select id="selectPage" resultType="com.hc.res.GoodsItem" parameterType="com.hc.query.GoodsCondition">
select
<include refid="Base_Column_List"/>
from tb_goods
<where>
<if test="condition.id != null">
and id = #{condition.id,jdbcType=BIGINT}
</if>
<if test="condition.name != null">
and name = #{condition.name,jdbcType=VARCHAR}
</if>
<if test="condition.barcode != null">
and barcode = #{condition.barcode,jdbcType=VARCHAR}
</if>
<if test="condition.price1 != null">
and price1 = #{condition.price1,jdbcType=DECIMAL}
</if>
<if test="condition.price2 != null">
and price2 = #{condition.price2,jdbcType=DECIMAL}
</if>
<if test="condition.amount != null">
and amount = #{condition.amount,jdbcType=INTEGER}
</if>
<if test="condition.categoryId != null">
and category_id = #{condition.categoryId,jdbcType=INTEGER}
</if>
<if test="condition.countryId != null">
and country_id = #{condition.countryId,jdbcType=INTEGER}
</if>
<if test="condition.addr != null">
and addr = #{condition.addr,jdbcType=VARCHAR}
</if>
<if test="condition.sellPoint != null">
and sell_point = #{condition.sellPoint,jdbcType=VARCHAR}
</if>
<if test="condition.status != null">
and status = #{condition.status,jdbcType=INTEGER}
</if>
<if test="condition.start != null and condition.end !=null ">
and create_time between #{condition.start,jdbcType=TIMESTAMP} and #{condition.end,jdbcType=TIMESTAMP}
</if>
</where>
</select>
</mapper>
测试代码
@SpringBootTest
class GoodsMapperTest {
@Resource
private GoodsMapper goodsMapper;
@Test
void selectPage() {
IPage<GoodsItem> 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);
}
}