【发布时间】:2017-09-28 02:34:24
【问题描述】:
我通过引用此博客设置了一个项目,但我没有使用 maven,而是使用了 gradle。 Registration and Login Example with Spring Security, Spring Data JPA, Spring Boot
一切正常,除了更新 Mysql 数据库中的值不起作用。
我创建了一个请求映射器来更新用户信息。我检查了请求正文不为空并且包含所有值。此外,我的响应状态为“SUCCESS”。但是当我在 Mysql 数据库中看到时,似乎没有任何更新。我的请求映射器如下。
@RequestMapping(value = "/updateRegistration", method = RequestMethod.POST)
public Map<String, Object> updateRegiatration(@RequestBody Map<String, Object> requestBody) {
Map<String, Object> response = new HashMap<>();
try {
UUID userId = UUID.fromString((String) requestBody.get("userId"));
if (userId != null) {
User user = userRepository.findOne(userId);
user.setFullName((String) requestBody.get("fullName"));
user.setPostOrDesignation((String) requestBody.get("designation"));
user.setCurrentComapny((String) requestBody.get("companyName"));
user.setState((String) requestBody.get("state"));
user.setCity((String) requestBody.get("city"));
user.setShortInfo((String) requestBody.get("shortInfo"));
user.setEmail((String) requestBody.get("email"));
user.setAddress((String) requestBody.get("address"));
userRepository.save(user);
response.put("STATUS", "SUCCESS");
response.put("STATUS_MESSAGE", "Successfully updated Information.");
} else {
response.put("STATUS", "ERROR");
response.put("STATUS_MESSAGE", "Something went wrong while updating information.");
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
由于我是 java 和 spring 的新手,我无法弄清楚这里发生了什么。 我怀疑这是因为应用程序中使用了“hsqldb”。 这是 build.gradle 文件。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle- plugin:1.5.2.RELEASE")
classpath 'mysql:mysql-connector-java:5.1.37'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'gs-accessing-data-mysql'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
// Use MySQL Connector-J
compile 'mysql:mysql-connector-java'
testCompile('org.springframework.boot:spring-boot-starter-test')
compile 'org.springframework.boot:spring-boot-starter-security'
runtime 'org.hsqldb:hsqldb:2.3.1'
compile 'org.apache.logging.log4j:log4j-api:2.0-beta9'
compile 'org.apache.logging.log4j:log4j-core:2.0-beta9'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile("org.apache.tomcat.embed:tomcat-embed-jasper")
compile('javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1') {
transitive = false
}
compile('org.glassfish.web:javax.servlet.jsp.jstl:1.2.1') {
transitive = false
}
}
我还设置了 application.properties 文件如下
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=abc123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.mvc.view.prefix: /
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
【问题讨论】:
-
你能在你的 gradle 中注释掉运行时 'org.hsqldb:hsqldb:2.3.1' 吗?
-
@georgesvan 当我对此发表评论时......我收到错误消息“无法确定数据库类型 NONE 的嵌入式数据库驱动程序类”
-
实际上,mysql 并没有反映你的 crud,因为它被持久化在 hsqldb 嵌入式数据库中。现在您的应用程序抛出异常,因为它无法找到您的 application.properties。
-
在你的 gradle 中添加 runtime('mysql:mysql-connector-java') 并尝试。
-
@georgesvan 不工作,使用运行时出现同样的错误('mysql:mysql-connector-java'),实际上当我第一次注册时,值存储在 mysql 数据库中。但是当我尝试更新时它不起作用。
标签: java spring spring-mvc gradle spring-data-jpa