当前位置:   article > 正文

@RequestAttribute,@RequestBody 注解,代码实现分页查询

requestattribute

@RequestAttribute,@RequestBody 注解,代码实现分页查询

1. @RequestAttribute注解

@RequestAttribute 获取request请求域中的数据

测试如下:

发起post请求

2、@RequestBody 注解

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。

  1. 注:一个请求,只有一个RequestBody;一个请求,可以有多个RequestParam。
  2. 注:当同时使用@RequestParam()@RequestBody时,@RequestParam()指定的参数可以是普通元素、
  3. 数组、集合、对象等等(即:当,@RequestBody@RequestParam()可以同时使用时,原SpringMVC接收
  4. 参数的机制不变,只不过RequestBody 接收的是请求体里面的数据;而RequestParam接收的是key-value
  5. 里面的参数,所以它会被切面进行处理从而可以用普通元素、数组、集合、对象等接收)。
  6. 即:如果参数时放在请求体中,application/json传入后台的话,那么后台要用@RequestBody才能接收到;
  7. 如果不是放在请求体中的话,那么后台接收前台传过来的参数时,要用@RequestParam来接收,或
  8. 则形参前 什么也不写也能接收。
  9. 注:如果参数前写了@RequestParam(xxx),那么前端必须有对应的xxx名字才行(不管其是否有值,当然可以通
  10. 过设置该注解的required属性来调节是否必须传),如果没有xxx名的话,那么请求会出错,报400
  11. 注:如果参数前不写@RequestParam(xxx)的话,那么就前端可以有可以没有对应的xxx名字才行,如果有xxx名
  12. 的话,那么就会自动匹配;没有的话,请求也能正确发送。
  13. 追注:这里与feign消费服务时不同;feign消费服务时,如果参数前什么也不写,那么会被默认是
  14. @RequestBody的。
  1. 后端@RequestBody注解对应的类在将HTTP的输入流(含请求体)装配到目标类(即:@RequestBody后面的类)时,会根据json字符串中的key来匹配对应实体类的属性,如果匹配一致且json中的该key对应的值符合(或可转换为),这一条我会在下面详细分析,其他的都可简单略过,但是本文末的核心逻辑代码以及几个结论一定要看! 实体类的对应属性的类型要求时,会调用实体类的setter方法将值赋给该属性。
  2. json字符串中,如果value为""的话,后端对应属性如果是String类型的,那么接受到的就是"",如果是后端属性的类型是Integer、Double等类型,那么接收到的就是null
  3. json字符串中,如果value为null的话,后端对应收到的就是null
  4. 如果某个参数没有value的话,在传json字符串给后端时,要么干脆就不把该字段写到json字符串中;要么写value时, 必须有值,null""都行。千万不能有类似"stature":,这样的写法,如:

测试如下:

查看后端:

3.mybatis 中的注解的常用方法

