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

Springboot內(nèi)嵌SQLite配置使用詳解

 更新時間:2023年08月24日 10:31:49   作者:”PANDA  
這篇文章主要介紹了Springboot內(nèi)嵌SQLite配置使用詳解,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

版本號

MacOS Apple M1 | Jdk17 | Maven 3.8.5 | SpringBoot 2.6.9 | SQLite 3.42.0.0

pom.xml

<dependencies>
    <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.42.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.gwenn</groupId>
            <artifactId>sqlite-dialect</artifactId>
            <version>0.1.4</version>
        </dependency>
</dependencies>

基礎(chǔ)配置

application.properties

# data source
spring.datasource.url=jdbc:sqlite:tutorial.db
spring.datasource.driver-class-name=org.sqlite.JDBC
# spring.datasource.journal_mode=WAL
spring.datasource.hikari.maximum-pool-size=1
spring.jpa.properties.hibernate.dialect=org.sqlite.hibernate.dialect.SQLiteDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

Configuration

import java.util.Properties;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
@Configuration
@EnableJpaRepositories(basePackages = "com.dipeak.diengine.backend.dao")
public class DataSourceConfig {
  @Resource private Environment environment;
  @Bean
  public DataSource dataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
    dataSource.setUrl(environment.getProperty("spring.datasource.url"));
    return dataSource;
  }
  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] {"com.dipeak.diengine.backend.model.entity"});
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    em.setJpaProperties(additionalProperties());
    return em;
  }
  final Properties additionalProperties() {
    final Properties hibernateProperties = new Properties();
    if (environment.getProperty("spring.jpa.hibernate.ddl-auto") != null) {
      hibernateProperties.setProperty(
          "hibernate.hbm2ddl.auto", environment.getProperty("spring.jpa.hibernate.ddl-auto"));
    }
    if (environment.getProperty("spring.jpa.properties.hibernate.dialect") != null) {
      hibernateProperties.setProperty(
          "hibernate.dialect", environment.getProperty("spring.jpa.properties.hibernate.dialect"));
    }
    if (environment.getProperty("hibernate.show_sql") != null) {
      hibernateProperties.setProperty(
          "hibernate.show_sql", environment.getProperty("hibernate.show_sql"));
    }
    return hibernateProperties;
  }
}

Entity 定義

@Entity
@Table(name = "users")
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String name;
}

Repository 定義

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
  @Query("select u from User u where u.id <= ?1")
  Page<User> findMore(Long maxId, Pageable pageable);
  @Modifying
  @Transactional
  @Query("update User u set u.name = ?1 where u.id = ?2")
  int updateById(String name, Long id);
}

Unit Test

public class SqliteTest {
    @Resource private UserRepository userRepository;
    @Test
    public void insert() {
        User user = new User();
        user.setId(3l);
        user.setName("wang da fang");
        userRepository.save(user);
    }
}

到此這篇關(guān)于Springboot內(nèi)嵌SQLite配置使用的文章就介紹到這了,更多相關(guān)Springboot內(nèi)嵌SQLite內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決報錯:java:讀取jar包時出錯:error in opening zip file問題

    解決報錯:java:讀取jar包時出錯:error in opening zip 

    文章總結(jié):解決Java讀取jar包時出錯的問題,通過下載源碼并刷新項目解決了問題,希望對大家有所幫助
    2024-11-11
  • JavaWeb項目web.xml中出現(xiàn)Element xxx is not allowed here問題

    JavaWeb項目web.xml中出現(xiàn)Element xxx is not al

    這篇文章主要介紹了JavaWeb項目web.xml中出現(xiàn)Element xxx is not allowed here問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • IDEA項目的依賴(pom.xml文件)導(dǎo)入問題及解決

    IDEA項目的依賴(pom.xml文件)導(dǎo)入問題及解決

    這篇文章主要介紹了IDEA項目的依賴(pom.xml文件)導(dǎo)入問題及解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java實現(xiàn)阿里云短信接口的示例

    Java實現(xiàn)阿里云短信接口的示例

    這篇文章主要介紹了Java實現(xiàn)阿里云短信接口的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • idea的運行按鈕是灰色問題及解決

    idea的運行按鈕是灰色問題及解決

    這篇文章主要介紹了idea的運行按鈕是灰色問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 對Mapper 中幾種update的區(qū)別說明

    對Mapper 中幾種update的區(qū)別說明

    這篇文章主要介紹了對Mapper 中幾種update的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 用Maven打成可執(zhí)行jar,包含maven依賴,本地依賴的操作

    用Maven打成可執(zhí)行jar,包含maven依賴,本地依賴的操作

    這篇文章主要介紹了用Maven打成可執(zhí)行jar,包含maven依賴,本地依賴的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Spring中的MultipartFile詳解

    Spring中的MultipartFile詳解

    這篇文章主要介紹了Spring中的MultipartFile詳解,隨著Spring框架的崛起,使用Spring框架中的MultipartFile來處理文件也是件很方便的事了,今天就為大家?guī)砥饰鯩ultipartFile的神秘面紗,需要的朋友可以參考下
    2024-01-01
  • 教你怎么用SpringBoot+Mybati-Plus快速搭建代碼

    教你怎么用SpringBoot+Mybati-Plus快速搭建代碼

    Mybatis自身通過了逆向工程來幫助我們快速生成代碼,但Mybatis-plus卻更加強大,不僅僅可以生成dao,pojo,mapper,還有基本的controller和service層代碼,接下來我們來寫一個簡單的人門案例是看看如何mybatis-plus是怎么實現(xiàn)的,需要的朋友可以參考下
    2021-06-06
  • 詳解MyBatis?ResultSetHandler?結(jié)果集的解析過程

    詳解MyBatis?ResultSetHandler?結(jié)果集的解析過程

    這篇文章主要為大家介紹了MyBatis?ResultSetHandler?結(jié)果集的解析過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02

最新評論