Java Spring引导连接到mysql docker
我已经旋转了Java Spring引导连接到mysql docker,java,mysql,spring-boot,database-connection,Java,Mysql,Spring Boot,Database Connection,我已经旋转了mysqldocker图像 从docker ps: bcb0a900b693mysql:5小时前最新的“docker入口点…”大约一小时前0.0.0.0:3306->3306/tcp chrisbolton 我创建了一个基本的springboot项目,其中我创建了一个简单的类 @SpringBootApplication @RestController public class ChrisboltonServiceApplication { public static void m
mysql
docker图像
从docker ps
:
bcb0a900b693mysql:5小时前最新的“docker入口点…”大约一小时前0.0.0.0:3306->3306/tcp chrisbolton
我创建了一个基本的springboot
项目,其中我创建了一个简单的类
@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;
}
当我启动应用程序时,我可以点击localhost:8080/hello
返回hello代码>
当我点击localhost:8080/blogs
时,我得到了这个错误
java.sql.SQLException:未选择任何数据库
所以我想我不明白自动连线
是如何完全工作的
我已经尝试过查看
bean
,或者可能使用连接
类。但是连接到我的mysql
实例的正确Spring Boot
方法是什么?看起来您缺少数据库名称,例如名为test的数据库:
spring.datasource.url=jdbc:mysql://localhost:3306/test
希望它有帮助您面临的问题是,因为您没有提供数据库名称,因此无法查询整个服务器 格式错误: spring.datasource.url=jdbc:mysql://localhost:3306 正确格式: spring.datasource.url=jdbc:mysql://localhost:3306/accounts 一般的格式是这样的 jdbc:[数据库类型]://[主机解析程序]:[端口]/[数据库名称]
当我这样做时,我会得到一个错误,因为:“通信链接失败”,我如何在Springboot中使用mysql docker容器,它是不是因为我在windows上而成为另一个url?
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