mapper层

  1. package com.bcht.api.mapper;
  2. import org.apache.ibatis.annotations.*;
  3. import org.apache.ibatis.type.JdbcType;
  4. import java.util.List;
  5. import java.util.Map;
  6. /**
  7. * 接口注册
  8. */
  9. @Mapper
  10. public interface TMsOuterInterfaceMapper {
  11. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "listSystem")
  12. List<Map> listSystem(@Param("registerId") Integer registerId);
  13. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "listIntefaceByRegisterId")
  14. List<Map> listIntefaceByRegisterId(@Param("registerId") Integer registerId);
  15. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "listParamsByInterfaceId")
  16. List<Map> listParamsByInterfaceId(@Param("interfaceId") Integer interfaceId);
  17. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "getParamNumById")
  18. int getParamNumById(@Param("dirId") Integer dirId);
  19. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "getDetailById")
  20. List<Map> getDetailById(@Param("dirId") Integer dirId);
  21. @Results(value = {@Result(property = "interface_id", column = "interface_id", id = true, javaType = Integer.class, jdbcType = JdbcType.INTEGER)
  22. })
  23. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "getIdByCode")
  24. int getIdByCode(@Param("interfaceCode") String interfaceCode);
  25. @InsertProvider(type = TMsOuterInterfaceMapperProvider.class, method = "createInterface")
  26. int createInterface(@Param("interface_name") String interface_name,
  27. @Param("interface_code") String interface_code,
  28. @Param("register_id") Integer register_id,
  29. @Param("run_status") String run_status,
  30. @Param("raw_url") String raw_url,
  31. @Param("req_method") String req_method,
  32. @Param("desc") String desc,
  33. @Param("create_user") String create_user,
  34. @Param("now") String now);
  35. @UpdateProvider(type = TMsOuterInterfaceMapperProvider.class, method = "updateInterface")
  36. int updateInterface(@Param("interface_id") Integer interface_id,
  37. @Param("interface_name") String interface_name,
  38. @Param("interface_code") String interface_code,
  39. @Param("register_id") Integer register_id,
  40. @Param("run_status") String run_status,
  41. @Param("raw_url") String raw_url,
  42. @Param("req_method") String req_method,
  43. @Param("desc") String desc,
  44. @Param("update_user") String update_user,
  45. @Param("now") String now);
  46. @UpdateProvider(type = TMsOuterInterfaceMapperProvider.class, method = "updateInterfaceStatus")
  47. int updateInterfaceStatus(@Param("update_user") String update_user,
  48. @Param("run_status") String run_status,
  49. @Param("now") String now,
  50. @Param("interfaceId") Integer interfaceId);
  51. @UpdateProvider(type = TMsOuterInterfaceMapperProvider.class, method = "deleteInterface")
  52. int deleteInterface(@Param("dirId") Integer dirId);
  53. @DeleteProvider(type = TMsOuterInterfaceMapperProvider.class, method = "deleteParams")
  54. int deleteParams(@Param("interfaceId") Integer interfaceId);
  55. @Results(value = {@Result(property = "run_status", column = "run_status", javaType = String.class, jdbcType = JdbcType.VARCHAR)
  56. })
  57. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "checkSystemStatus")
  58. String checkSystemStatus(@Param("interfaceId") Integer interfaceId);
  59. @Results(value = {@Result(property = "interface_code", column = "interface_code", javaType = String.class, jdbcType = JdbcType.VARCHAR),
  60. @Result(property = "interface_name", column = "interface_name", javaType = String.class, jdbcType = JdbcType.VARCHAR)
  61. })
  62. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "checkRepeat")
  63. List<Map> checkRepeat(@Param("interfaceId") Integer interfaceId);
  64. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "queryPageResultList")
  65. List<Map> queryPageResultList(@Param("searchVal") String searchVal,
  66. @Param("offset") Integer offset,
  67. @Param("pageSize") Integer pageSize);
  68. @SelectProvider(type = TMsOuterInterfaceMapperProvider.class, method = "countResultList")
  69. Integer countResultList(@Param("searchVal") String searchVal,
  70. @Param("offset") Integer offset,
  71. @Param("pageSize") Integer pageSize);
  72. }

