已经创建了数据库,但是当尝试在数据库中插入新数据时,数据没有保存在数据库中,并且不会显示任何错误。我试图从发布的问题中寻找解决方案,但是没有一个解决问题。我没有在pom.xml
中包括h2依赖项。我试图在用户类中实现Serializable
,但也没有解决问题
Application.properties
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/uwezo_banking
spring.datasource.username= root
spring.datasource.password=
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.validation-query= SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
logging.level.org.springframework=DEBUG
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.uwezotech</groupId>
<artifactId>uwezotech</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>uwezotech</name>
<description>An online banking web application</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>font-awesome</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>popper.js</artifactId>
<version>1.14.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
User.java
package com.uwezotech.domain;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "userId", nullable = false, updatable = false)
private Long userId;
private String username;
private String password;
private String firstName;
private String lastName;
private boolean enabled = true;
private String email;
private String phone;
@OneToOne
private PrimaryAccount primaryAccount;
@OneToOne
private SavingsAccount savingsAccount;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<Appointment> appointmentList;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<Recipient> recipientList;
@JsonBackReference
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "user_roles", joinColumns = {@JoinColumn(name = "user_id"
)}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
private List<Role> roles = new ArrayList<>();
public User() {
}
public User(String username, String password, String firstName, String lastName, boolean enabled, String email, String phone, String confirmPassword, PrimaryAccount primaryAccount, SavingsAccount savingsAccount, List<Appointment> appointmentList, List<Recipient> recipientList, List<Role> roles) {
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.enabled = enabled;
this.email = email;
this.phone = phone;
this.primaryAccount = primaryAccount;
this.savingsAccount = savingsAccount;
this.appointmentList = appointmentList;
this.recipientList = recipientList;
this.roles = roles;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public PrimaryAccount getPrimaryAccount() {
return primaryAccount;
}
public void setPrimaryAccount(PrimaryAccount primaryAccount) {
this.primaryAccount = primaryAccount;
}
public SavingsAccount getSavingsAccount() {
return savingsAccount;
}
public void setSavingsAccount(SavingsAccount savingsAccount) {
this.savingsAccount = savingsAccount;
}
public List<Appointment> getAppointmentList() {
return appointmentList;
}
public void setAppointmentList(List<Appointment> appointmentList) {
this.appointmentList = appointmentList;
}
public List<Recipient> getRecipientList() {
return recipientList;
}
public void setRecipientList(List<Recipient> recipientList) {
this.recipientList = recipientList;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", username='" + username + '\'' +
", password='" + password + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", enabled=" + enabled +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", primaryAccount=" + primaryAccount +
", savingsAccount=" + savingsAccount +
", appointmentList=" + appointmentList +
", recipientList=" + recipientList +
", roles=" + roles +
'}';
}
}
UserDto.java
package com.uwezotech.dto;
import com.uwezotech.validation.PasswordMatches;
import com.uwezotech.domain.Role;
import javax.validation.constraints.NotBlank;
import java.util.Set;
@PasswordMatches(message = "Password does not match")
public class UserDto {
private Long id;
@NotBlank(message = "First name is required")
private String firstName;
@NotBlank(message = "Last name is required")
private String lastName;
@NotBlank(message = "Phone number is required")
private String phone;
@NotBlank(message = "Password is required")
private String username;
@NotBlank(message = "Email is required")
private String email;
private boolean enabled;
@NotBlank(message = "Password is required")
private String password;
@NotBlank(message = "Matching password is required")
private String matchingPassword;
public UserDto(Long id, @NotBlank(message = "First name is required") String firstName, @NotBlank(message = "Last name is required") String lastName, @NotBlank(message = "Phone number is required") String phone, @NotBlank(message = "Password is required") String username, @NotBlank(message = "Email is required") String email, boolean enabled, @NotBlank(message = "Password is required") String password, @NotBlank(message = "Matching password is required") String matchingPassword, Set<Role> roles) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
this.username = username;
this.email = email;
this.enabled = enabled;
this.password = password;
this.matchingPassword = matchingPassword;
}
public UserDto() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMatchingPassword() {
return matchingPassword;
}
public void setMatchingPassword(String matchingPassword) {
this.matchingPassword = matchingPassword;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
UserServiceImpl.java
package com.uwezotech.service.userServiceImpl;
import com.uwezotech.dao.RoleDao;
import com.uwezotech.dao.UserDao;
import com.uwezotech.domain.User;
import com.uwezotech.service.AccountService;
import com.uwezotech.service.UserService;
import com.uwezotech.dto.UserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements UserService{
private static final Logger LOG = LoggerFactory.getLogger(UserService.class);
@Autowired
private UserDao userDao;
@Autowired
private AccountService accountService;
@Autowired
private RoleDao roleDao;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
public User findByUsername(String username) {
return userDao.findByUsername(username);
}
public User findByEmail(String email) {
return userDao.findByEmail(email);
}
public boolean checkUserExists(String username, String email) {
if (checkUsernameExists(username) || checkEmailExists(username)) {
return true;
} else {
return false;
}
}
public boolean checkUsernameExists(String username) {
if (null != findByUsername(username)) {
return true;
}
return false;
}
public boolean checkEmailExists(String email) {
if (null != findByEmail(email)) {
return true;
}
return false;
}
public void save(User user) {
userDao.save(user);
}
public User createNewAccount(UserDto userDto){
User user = new User();
user.setFirstName(userDto.getFirstName());
user.setLastName(userDto.getLastName());
user.setUsername(userDto.getUsername());
user.setPhone(userDto.getPhone());
user.setEmail(userDto.getEmail());
user.setPassword(passwordEncoder.encode(userDto.getPassword()));
user.setRoles(Collections.singletonList(roleDao.findByName("ROLE_USER")));
user.setPrimaryAccount(accountService.createPrimaryAccount());
user.setSavingsAccount(accountService.createSavingsAccount());
return user;
}
public List<User> findUserList() {
return userDao.findAll();
}
public void enableUser(String username) {
}
public void disableUser(String username) {
}
}
HomeController.java
package com.uwezotech.controllers;
import com.uwezotech.dao.RoleDao;
import com.uwezotech.domain.User;
import com.uwezotech.service.UserService;
import com.uwezotech.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
@Controller
@RequestMapping("")
public class HomeController {
@Autowired
private UserService userService;
@Autowired
private RoleDao roleDao;
@RequestMapping("/")
public String index(){
return "redirect:/index";
}
@RequestMapping("/index")
public String home(){
return "index";
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView showRegister(ModelAndView modelAndView, UserDto userDto){
modelAndView.addObject("userDto", userDto);
modelAndView.setViewName("register");
return modelAndView;
}
@PostMapping(value = "/submit-registration")
public ModelAndView saveUser(ModelAndView modelAndView, @ModelAttribute("userDto") @Valid final UserDto userDto, BindingResult bindingResult, HttpServletRequest request, Errors errors){
User emailExists = userService.findByEmail(userDto.getEmail());
User userNameExists = userService.findByUsername(userDto.getUsername());
System.out.println(emailExists);
if(emailExists != null){
modelAndView.setViewName("/register");
bindingResult.rejectValue("email", "alreadyRegisteredEmail", "There is already a registered with email provided.");
}
if(userNameExists !=null){
modelAndView.setViewName("/register");
bindingResult.rejectValue("username", "alreadyRegisteredUsername", "There is already a user registered with username provided");
}
if(bindingResult.hasErrors()){
modelAndView.setViewName("/register");
}else{
User user = userService.createNewAccount(userDto);
user.setEnabled(true);
userService.save(user);
}
return modelAndView;
}
}