使用Spring Boot快速构建基于SQLite数据源的应用
为了提供一个单包易部署的服务器应用,考虑使用spring boot,因为其集成了apache tomcat,易于运行,免去绝大部分了服务器配置的步骤。
项目初始化
首先从mvn archetype:generate
中选择 com.github.mkspcd:simple-webapp
(或其他webapp模版) 模版生成项目结构。
更多关于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以便于添加相关组件 --> <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> <!-- 用于配置数据源 --> <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 驱动 --> <dependency> <groupid>org.xerial</groupid> <artifactid>sqlite-jdbc</artifactid> <version>3.20.0</version> </dependency> </dependencies> </project>
pom中同时添加了hibernate以及spring jpa等相关组件。
配置数据源
@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(); } }
这里设置了该bean的destroymethod = ""是为了防止停止服务器时容器管理器两次销毁导致的异常,name = "embeddeddatasource"用于在自动装配bean时与其他datasource加以区分。
为了使该独立服务易部署易分发,使用sqlite3作为数据存取的源,值得注意的是,该场景非常少见。
配置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扫描的包路径需要根据实际进行调整。
hibernatejpavendoradapter.setgenerateddl(true);
能够在初次运行时自动根据entity的定义生成ddl并自动创建sqlite3的 .db 数据文件,在本例中是 example.db ,ddl会最小程度的满足entity的定义;如果该文件已经存在,则并不会对其进行覆盖。
由于hibernate并不对sqlite3提供支持,所以需要提供第三方dialect给它:hibernatejpavendoradapter.setdatabaseplatform("com.enigmabridge.hibernate.dialect.sqlitedialect");,这个类我们已经在pom中引入了。
配置入口
@springbootapplication public class application { public static void main(string[] args) { springapplication.run(application.class); } }
spring boot能够从jar包的入口直接启动整个应用程序。
总结
以上所述是小编给大家介绍的使用spring boot快速构建基于sqlite数据源的应用,希望对大家有所帮助