使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應(yīng)用
為了提供一個單包易部署的服務(wù)器應(yīng)用,考慮使用Spring Boot,因?yàn)槠浼闪薃pache Tomcat,易于運(yùn)行,免去絕大部分了服務(wù)器配置的步驟。
項(xiàng)目初始化
首先從mvn archetype:generate
中選擇 com.github.mkspcd:simple-webapp
(或其他webapp模版) 模版生成項(xiàng)目結(jié)構(gòu)。
更多關(guān)于maven請移步Maven - Users Centre
在pom.xml中添加parent來獲取Spring Boot所需的最小依賴。
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.github.hwding.example</groupId> <artifactId>example</artifactId> <packaging>jar</packaging> <version>0.0.1</version> <name>an example</name> <url>https://github.com/hwding</url> <!-- 添加Spring的Repository以便于添加相關(guān)組件 --> <repositories> <repository> <url>http://repo.spring.io/milestone/</url> <id>repo-spring</id> </repository> </repositories> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <build> <finalName>example</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 編譯級別,可選 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 用于Hibernate4的SQLite3 Dialect --> <dependency> <groupId>com.enigmabridge</groupId> <artifactId>hibernate4-sqlite-dialect</artifactId> <version>0.1.2</version> </dependency> <!-- 用于配置數(shù)據(jù)源 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>javax.persistence</artifactId> <version>2.2.0-RC1</version> </dependency> <!-- SQLite3 驅(qū)動 --> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.20.0</version> </dependency> </dependencies> </project>
pom中同時添加了Hibernate以及Spring JPA等相關(guān)組件。
配置數(shù)據(jù)源
@Configuration public class DataSourceConfiguration { @Bean(destroyMethod = "", name = "EmbeddeddataSource") public DataSource dataSource() { DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(); dataSourceBuilder.driverClassName("org.sqlite.JDBC"); dataSourceBuilder.url("jdbc:sqlite:" + "example.db"); dataSourceBuilder.type(SQLiteDataSource.class); return dataSourceBuilder.build(); } }
這里設(shè)置了該Bean的destroyMethod = ""是為了防止停止服務(wù)器時容器管理器兩次銷毀導(dǎo)致的異常,name = "EmbeddeddataSource"用于在自動裝配Bean時與其他dataSource加以區(qū)分。
為了使該獨(dú)立服務(wù)易部署易分發(fā),使用SQLite3作為數(shù)據(jù)存取的源,值得注意的是,該場景非常少見。
配置Spring Data JPA
@Configuration @EnableJpaRepositories( basePackages = "com.github.hwding.example.data.repository", transactionManagerRef = "jpaTransactionManager", entityManagerFactoryRef = "localContainerEntityManagerFactoryBean" ) @EnableTransactionManagement public class JpaConfiguration { @Autowired @Bean public JpaTransactionManager jpaTransactionManager(@Qualifier(value = "EmbeddeddataSource") DataSource dataSource, EntityManagerFactory entityManagerFactory) { JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); jpaTransactionManager.setEntityManagerFactory(entityManagerFactory); jpaTransactionManager.setDataSource(dataSource); return jpaTransactionManager; } @Autowired @Bean LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(@Qualifier(value = "EmbeddeddataSource") DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); localContainerEntityManagerFactoryBean.setDataSource(dataSource); localContainerEntityManagerFactoryBean.setPackagesToScan("com.github.hwding.example.data.model.local"); localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter); return localContainerEntityManagerFactoryBean; } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setGenerateDdl(true); hibernateJpaVendorAdapter.setShowSql(true); hibernateJpaVendorAdapter.setDatabasePlatform("com.enigmabridge.hibernate.dialect.SQLiteDialect"); return hibernateJpaVendorAdapter; } }
注意Repository和Entity掃描的包路徑需要根據(jù)實(shí)際進(jìn)行調(diào)整。
hibernateJpaVendorAdapter.setGenerateDdl(true);
能夠在初次運(yùn)行時自動根據(jù)Entity的定義生成DDL并自動創(chuàng)建SQLite3的 .db 數(shù)據(jù)文件,在本例中是 example.db ,DDL會最小程度的滿足Entity的定義;如果該文件已經(jīng)存在,則并不會對其進(jìn)行覆蓋。
由于Hibernate并不對SQLite3提供支持,所以需要提供第三方Dialect給它:hibernateJpaVendorAdapter.setDatabasePlatform("com.enigmabridge.hibernate.dialect.SQLiteDialect");,這個類我們已經(jīng)在pom中引入了。
配置入口
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
Spring Boot能夠從JAR包的入口直接啟動整個應(yīng)用程序。
總結(jié)
以上所述是小編給大家介紹的使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應(yīng)用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java實(shí)現(xiàn)PDF轉(zhuǎn)圖片的三種方法
有些時候我們需要在項(xiàng)目中展示PDF,所以我們可以將PDF轉(zhuǎn)為圖片,然后已圖片的方式展示,效果很好,Java使用各種技術(shù)將pdf轉(zhuǎn)換成圖片格式,并且內(nèi)容不失幀,本文給大家介紹了三種方法實(shí)現(xiàn)PDF轉(zhuǎn)圖片的案例,需要的朋友可以參考下2023-10-10java中的日期時間類Date和SimpleDateFormat
這篇文章主要介紹了java中的日期時間類Date和SimpleDateFormat,Date類的對象在Java中代表的是當(dāng)前所在系統(tǒng)的此刻日期時間,說白了就是你計算機(jī)上現(xiàn)實(shí)的時間,需要的朋友可以參考下2023-09-09Java實(shí)現(xiàn)將彩色PDF轉(zhuǎn)為灰度PDF的示例代碼
本文以Java代碼為例介紹如何實(shí)現(xiàn)將彩色PDF文件轉(zhuǎn)為灰度(黑白)的PDF文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2022-03-03idea編寫yml、yaml文件以及其優(yōu)先級的使用
本文主要介紹了idea編寫yml、yaml文件以及其優(yōu)先級的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Spring Boot 防止接口惡意刷新和暴力請求的實(shí)現(xiàn)
本文主要介紹了Spring Boot 防止接口惡意刷新和暴力請求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06