91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

springboot jpa配置的方法是什么

小億
86
2023-09-21 17:35:17
欄目: 編程語言

在Spring Boot中配置JPA的方法有以下幾種:

  1. 使用application.properties文件:在application.properties文件中配置JPA相關的屬性,如數據庫連接信息、實體類掃描路徑、數據庫方言等。示例:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.use_minimal_puts=true
spring.jpa.properties.hibernate.cache.use_structured_entries=true
spring.jpa.properties.hibernate.cache.infinispan.statistics=true
spring.jpa.properties.hibernate.cache.infinispan.debug=true
spring.jpa.properties.hibernate.cache.infinispan.eviction.strategy=LRU
spring.jpa.properties.hibernate.cache.infinispan.eviction.max-entries=10000
spring.jpa.properties.hibernate.cache.infinispan.eviction.wake-up-interval=2000
  1. 使用application.yml文件:在application.yml文件中配置JPA相關的屬性,語法與application.properties類似。示例:
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: 123456
jpa:
show-sql: true
hibernate:
ddl-auto: update
dialect: org.hibernate.dialect.MySQL5Dialect
format_sql: true
use_sql_comments: true
id:
new_generator_mappings: false
enable_lazy_load_no_trans: true
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
hibernate.cache.use_query_cache: true
hibernate.cache.use_minimal_puts: true
hibernate.cache.use_structured_entries: true
hibernate.cache.infinispan.statistics: true
hibernate.cache.infinispan.debug: true
hibernate.cache.infinispan.eviction.strategy: LRU
hibernate.cache.infinispan.eviction.max-entries: 10000
hibernate.cache.infinispan.eviction.wake-up-interval: 2000
  1. 使用編程方式配置:通過編寫Java代碼配置JPA,可以在@Configuration類中使用@EnableJpaRepositories注解和@Bean注解配置JPA相關的屬性。示例:
@Configuration
@EnableJpaRepositories(basePackages = "com.example.repository")
public class JpaConfig {
@Autowired
private DataSource dataSource;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.example.entity");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
return em;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
}

無論使用哪種配置方式,都需要引入相關的依賴,如spring-boot-starter-data-jpa、mysql-connector-java等。

0
水富县| 襄城县| 砀山县| 东阿县| 台山市| 富裕县| 香格里拉县| 黄山市| 定边县| 舒兰市| 衢州市| 新丰县| 佛坪县| 屏东县| 广宗县| 文水县| 静安区| 兴业县| 安龙县| 拉孜县| 芒康县| 瑞丽市| 思茅市| 临沂市| 肇东市| 全椒县| 福海县| 兴仁县| 长兴县| 河曲县| 贵德县| 鹰潭市| 赤峰市| 冕宁县| 姜堰市| 安宁市| 孝义市| 通城县| 棋牌| 响水县| 潼关县|