欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應(yīng)用

 更新時間:2017年08月14日 11:34:14   作者:hwding  
為了提供一個單包易部署的服務(wù)器應(yīng)用,考慮使用Spring Boot,因?yàn)槠浼闪薃pache Tomcat,易于運(yùn)行,免去絕大部分了服務(wù)器配置的步驟

為了提供一個單包易部署的服務(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)圖片的三種方法

    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-10
  • Spring Boot jar中沒有主清單屬性的解決方法

    Spring Boot jar中沒有主清單屬性的解決方法

    這篇文章主要介紹了Spring Boot jar中沒有主清單屬性的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Go?Java算法之交錯字符串示例詳解

    Go?Java算法之交錯字符串示例詳解

    這篇文章主要為大家介紹了Go?Java算法之交錯字符串示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • java中的日期時間類Date和SimpleDateFormat

    java中的日期時間類Date和SimpleDateFormat

    這篇文章主要介紹了java中的日期時間類Date和SimpleDateFormat,Date類的對象在Java中代表的是當(dāng)前所在系統(tǒng)的此刻日期時間,說白了就是你計算機(jī)上現(xiàn)實(shí)的時間,需要的朋友可以參考下
    2023-09-09
  • Java實(shí)現(xiàn)將彩色PDF轉(zhuǎn)為灰度PDF的示例代碼

    Java實(shí)現(xiàn)將彩色PDF轉(zhuǎn)為灰度PDF的示例代碼

    本文以Java代碼為例介紹如何實(shí)現(xiàn)將彩色PDF文件轉(zhuǎn)為灰度(黑白)的PDF文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧
    2022-03-03
  • idea編寫yml、yaml文件以及其優(yōu)先級的使用

    idea編寫yml、yaml文件以及其優(yōu)先級的使用

    本文主要介紹了idea編寫yml、yaml文件以及其優(yōu)先級的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • springboot多文件或者文件夾壓縮成zip的方法

    springboot多文件或者文件夾壓縮成zip的方法

    最近碰到個需要下載zip壓縮包的需求,于是我在網(wǎng)上找了下別人寫好的zip工具類,下面通過本文給大家分享springboot多文件或者文件夾壓縮成zip的方法,感興趣的朋友一起看看吧
    2024-07-07
  • 使用Files.walkFileTree遍歷目錄文件

    使用Files.walkFileTree遍歷目錄文件

    這篇文章主要介紹了使用Files.walkFileTree遍歷目錄文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Spring Boot 防止接口惡意刷新和暴力請求的實(shí)現(xiàn)

    Spring Boot 防止接口惡意刷新和暴力請求的實(shí)現(xiàn)

    本文主要介紹了Spring Boot 防止接口惡意刷新和暴力請求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Java內(nèi)存模型的深入講解

    Java內(nèi)存模型的深入講解

    這篇文章主要給大家介紹了關(guān)于Java內(nèi)存模型的相關(guān)資料,我們常說的JVM內(nèi)存模式指的是JVM的內(nèi)存分區(qū),而Java內(nèi)存模式是一種虛擬機(jī)規(guī)范,需要的朋友可以參考下
    2021-07-07

最新評論