2-2、Mybatis 轉 JPA輕量級Hibernate使用介紹
阿新 • • 發佈:2018-11-08
JPA 做為spring 開源的子專案,一個spring 框架中的一種,它符合Hibernate的使用規範。作為spring boot 和spring cloud 的首選ORM,是快速開發的神器。https://projects.spring.io/spring-data-jpa/
JPA的優勢:運維再也不用擔心這個SQL沒有用到,那個指令碼沒有執行,導致系統出問題。分分鐘上手JPA,自動建表,更新欄位,自動執行SQL,再也不需要繁瑣的配置了(簡稱無腦式呼叫)。
JPA的缺點:遇上覆雜的多表聯合查詢語句,JPA沒這麼友好(對於PPS一些簡單的元件查詢可以選擇)
1.config定義
DatabaseConfig1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package
com.arcvideo.pgcliveplatformserver.config;
import
com.zaxxer.hikari.HikariConfig;
import
com.zaxxer.hikari.HikariDataSource; import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.context.annotation.PropertySource;
import
org.springframework.core.env.Environment;
import
org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import
org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import
org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import
org.springframework.transaction.annotation.EnableTransactionManagement;
import
javax.sql.DataSource;
import
java.util.Properties;
@Configuration
@EnableTransactionManagement(proxyTargetClass =
true
)
@EnableJpaRepositories(basePackages = {
"com.arcvideo.pgcliveplatformserver.repo"
})
@PropertySource(
"classpath:database.properties"
)
public
class
DatabaseConfig {
@Autowired
private
Environment env;
@Bean(destroyMethod =
"close"
)
DataSource dataSource() {
HikariConfig dataSourceConfig =
new
HikariConfig();
dataSourceConfig.setPoolName(
"springHikariCP"
);
dataSourceConfig.setConnectionTestQuery(
"SELECT 1"
);
dataSourceConfig.setDataSourceClassName(env.getProperty(
"hibernate.connection.driver_class"
));
Properties dataSourceProperties =
new
Properties();
dataSourceProperties.put(
"url"
, env.getProperty(
"hibernate.connection.url"
));
dataSourceProperties.put(
"user"
, env.getProperty(
"hibernate.connection.username"
));
dataSourceProperties.put(
"password"
, env.getProperty(
"hibernate.connection.password"
));
dataSourceConfig.setDataSourceProperties(dataSourceProperties);
return
new
HikariDataSource(dataSourceConfig);
}
@Bean
public
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactory =
new
LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource());
entityManagerFactory.setPackagesToScan(env.getProperty(
"entitymanager.packagesToScan"
));
HibernateJpaVendorAdapter vendorAdapter =
new
HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
Properties additionalProperties =
new
Properties();
additionalProperties.put(
"hibernate.dialect"
, env.getProperty(
"hibernate.dialect"
));
additionalProperties.put(
"hibernate.cache.use_query_cache"
, env.getProperty(
"hibernate.cache.use_query_cache"
));
additionalProperties.put(
"hibernate.cache.use_second_level_cache"
, env.getProperty(
"hibernate.cache.use_second_level_cache"
));
additionalProperties.put(
"hibernate.cache.region.factory_class"
, env.getProperty(
"hibernate.cache.region.factory_class"
));
additionalProperties.put(
"hibernate.autoReconnect"
, env.getProperty(
"hibernate.autoReconnect"
));
additionalProperties.put(
"hibernate.hbm2ddl.auto"
, env.getProperty(
"hibernate.hbm2ddl.auto"
));
additionalProperties.put(
"hibernate.show_sql"
, env.getProperty(
"hibernate.show_sql"
));
additionalProperties.put(
"hibernate.format_sql"
, env.getProperty(
"hibernate.format_sql"
));
additionalProperties.put(
"javax.persistence.sharedCache.mode"
, env.getProperty(
"javax.persistence.sharedCache.mode"
));
additionalProperties.put(
"hibernate.generate_statistics"
, env.getProperty(
"hibernate.generate_statistics"
));
entityManagerFactory.setJpaProperties(additionalProperties);
return
entityManagerFactory;
}
}
|
2.database.properties 定義
database.properties1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
############################################################################################################
# DATABASE CONFIGURATION FILE #
############################################################################################################
#
# Configurations
for
JDBC
hibernate.connection.url=jdbc:mysql:
//localhost:3306/pgcliveplatformserver?useUnicode=yes&characterEncoding=UTF-8
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.driver_class=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
# Configurations
for
hibernate
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.cache.use_second_level_cache=
true
hibernate.cache.use_query_cache=
true
net.sf.ehcache.configurationResourceName=/ehcache.xml
hibernate.generate_statistics=
false
hibernate.autoReconnect=
true
hibernate.hbm2ddl.auto=update
hibernate.show_sql=
false
hibernate.format_sql=
false
javax.persistence.sharedCache.mode=ENABLE_SELECTIVE
entitymanager.packagesToScan=com.arcvideo.pgcliveplatformserver.entity
|
3.實體類直接定義表結構,表字段會自動建表建欄位
4.JPA repo 定義直接可以查詢
5.簡單SQL 自定義查詢
6.特殊複雜多結構查詢
優化建議:雖然JPA可以自動建立表實體欄位,但是不會增加資料。spring boot 新建data.sql會預設執行其中sql,這樣有助於初始化資料引數配置。