作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我已经启动了 mysql
docker 镜像。
来自 docker ps
:
bcb0a900b693 mysql:latest "docker-entrypoint..."5 小时前 Up 大约一个小时 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;
}
我已将我的配置放在我的 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
当我启动我的应用程序时,我可以点击 localhost:8080/hello
返回 Hello!
当我点击 localhost:8080/blogs
时,我得到了这个错误
java.sql.SQLException: 没有选择数据库
所以我想我不明白 autowired
是如何完全工作的。
我已经尝试研究 beans
或者可能使用 Connection
类。但是连接到我的 mysql
实例的正确 Spring Boot
方法是什么?
最佳答案
看起来您缺少数据库名称,例如名为 test 的数据库:
spring.datasource.url=jdbc:mysql://localhost:3306/test
希望对你有帮助
关于java - Spring Boot 连接到 mysql docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45946434/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!