资料
明白了什么?
- java运行环境相比php太easy,如同swoole类似框架
- 路由都通过注解的方式实现,规定请求方式以及路由名
- 实体model没有直接继承数据库的crud的基类,而是单独一个“表”仓库类去继承数据库的crud基类。猜测目的是是程序更灵活和更容易维护
- 配置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.(每次更具代码中的模型对象更新创建数据库,关闭时删除表)
不清楚什么?
-
怎么做热启动?
-
加一个官方的开发库;
-
在build.gradle文件里的dependencies里
implementation ‘org.springframework.boot:spring-boot-devtools’ -
然后开启持续编译,再启动就可以热更新了。
## 第一个终端 ./gradlew build --continuous ## 第二个终端 ./gradlew bootRun
-
-
请求数据默认只支持“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对应一个类
-
模型新增字段,字段需要默认值,怎么设置?
- 设置默认值
private Integer age=18;
- 设置默认值
-
java的是mvc模式是怎么样?java的代码结构是怎么样的?
-
java的接口请求过程是怎么样的?进程管理是怎么样的?
-
java和php相比,优势是什么?运行过程有什么区别?
-
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); }