为啥我的 MySql 出现用户注册错误
Posted
技术标签:
【中文标题】为啥我的 MySql 出现用户注册错误【英文标题】:Why do i have User registration error with MySql为什么我的 MySql 出现用户注册错误 【发布时间】:2021-02-07 08:28:11 【问题描述】:我是 mysql 的新手,当我尝试注册新用户时出现错误
java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“id,角色)值(25,'USER')'附近使用正确的语法 在 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.21.jar:8.0.21]
注册控制器.Java `
package com.example.fitter.controller;
import com.example.fitter.domain.Role;
import com.example.fitter.domain.User;
import com.example.fitter.repos.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.Collections;
import java.util.Map;
@Controller
public class RegistrationController
@Autowired
private UserRepo userRepo;
@GetMapping("/registration")
public String registration()
return "registration";
@PostMapping("/registration")
public String addUser(User user, Map<String, Object> model)
User userFromDb = userRepo.findByUsername(user.getUsername());
if (userFromDb != null)
model.put("message", "User exists!");
return "registration";
user.setActive(true);
user.setRoles(Collections.singleton(Role.USER));
userRepo.save(user);
return "redirect:/login";
` WebSecurityConfig.Java
``` package com.example.fitter.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import javax.sql.DataSource;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception
http
.authorizeRequests()
.antMatchers("/", "/registration").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(NoOpPasswordEncoder.getInstance())
.usersByUsernameQuery("select username, password, active from usr where username=?")
.authoritiesByUsernameQuery("select u.username, ur.roles from usr u inner join user_role ur on u.id = ur.user_id where u.username=?");
User.Java
package com.example.fitter.domain;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "usr")
public class User
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private boolean active;
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@CollectionTable(name = "userRole", joinColumns = @JoinColumn(name = "user id"))
@Enumerated(EnumType.STRING)
private Set<Role> roles;
public User()
public Long getId()
return id;
public void setId(Long id)
this.id = id;
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 boolean isActive()
return active;
public void setActive(boolean active)
this.active = active;
public Set<Role> getRoles()
return roles;
public void setRoles(Set<Role> roles)
this.roles = roles;
'
【问题讨论】:
在@JoinColumn(name = "user id")
中,user
和 id
之间有错字(空格)。检查这是否不是问题。
【参考方案1】:
这个好像错了:
@JoinColumn(name = "user id")
我猜是表中的user_id,所以应该是:
@JoinColumn(name = "user_id")
【讨论】:
【参考方案2】:在 User.Java 而不是 @CollectionTable(name = "userRole", joinColumns = @JoinColumn(name = "user id"))
中的错误太愚蠢了
@CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
【讨论】:
以上是关于为啥我的 MySql 出现用户注册错误的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 MySQL 数据库在 Windows IIS 6 上崩溃并出现 VMWARE 错误 1784