package com.repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
package com.service;
@Service
public class AppointmentPaymentServiceImpl implements AppointmentPaymentService {
@Autowired
private UserRepository userRepository;
......
......
}
package com.config;
@Configuration
@PropertySource(value = { "classpath:application.properties" })
@EnableTransactionManagement
@EnableJpaRepositories("com.repository.*")
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class DBConfig {
@Value("${spring.datasource.driver-class-name}")
public String driver;
@Value("${spring.datasource.url}")
public String url;
@Value("${spring.datasource.username}")
public String username;
@Value("${spring.datasource.password}")
public String password;
@Value("${spring.jpa.properties.hibernate.dialect}")
public String dialect;
@Value("${spring.jpa.hibernate.ddl-auto}")
public String ddl;
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "sessionFactory")
public LocalSessionFactoryBean hibernateSessionFactory(DataSource dataSource) {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.model.*" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
HibernateTransactionManager transactionManagerHib(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
/*@Bean
@Qualifier(value = "entityManager")
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}*/
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.MYSQL);
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("com.model.*");
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(hibernateProperties());
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto", ddl);
setProperty("hibernate.connection.useUnicode", "true");
setProperty("spring.jpa.hibernate.ddl-auto", ddl);
setProperty("hibernate.dialect", dialect);
setProperty("spring.jpa.properties.hibernate.dialect", dialect);
setProperty("hibernate.globally_quoted_identifiers", "true");
setProperty("hibernate.connection.CharSet", "utf8mb4");
setProperty("hibernate.connection.characterEncoding", "utf8");
}
};
}
}
package com;
@SpringBootApplication
@ComponentScan(basePackages = { "com.*"})
@EnableCaching
@Configuration
@PropertySource({"classpath:logging.properties"})
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
.
.
.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
</dependencies>
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.service.AppointmentPaymentServiceImpl required a bean of type 'com.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.repository.UserRepository' in your configuration.
我的用户实体:
package com.model;
@Entity
@Table(name = "USER")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@NotNull
@Column(name = "city_id")
private Long cityId;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
@Column(name = "locality")
private String locality;
@Column(name = "gender")
private String gender;
}
包名:com.sample 现在,当我把所有文件放在一个包下时,它工作得很好。但当我根据功能进行分配时,错误就会发生。我该如何解决这个问题。 按照下面的建议添加basepackages后,我收到的错误为
我是spring的初学者。所以现在我开始学习spring boot并构建这个简单的项目,但是当我运行它时,我得到了这样的错误“Field entityManager in sagala.rest.boot.remade.dao.EmployeeDaoImpl required a bean of type'javax.persistence.entityManager'that count fou
我对整个Spring的生态系统都是陌生的。我一直在学习一些教程,能够创建一个Spring Boot应用程序并执行crud操作。然后我开始把这个项目改成mybatis的标准。 我已经尝试了许多其他类似问题的答案,但到目前为止没有一个是有效的。 下面是问题陈述: 实现类实现为: 我的Mapper类如下所示: 我的Mapper.xml课是: 最后是我的控制器类: 我得到的错误是: 描述: com.cru
描述:com.mongotest.demo.seeder中构造函数的参数0需要类型为“com.mongotest.repositories.studentrepository”的bean,但找不到该bean。 pom.xml