автозаполнение базы

pull/2/head
L_DelOff 2023-03-04 21:49:17 +03:00
parent b21d3a6a5b
commit 7bb16ec60d
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package ru.ldeloff.hedgehogcloud.config.initclass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.ldeloff.hedgehogcloud.entity.RoleEntity;
import ru.ldeloff.hedgehogcloud.entity.UserEntity;
import ru.ldeloff.hedgehogcloud.service.RoleService;
import ru.ldeloff.hedgehogcloud.service.UserService;
import java.util.HashSet;
import java.util.List;
@Component
public class InitDB implements ApplicationRunner {
UserService userService;
RoleService roleService;
public InitDB(UserService userService, RoleService roleService) {
this.userService = userService;
this.roleService = roleService;
}
@Override
@Transactional
public void run(ApplicationArguments args) throws Exception {
RoleEntity role = new RoleEntity();
role.setName("ROLE_ADMIN");
roleService.saveRole(role);
UserEntity user = new UserEntity();
user.setUsername("admin");
user.setPassword("123");
user.setRoles(new HashSet<>(List.of(roleService.getByName(role.getName()))));
userService.saveUser(user);
}
}