gpt4 book ai didi

java - spring boot mysql jpa Command Line Runner问题

转载 作者:行者123 更新时间:2023-11-30 21:28:36 25 4
gpt4 key购买 nike

我想使用 CommandLineRunner 在 run () 方法中创建一个用户。我想在 Tomcat 运行时自动调用此方法并向 JPA 注册它。下面分享一下我在项目中使用的代码。这样运行时,tomcat服务器运行正常,系统启动但没有注册到数据库中。我该如何解决这个问题?

用户

@Data
@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private int id;
@Column(name = "email")
@Email(message = "*Please provide a valid Email")
@NotEmpty(message = "*Please provide an email")
private String email;
@Column(name = "password")
@Length(min = 5, message = "*Your password must have at least 5 characters")
@NotEmpty(message = "*Please provide your password")
private String password;
@Column(name = "name")
@NotEmpty(message = "*Please provide your name")
private String name;
@Column(name = "last_name")
@NotEmpty(message = "*Please provide your last name")
private String lastName;
@Column(name = "active")
private int active;
//JOIN tables
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;

public User() {};

public User(String email, String password, String name, String lastName, int active) {
this.email=email;
this.password=password;
this.name=name;
this.lastName=lastName;
this.active=active;
}

//G&S
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getActive() {
return active;
}
public void setActive(int active) {
this.active = active;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}

作用

@Data
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "role_id")
private int id;
@Column(name = "role")
private String role;

//G&S
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}

用户资料库

@Repository("userRepository")
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);
}

角色库

@Repository("roleRepository")
public interface RoleRepository extends JpaRepository<Role, Integer> {
Role findByRole(String role);
}

服务

public class ServiceImplement implements CommandLineRunner{
@Autowired
UserService userService;
@Autowired
UserRepository userRepository;
@Autowired
RoleRepository roleRepository;

//$2y$12$YN6IjSzj5DYvSpv6jofxV.p8C.zSKKTdnDZ4oc0XT0TmODcUe.s62 = umut1234

User umut = null;
Role userRole = null;

@Override
public void run(String... args) throws Exception {
umut = new User();
umut.setEmail("umut@umut.com");
umut.setPassword("$2y$12$YN6IjSzj5DYvSpv6jofxV.p8C.zSKKTdnDZ4oc0XT0TmODcUe.s62");
umut.setName("Umut");
umut.setLastName("Eren");
umut.setActive(1);
userRole = roleRepository.findByRole("ADMIN");
umut.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
userRepository.save(umut);

System.out.println("***Generated***");
}
}

最佳答案

import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner

// add these 2 lines and change your impelentation to ApplicationRunner
@Component
@Order(value = 1)
public class ServiceImplement implements ApplicationRunner{
@Autowired
UserService userService;
@Autowired
UserRepository userRepository;
@Autowired
RoleRepository roleRepository;

//$2y$12$YN6IjSzj5DYvSpv6jofxV.p8C.zSKKTdnDZ4oc0XT0TmODcUe.s62 = umut1234

User umut = null;
Role userRole = null;

@Override
public void run(ApplicationArguments args) throws Exception {
umut = new User();
umut.setEmail("umut@umut.com");
umut.setPassword("$2y$12$YN6IjSzj5DYvSpv6jofxV.p8C.zSKKTdnDZ4oc0XT0TmODcUe.s62");
umut.setName("Umut");
umut.setLastName("Eren");
umut.setActive(1);
//save user before add roles to it
userRepository.save(umut);
userRole = roleRepository.findByRole("ADMIN");
umut.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
userRepository.save(umut);

System.out.println("***Generated***");
}
}

关于java - spring boot mysql jpa Command Line Runner问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500237/

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