2019-9-26

今日任务

  • springboot + mongodb,完成mongo在springboot中的搭建,写一个小demo
  • 最主要的是把repository这一层写完,业务逻辑可以放到明天来写

完成过程

mongodb的安装

macOS下mongodb的安装与启动

Springboot下的相关注解

  • @Data
    Data注解使用
  • @Document
    标注在实体类上,类似于hibernate的entity注解,标明由mongo来维护该表,collection代表表的名称。
    SpringBoot中MongoDB中的相关注解

  • @Entity等一系列Spring Data JPA注解

    • @DynamicUpdate@DynamicInsert 是hibernate里面的注解,这两个注解加上之后就不会为字段值不变的字段生成sql语句,这样sql的长度就减少了提高了传输效率和执行效率,在插入和修改数据的时候,语句中只包括要插入或者修改的字段。
    • @Entity 标识这个实体类是一个JPA实体,告诉JPA在程序运行的时候记得生成这个实体类所对应的表~!
    • GeneratedValueGenericGenerator是用于主键生成策略的,具体见JPA注解主键生成策略-UUID
    • @Column(name = “自定义字段名”,length = “自定义长度”,nullable = “是否可以空”,unique = “是否唯一”,columnDefinition = “自定义该字段的类型和长度”)。表示对这个变量所对应的字段名进行一些个性化的设置,例如字段的名字,字段的长度,是否为空和是否唯一等等设置。
    • 剩余注释:Spring Data JPA中常用的注解详解
  • @Controller(Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)
    Controller层的注解大概有以下一些:(主要参考:Controller层主要注解

    • @Controller:标注 Controller 类,处理 http 请求
    • @RestController:标注 Controller 类,spring 4 新加注解,相当于@Controller + @ResponseBody ,主要是为了使 http 请求返回数据格式为 json 格式,正常情况下都是使用这个注解
    • @RequestMapping:配置 url 映射,可以作用于类上,也可以在方法上

      1
      2
      3
      4
      5
      6
      7
      8
      9
      @RestController//处理http请求,返回json格式
      @RequestMapping(value = "/users")//配置url,让该类下的所有接口url都映射在/users下
      public class UserController {

      @RequestMapping(value = "/myInfo", method = RequestMethod.GET)
      public String say() {
      return "我是张少林";
      }
      }

      @RequestMapping 定义在类上,指定该类下的所有接口 url 映射在 /users 下,定义在方法上,指定 请求方法,可以指定GET,POST,DELETE,PUT四种标准的 Restfulapi请求方法。
      那么此时的接口 url 为:http://127.0.0.1:8080/users/myInfo 请求方法:GET,类上也可以不用配置 url 映射的。

    • @PathVariable
      获取 url 中的数据,我们在 url 中拼接一个字符串 {username},类似于地址占位符,由用户请求时添加,请求获取。注意注解中的参数必须与占位符参数一致

      1
      2
      3
      4
      5
      6
      7
      8
      @RestController//处理http请求,返回json格式
      @RequestMapping(value = "/users")//配置url,让该类下的所有接口url都映射在/users下
      public class UserController {
      @RequestMapping(value = "/myInfo/{username}", method = RequestMethod.GET)
      public String say(@PathVariable("username") String username) {
      return username;
      }
      }
    • @RequestParam
      获取请求参数值,方法随意可以设置,但是通常需求都是使用 POST 请求处理表单提交。

      1
      2
      3
      4
      5
      6
      7
      8
      @RestController//处理http请求,返回json格式
      @RequestMapping(value = "/users")//配置url,让该类下的所有接口url都映射在/users下
      public class UserController {
      @RequestMapping(value = "/myInfo", method = RequestMethod.POST)
      public String say(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
      return username + password;
      }
      }

      假如用户输入的的uri是:http://127.0.0.1:8080/users/myInfo?username=yangweijie&password=123456,那么最后返回的是yangweijie123456,如果参数不带值且添加属性required=true,则会报错,此时就建议添加一个defaultValue属性:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      @RestController//处理http请求,返回json格式
      @RequestMapping(value = "/users")//配置url,让该类下的所有接口url都映射在/users下
      public class UserController {

      @RequestMapping(value = "/myInfo", method = RequestMethod.POST)
      public String say(@RequestParam(value = "username",required = false,defaultValue = "张少林") String username,
      @RequestParam(value = "password",required = false,defaultValue = "123456") String password) {
      return username + password;
      }
      }
    • @GetMapping、@PostMapping、@DeleteMapping、@PutMapping等是RequestMapping的组合注解,根据method的不同。

    • @RequestHeader
      可以把Request请求header部分的值绑定到方法的参数上。

    • @CookieValue
      可以把Request header中关于cookie的值绑定到方法的参数上。

    • RequestBody
      该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;
      它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上的。
      因为配置有FormHttpMessageConverter,所以也可以用来处理 application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String, String>里,这种情况在某些特殊需求下使用,详情查看FormHttpMessageConverter api;

    • ModelAttribute(参考:Spring MVC @ModelAttribute详解)
      @ModelAttribute有三种用法:

      • 可以标注在方法上;
      • 可以标注在方法中的参数上;
      • 还可以和@RequestMapping一起标注在方法上;
        目的都是在RequestMapping之前进行model属性的注入,对RestController好像没用,因为是返回json数据,并不能将model里面的数据直接返回给页面。
        如果没用@RestController,则@ModelAttribute方法通常被用来填充一些公共需要的属性或数据,比如一个下拉列表所预设的几种状态,或者宠物的几种类型,或者去取得一个HTML表单渲染所需要的命令对象,比如Account等。
        @ModelAttribute标注方法有两种风格:

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        // Add one attribute
        // The return value of the method is added to the model under the name "account"
        // You can customize the name via @ModelAttribute("myAccount")

        @ModelAttribute
        public Account addAccount(@RequestParam String number) {
        return accountManager.findAccount(number);
        }

        // Add multiple attributes

        @ModelAttribute
        public void populateModel(@RequestParam String number, Model model) {
        model.addAttribute(accountManager.findAccount(number));
        // add more ...
        }

        在第一种写法中,方法通过返回值的方式默认地将添加一个属性,在第二种写法中,方法接收一个Model对象,然后可以向其中添加任意数量的属性,可以在根据需要,在两种风格中选择合适的一种。
        model里的数据会被放入到request中,页面通过request域可以获取到。

    • @SessionAttributes和@SessionAttribute
      @SessionAttributes注解的使用
      就是将属性放入session域内,然后可以在session域内进行相关的操作。

      SpringMVC中@ModelAttribute和@SessionAttributes注解的使用

  • @id(好像如果表中数据字段是id,会比较特殊一点,springboot会自动检索传过来的参数是否有id,如果没有会自动添加id)

  • Repository相关注解

Springboot的分页和排序操作

由于采用的是Mongo数据库,所以先后使用了MongoTemplate()和MongoRepository(),MongoTemplate在写法上比MongoRepository 更复杂一些,但是带来更多的灵活性。对于复杂的查询操作,我们一般使用MongoTemplate,对于一些简单的查询我们会使用MongoRepository 。可以这么理解,MongoRepository 只是作为一种对于简单查询的简便操作,而MongoTemplate才是我们在做一些复杂查询时的首选。

MongoRepository

自己还借用了下@Query和@Modifying注解,方便自己摆脱关键字的查询,如果需要进行更新或者删除数据,则需要在@Query之上加上@Modifing。

Thank you for your accept. mua!
-------------本文结束感谢您的阅读-------------