请问一下 spring boot 在和数据库增删查改时,它是怎么知道我定义的参数就对应我数据库里的值呢

2018-03-16 15:43:51 +08:00
 shayang888

@Entity public class Mointors_Api { @Id @GeneratedValue private Integer id; private Date createTime; public Mointors_Api(){ }

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public Date getCreateTime() {
    return createTime;
}

public void setCreateTime(Date createTime) {
    this.createTime = createTime;
}

}

@RestController public class ApiController {

private final
ApiRepository apiRepository;

@Autowired
public ApiController(ApiRepository apiRepository) {
    this.apiRepository = apiRepository;
}

@GetMapping(value = "/api")
public List<Mointors_Api> apiList(Mointors_Api api) {
    return apiRepository.findAll(Example.of(api));
}

}

接口返回的结果数据和我数据库里的一一对应 springboot 它咋知道我定义的 createTime 参数就是我数据库里的 createtime 呢

2575 次点击
所在节点    Java
14 条回复
3a3Mp112
2018-03-16 15:44:32 +08:00
jdbc
johnj
2018-03-16 15:48:29 +08:00
根据命名规则
shayang888
2018-03-16 15:49:07 +08:00
@3a3Mp112
spring:
profiles:
active: a
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mointor?useSSL=false
username: root
password: 123456

jpa:
hibernate:
ddl-auto: update
show-sql: true
这里能反应出啥来?我还是没明白 为啥是一一对应呢
shayang888
2018-03-16 15:49:45 +08:00
@johnj
我对 Mointors_Api 里修改参数名 我发现也是取到了正确的值啊
shalk
2018-03-16 15:59:27 +08:00
既然 show sql 为 true 了 看日志里的 sql 吧
3a3Mp112
2018-03-16 16:12:41 +08:00
通过日志你能看到,执行操作实际上被翻译为了 sql 语句。
johnj
2018-03-16 16:13:49 +08:00
修改后的代码看看?
shayang888
2018-03-16 16:27:19 +08:00
@3a3Mp112 Hibernate: select mointors_a0_.id as id1_0_, mointors_a0_.method as method2_0_, mointors_a0_.param as param3_0_, mointors_a0_.project_id as project_4_0_, mointors_a0_.response_time as response5_0_, mointors_a0_.status as status6_0_, mointors_a0_.url as url7_0_ from mointors_api mointors_a0_ where ?=1
shayang888
2018-03-16 16:28:38 +08:00
@shalk Hibernate: select mointors_a0_.id as id1_0_, mointors_a0_.method as method2_0_, mointors_a0_.param as param3_0_, mointors_a0_.project_id as project_4_0_, mointors_a0_.response_time as response5_0_, mointors_a0_.status as status6_0_, mointors_a0_.url as url7_0_ from mointors_api mointors_a0_ where ?=1
shayang888
2018-03-16 16:29:39 +08:00
@johnj 我就是去掉了一个参数 它返回的结果里也正好没有我去掉的那个参数
Miy4mori
2018-03-16 16:45:41 +08:00
```
jpa:
open-in-view: false
hibernate:
ddl-auto: none
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
```
因为有默认策略保证实体映射。
shayang888
2018-03-16 16:54:16 +08:00
@Miy4mori 哦 就是说这是 springboot 的策略是吗?
Miy4mori
2018-03-16 17:04:10 +08:00
@shayang888 SpringBoot 会自动配置很多东西,即使有些配置你虽然没有显式声明。这个命名策略你也可以自己配置,只是你没有显式声明,SpringBoot 给你加上了默认配置。
shayang888
2018-03-16 17:06:52 +08:00
@Miy4mori 谢谢 刚接触 spring boot

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/438671

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX