我通过引用此博客设置了一个项目,但我没有使用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的新手,我无法弄清楚这里发生了什么。 我怀疑这是因为&#39; hsqldb&#39;在应用程序中使用。 这是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
答案 0 :(得分:0)
通常,您需要一个交易服务层。
@Service
@Transactional
public class MyService {
}
简而言之:
只是为了让您的代码快速而肮脏地运行,请尝试使用updateRegiatration
注释@Transactional
方法:
@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
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;