springMVC使用map接收入参 + mybatis使用map 传入查询参数

 测试例子:

controllel层 ,使用map接收请求参数,通过Debug可以看到,请求中的参数的值都是字符串形式,如果将这个接收参数的map直接传入service,mybatis接收参数时会报错,因此要先对请求中的参数进行预处理

 1 package org.slsale.test;
 2 
 3 import java.util.Date;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import javax.servlet.http.HttpServletRequest;
 9 
10 import net.sf.json.JSONArray;
11 import net.sf.json.JsonConfig;
12 
13 import org.slsale.common.JsonDateValueProcessor;
14 import org.slsale.dao.testmapper.TestUser;15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Controller;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.RequestParam;
20 import org.springframework.web.bind.annotation.ResponseBody;
21 
22 @Controller
23 @RequestMapping("/test")
24 public class TestController {
25 
26     @Autowired
27     private TestService service;
28 
29     // 返回页面的数据中有中文,用produces 处理乱码
30     @RequestMapping(value = "/getUsers", produces = { "text/html;charset=UTF-8" }, method = RequestMethod.GET)
31     @ResponseBody
32     public String getUsers(HttpServletRequest request,
33             @RequestParam Map params1) {
34 
35         Map params = new HashMap();
36         // params1中的value值全部都是字符串,进行转换
37         params.put("isStart", Integer.valueOf(params1.get("isStart")));
38         params.put("startNum", Integer.valueOf(params1.get("startNum")));
39         params.put("pageSize", Integer.valueOf(params1.get("pageSize")));
40         params.put("userType", params1.get("userType"));   //数据库中userType字段是varchar类型,isStart为int类型
41         List users = service.getUsers(params); 
42         JsonConfig jsonConfig = new JsonConfig();
43         // 返回到前端的json数据中,User类里面有Date类型的数据,使用JsonConfig进行时间格式转换
44         jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor());
45         JSONArray array = JSONArray.fromObject(users, jsonConfig);
46         return array.toString();
47     }
48 
49 }
自定义 JsonDateValueProcessor,对返回前端的json数据中的时间进行指定时间格式的转换,这里使用net.sf.json的JsonConfig类,要添加 json-lib-2.4-jdk15.jar 这个jar包
 1 package org.slsale.common;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 import java.util.Locale;
 6 
 7 import net.sf.json.JsonConfig;
 8 import net.sf.json.processors.JsonValueProcessor;
 9 
10 /**
11  * JsonDateValueProcessor JSON 日期格式处理(java转化为JSON)
12  * 
13  * @author
14  * @date
15  */
16 public class JsonDateValueProcessor implements JsonValueProcessor {
17 
18     
19     private String datePattern = "yyyy-MM-dd";
20 
21     
22     public JsonDateValueProcessor() {
23         super();
24     }
25 
26     
27     public JsonDateValueProcessor(String format) {
28         super();
29         this.datePattern = format;
30     }
31 
32 
33     public Object processArrayValue(Object value, JsonConfig jsonConfig) {
34         return process(value);
35     }
36 
37     
38     public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {
39         return process(value);
40     }
41 
42 
43     private Object process(Object value) {
44         try {
45             if (value instanceof Date) {
46                 SimpleDateFormat sdf = new SimpleDateFormat(datePattern,Locale.US);
47                 return sdf.format((Date) value);
48             }
49             return value == null ? "" : value.toString();
50         } catch (Exception e) {
51             return "";
52         }
53 
54     }
55 
56     public String getDatePattern() {
57         return datePattern;
58     }
59 
60 
61     public void setDatePattern(String pDatePattern) {
62         datePattern = pDatePattern;
63     }
64 
65 }

2.service层

 1 package org.slsale.test;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import org.slsale.dao.testmapper.TestUser;
 7 import org.slsale.dao.testmapper.TestUserMapper;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.stereotype.Service;
10 
11 @Service
12 public class TestService {
13     
14     @Autowired
15     private TestUserMapper mapper ;
16     
17     public List getUsers(Map params){
18         return mapper.getUserList1(params);
19     }
20 
21 }

3.dao层,使用map做入参,在sqlMapper.xml中,查询的预编译参数名就是map的key,实际传递进去的查询参数值就是map的value,返回list

 1 package org.slsale.dao.testmapper;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 
 7 public interface TestUserMapper {
 8     
 9     public List getUserList1(Map map);
10 
11 }

sql映射mapper xml:

 1 xml version="1.0" encoding="UTF-8"?>  
 2 DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 
 5 <mapper namespace="org.slsale.dao.testmapper.TestUserMapper">
 6 
 7     
 8     <select id="getUserList1" resultMap="userResultMap" parameterType="Map">
 9         select u.userName ,u.loginCode,u.createTime,r.roleName 
10         from au_user u
11         left join au_role r on r.id=u.roleId 
12         <where>
13             <if test="userName!=null"> and u.userName like  CONCAT('%', #{userName},'%')if>
14             <if test="isStart!=null">and u.isStart=#{isStart}if>
15             <if test="userType!=null">and u.userType=#{userType}if>
16         where>
17         order by createTime desc limit #{startNum},#{pageSize}
18     select>
19 
20     <resultMap id="userResultMap" type="org.slsale.dao.testmapper.TestUser">
21         <id property="id" column="u.id" />
22         <result property="userName" column="u.userName" />
23         <result property="loginCode" column="u.loginCode" />
24         <result property="roleName" column="r.roleName" />
25         <result property="createTime" column="u.createTime" />
26     resultMap>
27 
28 mapper>

联表查询,如果表名使用了别名,接收结果集必须 使用resultmap,resultmap中的column 是sql语句中的查询字段或者该查询字段的别名。sql中的参数名称就是controller中定义的入参map的key

User 类属性:

    private Integer id;
    private String userName;
    private String loginCode;
    private String roleName;
    private Date createTime;    
运行项目,在浏览器输入http://localhost:8080/slsys/test/getUsers?isStart=1&userType=1&startNum=0&pageSize=5 ,控制台打印的sql语句如下:
==>  Preparing: select u.userName ,u.loginCode,u.createTime,r.roleName from au_user u left join au_role r on r.id=u.roleId WHERE u.isStart=? and u.userType=? order by createTime desc limit ?,? 
==> Parameters: 1(Integer), 1(String), 0(Integer), 5(Integer)
<==    Columns: userName, loginCode, createTime, roleName
<==        Row: 李明亮, admin1234, 2018-07-07 18:31:43.0, 会员
<==        Row: 测试用户10, tes10, 2018-06-10 23:21:35.0, 会员
<==        Row: test08, test08, 2018-04-14 09:46:43.0, 会员
<==        Row: test07, test07, 2018-04-09 23:06:21.0, 会员
<==        Row: JBIT, JBIT, 2014-04-10 00:00:00.0, 会员
浏览器调试界面如下:

代码结构:

springMVC使用map接收入参 + mybatis使用map 传入查询参数_第1张图片

 

转载于:https://www.cnblogs.com/enjoyjava/p/9250429.html

你可能感兴趣的:(springMVC使用map接收入参 + mybatis使用map 传入查询参数)