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

测试例子:

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

 package org.slsale.test;

 import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest; 10 import net.sf.json.JSONArray;
11 import net.sf.json.JsonConfig;

import org.slsale.common.JsonDateValueProcessor;
import org.slsale.dao.testmapper.TestUser; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/test")
public class TestController { @Autowired
private TestService service; // 返回页面的数据中有中文,用produces 处理乱码
@RequestMapping(value = "/getUsers", produces = { "text/html;charset=UTF-8" }, method = RequestMethod.GET)
@ResponseBody
public String getUsers(HttpServletRequest request,
@RequestParam Map<String, String> params1) { Map<String, Object> params = new HashMap<String, Object>();
// params1中的value值全部都是字符串,进行转换
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类型
List<TestUser> users = service.getUsers(params);
JsonConfig jsonConfig = new JsonConfig();
// 返回到前端的json数据中,User类里面有Date类型的数据,使用JsonConfig进行时间格式转换
jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor());
45 JSONArray array = JSONArray.fromObject(users, jsonConfig);
return array.toString();
} }
自定义 JsonDateValueProcessor,对返回前端的json数据中的时间进行指定时间格式的转换,这里使用net.sf.json的JsonConfig类,要添加 json-lib-2.4-jdk15.jar 这个jar包
 package org.slsale.common;

 import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale; import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor; /**
* JsonDateValueProcessor JSON 日期格式处理(java转化为JSON)
*
* @author
* @date
*/
public class JsonDateValueProcessor implements JsonValueProcessor { private String datePattern = "yyyy-MM-dd"; public JsonDateValueProcessor() {
super();
} public JsonDateValueProcessor(String format) {
super();
this.datePattern = format;
} public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
} public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {
return process(value);
} private Object process(Object value) {
try {
if (value instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat(datePattern,Locale.US);
return sdf.format((Date) value);
}
return value == null ? "" : value.toString();
} catch (Exception e) {
return "";
} } public String getDatePattern() {
return datePattern;
} public void setDatePattern(String pDatePattern) {
datePattern = pDatePattern;
} }

2.service层

 package org.slsale.test;

 import java.util.List;
import java.util.Map; import org.slsale.dao.testmapper.TestUser;
import org.slsale.dao.testmapper.TestUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class TestService { @Autowired
private TestUserMapper mapper ; public List<TestUser> getUsers(Map<String, Object> params){
return mapper.getUserList1(params);
} }

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

 package org.slsale.dao.testmapper;

 import java.util.List;
import java.util.Map; public interface TestUserMapper { public List<TestUser> getUserList1(Map<String, Object> map);

}

sql映射mapper xml:

 <?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="org.slsale.dao.testmapper.TestUserMapper"> <!-- getUserList1 根据条件查询分页用户列表,联表查询,表名使用了别名,需要使用ResultMap进行字段映射而不再是 resultType="user"-->
<select id="getUserList1" resultMap="userResultMap" parameterType="Map">
select u.userName ,u.loginCode,u.createTime,r.roleName
from au_user u
left join au_role r on r.id=u.roleId
<where>
<if test="userName!=null"> and u.userName like CONCAT('%', #{userName},'%')</if>
<if test="isStart!=null">and u.isStart=#{isStart}</if>
<if test="userType!=null">and u.userType=#{userType}</if>
</where>
order by createTime desc limit #{startNum},#{pageSize}
</select> <resultMap id="userResultMap" type="org.slsale.dao.testmapper.TestUser">
<id property="id" column="u.id" />
<result property="userName" column="u.userName" />
<result property="loginCode" column="u.loginCode" />
<result property="roleName" column="r.roleName" />
<result property="createTime" column="u.createTime" />
</resultMap> </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 传入查询参数

代码结构:

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

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

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

上一篇:WPF 组合快捷键(Ctrl+C)


下一篇:Android多种进度条使用详解