最近搜索

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

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






mybatis 搜索功能    list 查询的写法。  resultMap 怎么写 

模糊搜索功能   



 
@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  但是现实的时候  ,他还是会有这个值
     jdbcType 这个也可以不用写。
     
     
    
    <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>

    
    
    
    
    <select id="getTotal" parameterType="Map" resultType="Integer">
   select count(*) from t_jyz_client
   <where>
      <if test="q!=null and q!=''">
         and (name like #{q} or bianma like #{q}  or jiancheng like #{q} or conn1 like #{q} or conn2 like #{q}  or remark like #{q} or conn2_phone like #{q} or conn1_phone like #{q}  ) 
      </if>
      <if test="clientTypeId!=null and clientTypeId!=''">
         and  clientTypeId  = #{clientTypeId}
      </if>
   </where>
</select>
    
    
</mapper>




有一种情况是要写映射的。

就是说,他们的名字没有约定成俗那种。 实体的属性和数据库字段名字差别很大。

@TableField(value = "date111")
private String date1  ;//  即使实体 指定了,也要在resultMap 在指定一下。 其余情况  resultMap 不写也没事。

那种 private String imageUrl; 这种不用指定。 sql语句是  image_url 但是他会自动匹配实体imageUrl


<resultMap id="BaseResultMap2" type="java456.com.entity_xcx.Goods">
    <id property="id" column="id"  />
    <result property="date1" column="date111"  />
</resultMap>


<select id="list" parameterType="Map" resultMap="BaseResultMap2">
    select id,title,image_url,price,tag,date111   from t_goods


联系站长

站长微信:xiaomao0055

站长QQ:14496453