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 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 entity = this.helloRepository.findById(1L); if(entity == null) { return "没有找到ID=1对象"; }else { return entity.get().getName(); } } }