gpt4 book ai didi

java - 更新的信息没有反射(reflect)在mysql数据库中

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:40 26 4
gpt4 key购买 nike

我通过引用此博客设置了一个项目,但我没有使用 Maven,而是使用了 gradle。 Registration and Login Example with Spring Security, Spring Data JPA, Spring Boot

一切正常,除了更新 Mysql 数据库中的值不起作用。

我创建了一个请求映射器来更新用户信息。我已检查请求正文不为空并且具有所有值。另外,我收到的响应状态为“成功”。但是当我在 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

最佳答案

一般来说,您需要一个事务服务层。

@Service
@Transactional
public class MyService {
}

简而言之:

  • 声明存储库
  • 声明使用这些存储库的事务服务
  • 在 Controller 内使用您的服务

为了让您的代码以快速而肮脏的方式工作,请尝试使用 @Transactional 注释您的 updateRegiatration 方法:

@Transactional
@RequestMapping(value = "/updateRegistration", method = RequestMethod.POST)
public Map<String, Object> updateRegiatration(@RequestBody Map<String, Object> requestBody) {
}

无论如何,如果您查看示例代码,您会看到这个服务层:

@Service
public class UserDetailsServiceImpl implements UserDetailsService{
@Autowired
private UserRepository userRepository;

@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);

Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
for (Role role : user.getRoles()){
grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
}

return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
}
}

Controller 使用服务,而不是存储库:

@Controller
public class UserController {
@Autowired
private UserService userService;

@Autowired
private SecurityService securityService;

关于java - 更新的信息没有反射(reflect)在mysql数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43706207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com