我是Spring的开始者,请帮忙
实体
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String username;
private String password;
@ManyToMany
@JoinTable(name = "users_roles",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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;
}
protected User(){}
public User(String name, String pass) {
username = name;
password = pass;
}
}
存储库:
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
控制器:
@RestController
@RequestMapping("/users")
@PreAuthorize("hasRole('ADMIN')")
public class UserController {
@Autowired
UserRepository userRepository;
@RequestMapping(method = RequestMethod.GET)
public List<User> getUsers()
{
List<User> result = new ArrayList<User>();
userRepository.findAll().forEach(result::add);
return result;
}
@RequestMapping(method = RequestMethod.POST)
public User addUser(String username, String password, String password_confirm)
{
if (username.isEmpty() || password.isEmpty() || password_confirm.isEmpty())
return null;
if (!password.equals(password_confirm))
return null;
return userRepository.save(new User(username, password));
}
@RequestMapping(value = "/add",method = RequestMethod.GET)
public ModelAndView getUserForm()
{
return new ModelAndView("add");
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable("id") Long id)
{
userRepository.delete(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable("id") Long id)
{
return userRepository.findOne(id);
}
}
数据配置 :
@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:hibernate.properties"})
@EnableJpaRepositories( "com.sergey.repository")
public class DataConfig {
@Autowired
private Environment environment;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("com.sergey.entity");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource());
emfb.setPackagesToScan("com.sergey.entity");
emfb.setJpaVendorAdapter(jpaVendorAdapter());
emfb.setJpaProperties(hibernateProperties());
return emfb;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
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>
<groupId>springSecure</groupId>
<artifactId>springSecure</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring.version>4.3.2.RELEASE</spring.version>
<spring.security.version>4.0.4.RELEASE</spring.security.version>
<hibernate-version>5.0.11.Final</hibernate-version>
<jdk.version>1.8</jdk.version>
</properties>
<dependencies>
<!--Spring Framework-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring Security-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<!--Spring DB-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa
-
</dependencies>
</project>
错误:
2016年8月29日下午3:33:04 org.apache.catalina.core。ApplicationContext日志
Spring存储库在运行时由Spring JPA框架实现,@NoRepositoryBean只应标记为BaseRepositories,BaseRepostories用于为所有子接口(如果有)提供一些默认行为。
例外情况是“没有找到 [com.sergey.存储库.用户存储库]
类型的合格 Bean”作为依赖项,这是因为 Spring 容器没有提供实现,因为它被标记为@NoRepositoryBean
换句话说,您不应该在用户存储库
中添加@NoRepositoryBean
注释,因为它是一个具体的存储库接口,需要由Spring-JPA框架(在运行时)实现。因此,删除顶部的注释,然后用户存储库
类将变为如下所示:
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
有关更多详细信息,请参阅以下文档:
http://docs . spring . io/spring-data/JPA/docs/current/reference/html/
问题内容: 我正在尝试集成PostgreSQL和SQLAlchemy,但是SQLAlchemy.create_all()并未从我的模型中创建任何表。 我的代码: 但是我得到这个错误: 我怎样才能解决这个问题? 问题答案: 您应该在调用之前放置模型类,如下所示: 如果您的模型是在单独的模块中声明的,请在调用之前将其导入。 说,模型在名为的文件中, 重要说明: 在初始化对象之后导入模型很重要,因为在_
问题内容: 我的档案中包含以下模型listpull/models.py: 我打电话./run.py init,然后./run.py migrate然后./run.py upgrade,我看到迁移文件生成,但其空: 运行 listpull / init.py 更新 如果我通过运行,然后执行和,那么将获得以下架构: 不幸的是,迁移仍然无法进行。 问题答案: 当你调用命令Flask-Migrate(或实
我有一个使用Spring Boot的中等规模项目,我正在尝试使用嵌入式H2创建我的第一个DataJpaTest,但我遇到了以下例外: 我已经尝试了这一点,并使用了一个模式。sql,还有这个和使用测试。测试/资源中的属性,以及其他答案。但什么都没用。我真的很困惑;这是我第一次在Spring Boot中遇到无法解决的问题。 我的实体类定义为: 关于如何强制Hibernate在H2中创建模式的任何建议?
我刚接触hibernate并在Spring5中使用它,我有一个配置类创建sessionFactory bean,但它不工作(创建),当我运行我的项目时,我得到这个错误: 异常为org.springframework.beans.factory.beanCreationException:创建com.t4b.project.pricebuy.configuration.hibernateConfig
这怎么了? 我有Windows10 Pro。所以,我有Docker Desktop并且我正在使用WSL2
我的项目是一个Maven项目,具有以下依赖关系: 为了保持文章的简短性,我试图只放置相关的依赖项,避免其他的依赖项(Lombok、jpa等) 和我的graphqls文件(在参考资料文件夹中):