最近搜索

mybatis 搜索功能 模糊搜索功能 xml文件怎么写。

浏览:17
管理员 2025-07-02 09:16






mybatis 搜索功能 

模糊搜索功能


 
@ResponseBody
@RequestMapping("/list")
public Map<String, Object> list(@RequestParam(value = "page", required = false) Integer page,
                                @RequestParam(value = "limit", required = false) Integer limit,
                                @RequestParam(value = "state", required = false) Integer state,
                                @RequestParam(value = "q", required = false) String q,
                                @RequestParam(value = "isTop", required = false) Integer isTop,
                                @RequestParam(value = "recommend", required = false) String recommend,
                                @RequestParam(value = "goodsTypeId", required = false) Integer goodsTypeId,
                                HttpServletResponse response,
                                HttpServletRequest request) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    if (isTop != null) {
        map.put("isTop", isTop);
    }
    if (StringUtil.isNotEmpty(recommend)) {
        map.put("recommend", recommend);
    }
    if (goodsTypeId != null) {
        map.put("goodsTypeId", goodsTypeId);
    }
    if (StringUtil.isNotEmpty(q)) {
        map.put("q", q);
    }
    map.put("state", 1);//0未审校默认   1已审核

    Integer start =  (page-1)*limit ;
    map.put("start", start);
    map.put("size", limit);
    //        Page<Goods> pageList = goodsService.list(map, page, limit);
    //        List<Goods> list = pageList.getRecords();
    List<Goods> list =  goodsMapper.list(map);



<?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="java456.com.mapper.GoodsMapper">

    <resultMap id="BaseResultMap" type="java456.com.entity_xcx.Goods">
            <id property="id" column="id" jdbcType="INTEGER"/>
    </resultMap>


    <resultMap id="BaseResultMap2"   type="java456.com.entity_xcx.Goods">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="title" column="title" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <result property="clickHit" column="click_hit" jdbcType="INTEGER"/>
        <result property="createDateTime" column="create_date_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    虽然resultmap 没有映射   image_url  price  tag  但是现实的时候  ,他还是会有这个值
    <select id="list" parameterType="Map" resultMap="BaseResultMap2">
        select id,title,image_url,price,tag   from t_goods
        <where>
            <if test="recommend!=null and recommend!='' ">
                and recommend = #{recommend}
            </if>
            <if test="isTop!=null and isTop!='' ">
                and is_top = #{isTop}
            </if>
            <if test="state!=null and state!='' ">
                and state = #{state}
            </if>
            <if test="goodsTypeId!=null and goodsTypeId!='' ">
                and goods_type_id = #{goodsTypeId}
            </if>
            <if test="date1!=null and date1!='' ">
                and date &gt;= #{date1}
            </if>
            <if test="date2!=null and date2!='' ">
                and date &lt;=  #{date2}
            </if>
            <if test="q!=null and q!=''">
                and (title like concat('%', #{q}, '%')
                or content like concat('%', #{q}, '%')
                or remark like concat('%', #{q}, '%'))
            </if>
        </where>
        order by id DESC
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>

</mapper>


联系站长

站长微信:xiaomao0055

站长QQ:14496453