您好,登錄后才能下訂單哦!
為了提供一個單包易部署的服務(wù)器應(yīng)用,考慮使用Spring Boot,因為其集成了Apache Tomcat,易于運行,免去絕大部分了服務(wù)器配置的步驟。
項目初始化
首先從mvn archetype:generate
中選擇 com.github.mkspcd:simple-webapp
(或其他webapp模版) 模版生成項目結(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ū)分。
為了使該獨立服務(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ù)實際進行調(diào)整。
hibernateJpaVendorAdapter.setGenerateDdl(true);
能夠在初次運行時自動根據(jù)Entity的定義生成DDL并自動創(chuàng)建SQLite3的 .db 數(shù)據(jù)文件,在本例中是 example.db ,DDL會最小程度的滿足Entity的定義;如果該文件已經(jīng)存在,則并不會對其進行覆蓋。
由于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)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。