不得不写个博客了,来记录一下这个知识点,在我的项目中用到的一个点。

           大概是这样的:在条件搜索时,有个根据价格范围来进行搜索,在输入价格范围时,有一个最低价和一个最高价,那么在controller进行参数映射时,我的实体类中只有price这一个属性,但前台页面传过来了startPriceendPrice这俩个属性。为了能够映射成功,我把在mapper接口中查找的方法参数设为了map。并在controller中方法参数通过@RequestParam来接收实体类中不能匹配的参数。

实体类:

public class Commodities extends PageEntity{//继承了分页实体类
    private String fruitid;
    private String name;
    private double price;
    private String locality;
    private String createtime;

}

 

前台页面条件表单:

<form id=\"listForm\" action=\"/commodities\" method=\"post\">
     名称:<input type=\"text\" name=\"name\" style=\"width:120px\" value=\"${commodities.name}\"/> 
     产地:<input type=\"text\" name=\"locality\" style=\"width:120px\" value=\"${commodities.locality}\"/>
     价格:<input  name=\"startPrice\" type=\"number\" min=\"0.0\" step=\"0.1\" style=\"width:60px\" value=\"${startPrice}\"/> 
        - <input  name=\"endPrice\" type=\"number\" min=\"0.0\" step=\"0.1\" style=\"width:60px\" value=\"${endPrice}\"/><br/><br/>
	 <input type=\"hidden\" name=\"startPage\" id=\"startPage\" value=\"${startPage}\"/>
	 <input type=\"hidden\" name=\"currentPage\" id=\"currentPage\" value=\"${currentPage}\"/>
	 <input type=\"hidden\" name=\"pageSize\" id=\"pageSize\" value=\"${pageSize}\"/>
	 <input type=\"hidden\" name=\"sumPageNumber\" id=\"sumPageNumber\" value=\"${sumPageNumber}\"/>
	 <input type=\"hidden\" name=\"countNumber\" id=\"countNumber\" value=\"${countNumber}\"/>
	 <input type=\"submit\" value=\"搜索\" style=\"background-color:#173e65;color:#ffffff;width:70px;\"/> <br/>
  </form>

可以看到,在表单的价格中是startPriceendPrice。但实体类中只有一个price属性。是无法进行映射的。为了进行映射,在方法参数中通过@RequestParam注解来接收startPriceendPrice这俩double型数据,可以通过defaultValue设置默认值。然后把它装进构建得map条件中,这样在mapper映射文件中就可以接收到这俩参数了。

/**
  * 
  * liema:查询所有商品,跳到商品页面
  * @return
  */
@RequestMapping(\"/commodities\")
public String index(Model model,Commodities com,@RequestParam(defaultValue=\"0.0\") double startPrice,@RequestParam(defaultValue=\"0.0\") double endPrice) {
	Map map=toMap(com);//将实体类构建成map参数,作为条件
	if(startPrice>0.0) {map.put(\"startPrice\", startPrice);}
	if(endPrice>0.0) {map.put(\"endPrice\", endPrice);}
	int countNumber=commodity.selectCount();//记录总数
	int pageSize=com.getPageSize();//每页显示数据条数
	int currentPage=com.getCurrentPage();//当前页默认是1
	int sumPageNumber=countNumber%pageSize==0?(countNumber/pageSize):(countNumber/pageSize)+1;//总页数
	List<Commodities> list=commodity.findAll(map);//查询数据
	model.addAttribute(\"commodities\",com);//搜索条件回显(货物基本信息)
	model.addAttribute(\"list\",list);
	return \"commodity/commoditiesHome\";
	}

        //构建map条件
	private Map<String, > toMap(Commodities com){
		Map<String, > map=new HashMap<String, >();
		map.put(\"name\", check(com.getName()));
		map.put(\"locality\",check(com.getLocality()));
		map.put(\"startPage\",com.getStartPage());//不为空
		map.put(\"pageSize\",com.getPageSize());//不为空
		return map;
	} 
	
	//判断是否是空的方法
	private String check(  param) {
		return param==null?null:(param.equals(\"\")?null:\"%\"+param+\"%\");
	}

 

mapper映射文件:在mapper映射文件中,由于在\'price>=startPrice\'\'price<=endPrice\'这两个sql片段中含有大于号和小于号,在 配置文件中是不能直接写\'>\'\'<\'这些符号的,将其写到<![CDATA[]]>里面,表示纯文本,这样就可以正常拼接sql

<!-- 所有商品 -->
<select id=\"findAll\" parameterType=\"java.util.HashMap\" resultType=\"commodities\">
	select * from commodities where 1=1 
	<if test=\"name != null\"> and name like #{name}</if>
	<if test=\"locality != null\"> and locality like #{locality}</if>
	<if test=\"startPrice != null\"> <![CDATA[ and price >= #{startPrice}]]></if>
	<if test=\"endPrice != null\"> <![CDATA[ and price <= #{endPrice}]]></if>
	<if test=\"startPage != null and pageSize != null\"> limit #{startPage},#{pageSize}</if>
</select>

 

算是个随笔吧!

收藏 打印