使用Spring Boot快速构建基于SQLite数据源的应用

网友投稿 491 2023-04-20


使用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所需的最小依赖。

xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.github.hwding.example

example

jar

0.0.1

an example

https://github.com/hwding

http://repo.spring.io/milestone/

repo-spring

org.springframework.boot

spring-boot-starter-parent

1.5.6.RELEASE

example

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-data-jpa

com.enigmabridge

hibernate4-sqlite-dialect

0.1.2

org.apache.commons

commons-dbcp2

2.1.1

org.eclipse.persistence

javax.persistence

2.2.0-RC1

org.xerial

sqlite-jdbc

3.20.0

xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.github.hwding.example

example

jar

0.0.1

an example

https://github.com/hwding

http://repo.spring.io/milestone/

repo-spring

org.springframework.boot

spring-boot-starter-parent

1.5.6.RELEASE

example

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-data-jpa

com.enigmabridge

hibernate4-sqlite-dialect

0.1.2

org.apache.commons

commons-dbcp2

2.1.1

org.eclipse.persistence

javax.persistence

2.2.0-RC1

org.xerial

sqlite-jdbc

3.20.0

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",

entityManagerFactoryRhttp://ef = "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 http://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数据源的应用,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Vue+Element使用富文本编辑器的示例代码
下一篇:Java字符编码简介_动力节点Java学院整理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~