Chr*_*ton 4 java mysql database-connection spring-boot
我已经启动了mysqldocker 镜像。
来自docker ps:
bcb0a900b693 mysql:latest "docker-entrypoint..." 5 hours ago Up About an hour 0.0.0.0:3306->3306/tcp chrisbolton
我创建了一个基本spring boot项目,在其中创建了一个简单的类。
@SpringBootApplication
@RestController
public class ChrisboltonServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ChrisboltonServiceApplication.class, args);
}
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/hello")
public String sayHello(){
return "Hello";
}
@RequestMapping(path="/blogs")
public @ResponseBody Iterable<ChrisBolton> getAllUsers() {
List<ChrisBolton> result = jdbcTemplate.query(
"SELECT * FROM blog",
(rs, rowNum) -> new ChrisBolton(rs.getString("author"),
rs.getString("title"),
rs.getString("content"),
rs.getDate("date"))
);
return result;
}
Run Code Online (Sandbox Code Playgroud)
}
我已经把我的配置放在我的 application.properties
spring.main.banner-mode=off
spring.datasource.url=jdbc:mysql://localhost:3306
spring.datasource.username=root
spring.datasource.password=opening
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)
当我启动我的应用程序时,我可以点击localhost:8080/hello返回Hello!
当我点击时localhost:8080/blogs,我收到此错误
java.sql.SQLException: No database selected
所以我想我不明白它autowired是如何完全工作的。
我曾尝试调查beans或使用该Connection课程。但是Spring Boot连接到我的mysql实例的正确方法是什么?
| 归档时间: |
|
| 查看次数: |
10303 次 |
| 最近记录: |