初涉Spring Boot回顾

资料

spring boot官网
spring boot中文官网

明白了什么?

  1. java运行环境相比php太easy,如同swoole类似框架
  2. 路由都通过注解的方式实现,规定请求方式以及路由名
  3. 实体model没有直接继承数据库的crud的基类,而是单独一个“表”仓库类去继承数据库的crud基类。猜测目的是是程序更灵活和更容易维护
  4. 配置ddl-auto
  • none: The default for MySQL. No change is made to the database structure.(代码什么都不做,依据数据库为准)

  • update: Hibernate changes the database according to the given entity structures.(根据代码中实体mode数据结构“属性”,更新数据字段)

  • create: Creates the database every time but does not drop it on close.
    (每次更具代码中的模型对象更新创建数据库——每次重启程序,数据库中的数据都会被删除,新建一张表,关闭时不删除)

  • create-drop: Creates the database and drops it when SessionFactory closes.(每次更具代码中的模型对象更新创建数据库,关闭时删除表)

不清楚什么?

  1. 怎么做热启动?

    • 加一个官方的开发库;

    • 在build.gradle文件里的dependencies里
      implementation ‘org.springframework.boot:spring-boot-devtools’

    • 然后开启持续编译,再启动就可以热更新了。

      ## 第一个终端
      ./gradlew build --continuous
      ## 第二个终端
      ./gradlew bootRun 
      
  2. 请求数据默认只支持“x-www-form-urlencoded”, 怎么配置让他支持json和form-data?

    • 获得请求的各种参数:
      url上的,get上的,json,form-data
      https://www.jianshu.com/p/b2ade9d28275
      https://juejin.im/post/5b5efff0e51d45198469acea
   // 测试post获得body里所有数据
    @PostMapping("/testbody2")
    public Map testbody2(@RequestBody final Map body) {
      return body;
    }
    //获取单个参数
    @PostMapping("/testbody2")
    public String testbody2(@RequestBody final Map body) {
      return body.get("email").toString();
    }
     // 这样写,name参数在url上或者在body里的x-www-form-urlencode里都能获取到
  // 如果两个地方(POST/GET)都有,参数会是两个字符串用逗号拼接好
  @PostMapping(path="/testpost")
  public Map<String, String> testpost(@RequestParam(value = "name", defaultValue = "testttt") final String[] name,
      @RequestParam(value = "email", defaultValue = "123456") final String email) {

    final Map<String, String> map = new HashMap<>();
    map.put("name", Arrays.toString(name));
    //map.put("name", name);
    map.put("email", email);
    return map;
  }
  // 测试获得form-data,并且可以上传文件
    @PostMapping(path="/testformdata")
    public Map<String, String> testformdata(@RequestParam(value = "name", defaultValue = "x-urlencode") final String name,
    @RequestParam(value = "file", required = false) final MultipartFile file) {
      final Map<String, String> map = new HashMap<>();
      map.put("name", name);
      map.put("file", file.getOriginalFilename());
      return map;
    }

官方的form-data例子
https://spring.io/guides/gs/handling-form-submission/
注意,requestBody对应一个类

  1. 模型新增字段,字段需要默认值,怎么设置?

    • 设置默认值
      private Integer age=18;
      
  2. java的是mvc模式是怎么样?java的代码结构是怎么样的?

  3. java的接口请求过程是怎么样的?进程管理是怎么样的?

  4. java和php相比,优势是什么?运行过程有什么区别?

  5. java的复杂查询sql怎么写,支持原生sql吗

     	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    
    	@Repository
    	public interface UserRepository extends CrudRepository<User, Integer> {
    		  @Query("SELECT u FROM User u WHERE u.id = 1 or u.id=2")
    		  Collection<User> findAllId123Users();
    
    		  @Query("SELECT u FROM User u WHERE u.name = ?1")
    		  Collection<User> findUserNameLike(String name);
    	}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值