Magic-Club开发--第十三天

一、今日完成

  1.完成数据库--社团表单。

  

  2.实现分类查询社团,搜索查询略有bug。

    

  后端主要代码

  Mapper.XML

    <select id="getAssociationListByKey" resultType="com.example.demo.domin.Association" parameterType="java.lang.Integer">
        SELECT * FROM association where `key`=#{key};
    </select>
    <select id="getAssociationListBySearch" resultType="com.example.demo.domin.Association" parameterType="java.lang.String">
        SELECT * FROM association where `associationName` like '%'+#{key}+'%';
    </select>

  Controller.java

   @GetMapping(value = "/associationList")
    public Map<String,Object> getAssociation(@Param("key")Integer key){
        Map<String,Object> model = new HashMap<String,Object>();
        List<Association> list = new ArrayList<Association>();
        list= associationService.getAssociationListByKey(key);
        model.put("result",list);
        return  model;
    }
    @GetMapping(value = "/search")
    public Map<String,Object> searchAssociation(@Param("key")String key){
        Map<String,Object> model = new HashMap<String,Object>();
        List<Association> list = new ArrayList<Association>();
        list= associationService.getAssociationListBySearch(key);
        model.put("result",list);
        return  model;
    }

二、明日计划

  1.完成数据库--用户表单,以及相关API开发。

  2.完善社团部分API.

三、个人小结

  今日开发API时,传递参数出现了不少问题。

  前端传出参数,后端接收问题。

  前端代码:

    wx.request({
    url: 'http://127.0.0.1:8080/insert',
        method:"POST",
        header:{
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data:{
          id:6,
          openid:"asdfg",
          name:"ljq"
        },
        success(res) {
          console.log(res.data)
        }
      })

  后端代码:

   @PostMapping(value = "/insert")
    public void insert(HttpServletRequest request, HttpServletResponse response)throws IOException {
        request.setCharacterEncoding("utf-8");
        logger.info(request.getParameter("name"));
        User user = new User();
        user.setId(Integer.parseInt(request.getParameter("id")));
        user.setOpenid(request.getParameter("openid"));
        user.setUser_name(request.getParameter("name"));
        associationService.insert(user);
    }

  小程序中POST方法请求API时,要设置header,确定数据格式,后端通过HttpServeltRequest的getParameter获取前端data中的数据。

  前端发出请求,后端处理请求后返回参数问题。

  前端代码:

   wx.request({
        url: 'http://127.0.0.1:8080/association/associationList?key=' + that.data.key,
        success(res){
          that.setData({
            associations:res.data.result
          })
        }
      })

  后端代码:

    @GetMapping(value = "/associationList")
    public Map<String,Object> getAssociation(@Param("key")Integer key){
        Map<String,Object> model = new HashMap<String,Object>();
        List<Association> list = new ArrayList<Association>();
        list= associationService.getAssociationListByKey(key);
        model.put("result",list);
        return  model;
    }

  小程序前端GET方法,参数由url携带,后端在形参处使用@Param接收参数,返回参数给前端时,使用Map数据类型封装为JSON结构,方便前端读取数据。

  个人感觉每天都在进步,对于spring框架的理解越来越深入,与小程序的交互有了不少长进,开发项目过程中对于已学过的知识进行融会贯通。

原文地址:https://www.cnblogs.com/ljq1313/p/11440382.html