【发布时间】:2020-08-09 08:51:19
【问题描述】:
我目前正在构建一个简单的基于 CRUD 的应用程序,并且我已经完成了基本的工作,例如创建带有标题、日期、描述等的表单,编辑或删除帖子等。所以现在我正在尝试添加图片上传功能。 (我使用 Windows 10 作为我的操作系统)
目前正在学习以下网址的教程
https://www.callicoder.com/spring-boot-file-upload-download-jpa-hibernate-mysql-database-example/
,当我看的时候
配置数据库和多部分文件属性
部分,它解释了配置数据库和多部分文件属性需要哪些语句,但是当我添加教程页面的示例时,它导致了冲突。
下面是我的application.properties 文件的样子。
(“## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties”后面是我从教程中复制粘贴的部分,上面是添加教程之前的原始代码)
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.h2.console.enabled=true
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url= jdbc:mysql://localhost:3306/file_demo?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username= root
spring.datasource.password= callicoder
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
## Hibernate Logging
logging.level.org.hibernate.SQL= DEBUG
## MULTIPART (MultipartProperties)
# Enable multipart uploads
spring.servlet.multipart.enabled=true
# Threshold after which files are written to disk.
spring.servlet.multipart.file-size-threshold=2KB
# Max file size.
spring.servlet.multipart.max-file-size=200MB
# Max Request Size
spring.servlet.multipart.max-request-size=215MB
导致冲突的部分如下。
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=sa
spring.datasource.url= jdbc:mysql://localhost:3306/file_demo?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username= root
我认为冲突的原因是我试图同时使用 JDBC H2 数据库和 JDBC MySQL。起初我认为像下面这样注释掉我的原始配置可以解决问题,
#spring.datasource.url=jdbc:h2:mem:test
#spring.datasource.driverClassName=org.h2.Driver
#spring.datasource.username=sa
#spring.h2.console.enabled=true
,但是在这之后我无法运行程序,可能是因为有一部分我使用了 JDBC Templace,如下所示。
[ReportDaoImpl.java]
package com.example.demo.repository;
import java.sql.Timestamp;
@Repository
public class ReportDaoImpl implements ReportDao {
private final JdbcTemplate jdbcTemplate;
@Autowired
public ReportDaoImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Report> findAll() {
String sql = "SELECT report_id, title, threat_level, report_date, description, img_path, "
+ "user.user_id, user_name FROM report "
+ "INNER JOIN user ON report.user_id = user.user_id";
List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
……
我最大的问题是,如何将教程的“上传图像”功能集成到我的基本 CRUD 应用程序中而不会导致配置冲突?
我是否应该放弃使用 JDBC H2 数据库和 JDBC 模板并使用与我从教程中获取的 JDBC MySQL 部分兼容的其他东西?换句话说,为了集成教程的图片上传功能,我应该从根本上重构我的代码在ReportDaoImpl.java文件中(甚至可能还有其他文件?),还是有一个简单的方法来解决配置冲突?
【问题讨论】:
标签: mysql spring-boot h2