作者:mobiledu2502883211 | 来源:互联网 | 2023-09-18 17:32
我尝试在没有SpringBoot的情况下创建Spring数据jpa程序。
配置类:
avg = Application.Worksheetfunction.Average(arr.columns(i))
测试类:
@Configuration
@EnableTransactionmanagement
@EnableJpaRepositories(basePackages = {"springdata.repository"})
@ComponentScan(basePackages = {"springdata.entities.service"})
@PropertySource("classpath:data.properties")
public class SpringDataConfig {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
final BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(env.getProperty("jdbc.url"));
basicDataSource.setusername(env.getProperty("jdbc.user"));
basicDataSource.setPassword(env.getProperty("jdbc.password"));
return basicDataSource;
}
@Bean
public Properties hibernateProperties() {
final Properties hibernateProp = new Properties();
hibernateProp.put("hibernate.dialect","org.hibernate.H2Dialect");
hibernateProp.put("hibernate.format_sql",true);
hibernateProp.put("hibernate.use_sql_comments",true);
hibernateProp.put("hibernate.show_sql",true);
hibernateProp.put("hibernate.hbm2ddl.auto","update");
hibernateProp.put("hibernate.max_fetch_depth",3);
hibernateProp.put("hibernate.jdbc.batch_size",10);
hibernateProp.put("hibernate.jdbc.fetch_size",50);
return hibernateProp;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean
public PlatformTransactionmanager transactionmanager(final EntityManagerFactory entityManagerFactory) {
return new JpaTransactionmanager(entityManagerFactory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(final JpaVendorAdapter jpaVendorAdapter,final Properties hibernateProperties,final DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPackagesToScan("springdata.entities");
em.setDataSource(dataSource);
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaProperties(hibernateProperties);
em.afterPropertiesSet();
return em;
}
}
maven依赖项:
public class Test {
public static void main(final String[] args) throws SQLException {
ApplicationContext ctx = new AnnotationconfigApplicationContext(SpringDataConfig.class);
DuckService duckService = ctx.getBean(DuckService.class);
System.out.println(duckService.findAll().size());
}
}
还有例外:
com.h2database
h2
1.4.200
org.apache.commons
commons-dbcp2
2.7.0
org.springframework
spring-jdbc
5.2.1.RELEASE
org.springframework
spring-context
5.2.1.RELEASE
org.springframework.data
spring-data-jpa
2.2.2.RELEASE
org.hibernate.javax.persistence
hibernate-jpa-2.1-api
1.0.2
org.hibernate
hibernate-entitymanager
5.4.6.Final
org.hibernate
hibernate-core
5.4.8.Final
org.hibernate.validator
hibernate-validator
6.1.0.Final
org.hibernate.validator
hibernate-validator-annotation-processor
6.1.0.Alpha2
我试图找到这个问题的答案,因此在SO上也有类似的问题,解决方案是在hibernate.javax.persistance上替换javax.persistance。但就我而言,我有一个新的例外。
更改hibernate
的方言
hibernateProp.put("hibernate.dialect","org.hibernate.dialect.H2Dialect");