JdbcServerImpl.java
1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.springboot.template.service.impl;
import java.util.Map;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import com.springboot.template.entity.HelloEntity;
import com.springboot.template.reposity.HelloRepository;
import com.springboot.template.service.JdbcService;
@Component
public class JdbcServerImpl implements JdbcService{
@Autowired
private JdbcTemplate JdbcTemplte;
@Autowired
private HelloRepository helloRepository;
@Override
public String queryHello() {
try {
Map<String, Object> data = this.JdbcTemplte.queryForMap("select * from demo.hello limit 1");
if(data.isEmpty()) {
return "没有发现任何数据";
}else {
return data.get("name").toString();
}
}catch(Exception e) {
e.printStackTrace();
return "数据库不存在记录";
}
}
@Override
public String queryHelloFromJpa() {
Optional<HelloEntity> entity = this.helloRepository.findById(1L);
if(entity == null) {
return "没有找到ID=1对象";
}else {
return entity.get().getName();
}
}
}