对用的实现类

  1. package com.bcht.api.mapper;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.ibatis.jdbc.SQL;
  4. import java.text.MessageFormat;
  5. import java.util.List;
  6. import java.util.Map;
  7. public class TMsOuterInterfaceMapperProvider {
  8. private static final String TABLE_SYSTEM = "t_ms_outer_register";
  9. private static final String TABLE_INTERFACE = "t_ms_outer_interface";
  10. private static final String TABLE_PARAMS = "t_ms_outer_interface_params";
  11. public String listSystem(Map<String, Object> parameter) {
  12. return new SQL() {{
  13. SELECT(" * ");
  14. FROM(TABLE_SYSTEM);
  15. Object registerId = parameter.get("registerId");
  16. if (registerId != null && StringUtils.isNotEmpty(registerId.toString())) {
  17. WHERE("register_id=#{registerId}");
  18. }
  19. WHERE(" run_status <> '0' ");
  20. }}.toString();
  21. }
  22. public String listIntefaceByRegisterId(Map<String, Object> parameter) {
  23. return new SQL() {{
  24. SELECT(" * ");
  25. FROM(TABLE_INTERFACE);
  26. Object registerId = parameter.get("registerId");
  27. if (registerId != null && StringUtils.isNotEmpty(registerId.toString())) {
  28. WHERE("register_id=#{registerId}");
  29. }
  30. WHERE(" run_status <> '0' ");
  31. ORDER_BY(" update_time desc ");
  32. }}.toString();
  33. }
  34. public String listParamsByInterfaceId(Map<String, Object> parameter) {
  35. return new SQL() {{
  36. SELECT(" * ");
  37. FROM(TABLE_PARAMS);
  38. Object interfaceId = parameter.get("interfaceId");
  39. if (interfaceId != null && StringUtils.isNotEmpty(interfaceId.toString())) {
  40. WHERE("interface_id=#{interfaceId}");
  41. }
  42. }}.toString();
  43. }
  44. public String getParamNumById(Map<String, Object> parameter) {
  45. return new SQL() {{
  46. SELECT(" count(1) cnt ");
  47. FROM(TABLE_PARAMS);
  48. WHERE("interface_id=#{dirId}");
  49. }}.toString();
  50. }
  51. public String getDetailById(Map<String, Object> parameter) {
  52. return new SQL() {{
  53. SELECT(" * ");
  54. FROM(TABLE_INTERFACE);
  55. WHERE("interface_id=#{dirId}");
  56. WHERE(" run_status <> '0' ");
  57. }}.toString();
  58. }
  59. public String getIdByCode(Map<String, Object> parameter) {
  60. return new SQL() {
  61. {
  62. SELECT(" interface_id ");
  63. FROM(TABLE_INTERFACE);
  64. WHERE("`interface_code`=#{interfaceCode}");
  65. WHERE(" run_status <> '0' ");
  66. }
  67. }.toString();
  68. }
  69. public String createInterface(Map<String, Object> parameter) {
  70. return new SQL() {{
  71. INSERT_INTO(TABLE_INTERFACE);
  72. VALUES("`interface_name`", "#{interface_name}");
  73. VALUES("`interface_code`", "#{interface_code}");
  74. VALUES("`register_id`", "#{register_id}");
  75. VALUES("`run_status`", "#{run_status}");
  76. VALUES("`raw_url`", "#{raw_url}");
  77. VALUES("`req_method`", "#{req_method}");
  78. VALUES("`desc`", "#{desc}");
  79. VALUES("`create_user`", "#{create_user}");
  80. VALUES("`create_time`", "#{now}");
  81. }}.toString();
  82. }
  83. public String updateInterface(Map<String, Object> parameter) {
  84. return new SQL() {{
  85. UPDATE(TABLE_INTERFACE);
  86. SET("`interface_name` = #{interface_name}");
  87. SET("`interface_code` = #{interface_code}");
  88. SET("`register_id` = #{register_id}");
  89. SET("`run_status` = #{run_status}");
  90. SET("`raw_url` = #{raw_url}");
  91. SET("`req_method` = #{req_method}");
  92. SET("`desc` = #{desc}");
  93. SET("`update_user` = #{update_user}");
  94. SET("`update_time` = #{now}");
  95. WHERE("`interface_id` = #{interface_id}");
  96. }}.toString();
  97. }
  98. public String updateInterfaceStatus(Map<String, Object> parameter) {
  99. return new SQL() {{
  100. UPDATE(TABLE_INTERFACE);
  101. SET("`run_status` = #{run_status}");
  102. SET("`update_user` = #{update_user}");
  103. SET("`update_time` = #{now}");
  104. WHERE("`interface_id` = #{interfaceId}");
  105. }}.toString();
  106. }
  107. public String deleteInterface(Map<String, Object> parameter) {
  108. return new SQL() {{
  109. UPDATE(TABLE_INTERFACE);
  110. SET("`run_status` = '0' ");
  111. WHERE("`interface_id`=#{dirId}");
  112. }}.toString();
  113. }
  114. public String deleteParams(Map<String, Object> parameter) {
  115. return new SQL() {{
  116. DELETE_FROM(TABLE_PARAMS);
  117. Object interfaceId = parameter.get("interfaceId");
  118. if (interfaceId != null && StringUtils.isNotEmpty(interfaceId.toString())) {
  119. WHERE(" interface_id = #{interfaceId} ");
  120. }
  121. }}.toString();
  122. }
  123. public String checkSystemStatus(Map<String, Object> parameter) {
  124. return new SQL() {{
  125. SELECT(" b.run_status ");
  126. FROM(TABLE_INTERFACE + " a ");
  127. LEFT_OUTER_JOIN(TABLE_SYSTEM + " b on a.register_id = b.register_id ");
  128. Object interfaceId = parameter.get("interfaceId");
  129. if (interfaceId != null && StringUtils.isNotEmpty(interfaceId.toString())) {
  130. WHERE(" a.interface_id = #{interfaceId} ");
  131. }
  132. }}.toString();
  133. }
  134. public String checkRepeat(Map<String, Object> parameter) {
  135. return new SQL() {{
  136. SELECT(" interface_code,interface_name ");
  137. FROM(TABLE_INTERFACE);
  138. Object interfaceId = parameter.get("interfaceId");
  139. if (interfaceId != null && StringUtils.isNotEmpty(interfaceId.toString())) {
  140. WHERE(" interface_id <> #{interfaceId} ");
  141. }
  142. WHERE(" run_status <> '0' ");
  143. GROUP_BY(" interface_code,interface_name ");
  144. }}.toString();
  145. }
  146. public String queryPageResultList(Map<String, Object> parameter) {
  147. return new SQL() {{
  148. SELECT(" a.*,b.register_name ");
  149. FROM(TABLE_INTERFACE + " a ");
  150. LEFT_OUTER_JOIN(TABLE_SYSTEM + " b on a.register_id = b.register_id ");
  151. Object searchVal = parameter.get("searchVal");
  152. if (searchVal != null && StringUtils.isNotEmpty(searchVal.toString())) {
  153. WHERE(" a.interface_name like concat('%', '" + parameter.get("searchVal").toString() + "', '%')");
  154. }
  155. WHERE(" a.run_status <> '0' ");
  156. ORDER_BY(" a.update_time desc limit #{offset},#{pageSize} ");
  157. }}.toString();
  158. }
  159. public String countResultList(Map<String, Object> parameter) {
  160. return new SQL() {{
  161. SELECT(" count(1) ");
  162. FROM(TABLE_INTERFACE);
  163. Object searchVal = parameter.get("searchVal");
  164. if (searchVal != null && StringUtils.isNotEmpty(searchVal.toString())) {
  165. WHERE("interface_name like concat('%', '" + parameter.get("searchVal").toString() + "', '%')");
  166. }
  167. WHERE(" run_status <> '0' ");
  168. ORDER_BY(" update_time desc limit #{offset},#{pageSize} ");
  169. }}.toString();
  170. }
  171. }

4、通过pageNum和pageSize 实现分页查询

通过页数和每页有多少数据量,求得偏移量,然后通过 limit 偏移量,数量。求得查询的数据,然后做展示就行了、

代码如下:

Controller层:

  1. /**
  2. * 分页查询
  3. *
  4. * @param loginUser
  5. * @param searchVal
  6. * @param pageNo
  7. * @param pageSize
  8. * @return
  9. */
  10. @GetMapping("/list-paging")
  11. public Result listPaging(@RequestAttribute(value = Constants.SESSION_USER) User loginUser,
  12. @RequestParam(value = "searchVal", required = false) String searchVal,
  13. @RequestParam("pageNo") Integer pageNo,
  14. @RequestParam("pageSize") Integer pageSize) {
  15. return tMsOuterInterfaceService.queryPageList(loginUser.getId(), searchVal, pageNo, pageSize);
  16. }

Service层:

  1. /**
  2. * 分页查询
  3. *
  4. * @param userId
  5. * @param searchVal
  6. * @param pageNo
  7. * @param pageSize
  8. * @return
  9. */
  10. public Result queryPageList(Integer userId, String searchVal, int pageNo, int pageSize) {
  11. Result result = new Result();
  12. Integer offset = (pageNo - 1) * pageSize;
  13. List<Map> list = tMsOuterInterfaceMapper.queryPageResultList(searchVal, offset, pageSize);
  14. if (list.size() > 0) {
  15. for (Map map : list) {
  16. String run_statusName = "";
  17. String req_method = "";
  18. String position = "";
  19. String type = "";
  20. String is_required = "";
  21. switch (map.get("run_status").toString()) {
  22. case "0":
  23. run_statusName = TABLE_RUN_DELETED.getName();
  24. break;
  25. case "1":
  26. run_statusName = TABLE_RUN_ENABLED.getName();
  27. break;
  28. case "2":
  29. run_statusName = TABLE_RUN_DISABLED.getName();
  30. break;
  31. }
  32. switch (map.get("req_method").toString()) {
  33. case "1":
  34. req_method = HTTP_GET.getName();
  35. break;
  36. case "2":
  37. req_method = HTTP_POST.getName();
  38. break;
  39. }
  40. Integer interfaceId = (Integer) map.get("interface_id");
  41. List<Map> paramList = tMsOuterInterfaceMapper.listParamsByInterfaceId(interfaceId);
  42. for (Map param : paramList) {
  43. switch (param.get("position").toString()) {
  44. case "0":
  45. position = POSITION_HEADER.getName();
  46. break;
  47. case "1":
  48. position = POSITION_QUERY.getName();
  49. break;
  50. case "2":
  51. position = POSITION_BODY.getName();
  52. break;
  53. }
  54. switch (param.get("type").toString()) {
  55. case "1":
  56. type = TYPE_CONST.getName();
  57. break;
  58. case "2":
  59. type = TYPE_DATE.getName();
  60. break;
  61. }
  62. switch (param.get("is_required").toString()) {
  63. case "0":
  64. is_required = IS_REQUIRED_FALSE.getName();
  65. break;
  66. case "1":
  67. is_required = IS_REQUIRED_TRUE.getName();
  68. break;
  69. }
  70. param.put("positionName", position);
  71. param.put("typeName", type);
  72. param.put("is_requiredName", is_required);
  73. }
  74. map.put("param", paramList);
  75. map.put("run_statusName", run_statusName);
  76. map.put("req_methodName", req_method);
  77. }
  78. }
  79. result.setData(list);
  80. Map<String, Object> mapData = new HashMap<>();
  81. Integer total = tMsOuterInterfaceMapper.countResultList(searchVal, offset, pageSize);
  82. mapData.put("total", total);
  83. result.setDataMap(mapData);
  84. putMsg(result, Status.SUCCESS);
  85. return result;
  86. }

Mapper层:

  1. public String countResultList(Map<String, Object> parameter) {
  2. return new SQL() {{
  3. SELECT(" count(1) ");
  4. FROM(TABLE_INTERFACE);
  5. Object searchVal = parameter.get("searchVal");
  6. if (searchVal != null && StringUtils.isNotEmpty(searchVal.toString())) {
  7. WHERE("interface_name like concat('%', '" + parameter.get("searchVal").toString() + "', '%')");
  8. }
  9. WHERE(" run_status <> '0' ");
  10. ORDER_BY(" update_time desc limit #{offset},#{pageSize} ");
  11. }}.toString();
  12. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/310110
推荐阅读
相关标签
  

闽ICP备14008679号