Springboot內(nèi)嵌SQLite配置使用詳解
版本號
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)文章
解決報(bào)錯:java:讀取jar包時(shí)出錯:error in opening zip 
文章總結(jié):解決Java讀取jar包時(shí)出錯的問題,通過下載源碼并刷新項(xiàng)目解決了問題,希望對大家有所幫助2024-11-11
JavaWeb項(xiàng)目web.xml中出現(xiàn)Element xxx is not al
這篇文章主要介紹了JavaWeb項(xiàng)目web.xml中出現(xiàn)Element xxx is not allowed here問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
IDEA項(xiàng)目的依賴(pom.xml文件)導(dǎo)入問題及解決
這篇文章主要介紹了IDEA項(xiàng)目的依賴(pom.xml文件)導(dǎo)入問題及解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
用Maven打成可執(zhí)行jar,包含maven依賴,本地依賴的操作
這篇文章主要介紹了用Maven打成可執(zhí)行jar,包含maven依賴,本地依賴的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
教你怎么用SpringBoot+Mybati-Plus快速搭建代碼
Mybatis自身通過了逆向工程來幫助我們快速生成代碼,但Mybatis-plus卻更加強(qiáng)大,不僅僅可以生成dao,pojo,mapper,還有基本的controller和service層代碼,接下來我們來寫一個簡單的人門案例是看看如何mybatis-plus是怎么實(shí)現(xiàn)的,需要的朋友可以參考下2021-06-06
詳解MyBatis?ResultSetHandler?結(jié)果集的解析過程
這篇文章主要為大家介紹了MyBatis?ResultSetHandler?結(jié)果集的解析過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

