我正在尝试从内存数据库中的 H2 迁移到 mysql,并在删除 H2 后将 compile("mysql:mysql-connector-java:5.1.6")
合并到我的 build.gradle 中依赖性。我还将以下内容放入我的 application.properties 中:
spring.datasource.url=jdbc:mysql://localhost:3306/mutibodb
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto: create
但是,当我重建时,我收到此错误:http://pastebin.com/4cS9Dk0U 如果我不添加 mysql 或 h2 依赖项,这与我得到的错误相同,这意味着它根本不接受它。
我使用 H2 数据库的完整现有代码在这里:https://github.com/devdeep1987/MutiboProject/tree/master/MutiboServer
您能告诉我是否有遗漏的步骤吗?
更新: 我的 CustomUserDetailsService 类如下:
@Service
public class CustomUserDetailsService implements UserDetailsService {
private UserRepository repository;
private static final Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);
@Autowired
public CustomUserDetailsService(UserRepository repo) {
this.repository = repo;
}
@Override
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
logger.info("username:"+name);
User u = repository.findByUsername(name);
if (u == null)
throw new UsernameNotFoundException("User details not found with this username: " + name);
String username = u.getUsername();
String password = u.getPassword();
List authList = new ArrayList();
authList.add(new SimpleGrantedAuthority("USER"));
//get the encoded password
//String encodedPassword = passwordEncoder.encode(password);
org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User(username, password, authList);
return user;
}
public boolean createUser(String username, String password) {
User existing = repository.findByUsername(username);
if(existing!=null)
return false;
User u = new User();
u.setUsername(username);
u.setPassword(password);
repository.save(u);
return true;
}
private List getAuthorities(String role) {
List authList = new ArrayList();
authList.add(new SimpleGrantedAuthority("USER"));
//you can also add different roles here
//for example, the user is also an admin of the site, then you can add ROLE_ADMIN
//so that he can view pages that are ROLE_ADMIN specific
if (role != null && role.trim().length() > 0) {
if (role.equals("admin")) {
authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
}
}
return authList;
}
}
我的 UserRepository 类是:
@Repository
public interface UserRepository extends CrudRepository<User, Long>{
User findByUsername(String username);
}
最佳答案
将 spring-boot-gradle-plugin 的版本从 1.0.2.RELEASE 更改为 1.1.4.RELEASE 可以修复此问题。但不知道为什么。
关于mysql - Gradle 无法识别 mysql 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28019496/