赞
踩
1.maven用到的jar包:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>2.1.3.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>com.bcht.its.das.commons</groupId>
- <artifactId>das-commons-traffic</artifactId>
- <version>1.0</version>
- </dependency>
-
- <dependency>
- <groupId>com.bcht.its.das</groupId>
- <artifactId>das-data-send-rabbitmq</artifactId>
- <version>1.0</version>
- </dependency>
-
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.2</version>
- </dependency>
-
- <dependency>
- <groupId>org.xerial</groupId>
- <artifactId>sqlite-jdbc</artifactId>
- <version>3.30.1</version>
- </dependency>
-
- </dependencies>

2.配置文件:
- ## 数据库配置
- spring.datasource.driver-class-name=org.sqlite.JDBC
- spring.datasource.url=jdbc:sqlite:E:/worktool/sqlite-dll-win64-x64-3360000/demo.db
- spring.datasource.username=
- spring.datasource.password=
注意,SQLITE是不需要登录账号的。
3.配置数据源代码
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.sqlite.SQLiteDataSource;
-
- import javax.sql.DataSource;
-
- /**
- * <h3>bcht-its-das</h3>
- * <p></p>
- *
- * @author : ZFX
- * @date : 2021-10-12 11:19
- **/
- @Configuration
- public class MyBatisConfig {
-
- @Value("${spring.datasource.url}")
- private String url;
-
- @Bean(name="dataSource")
- public DataSource dataSource() {
- SQLiteDataSource dataSource = new SQLiteDataSource();
- dataSource.setUrl(url);
- return dataSource;
- }
-
- public SqlSessionFactory sqlSessionFactory() throws Exception {
- SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
- sqlSessionFactoryBean.setDataSource(dataSource());
- return sqlSessionFactoryBean.getObject();
- }
-
- }

- import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
- import org.mybatis.spring.mapper.MapperScannerConfigurer;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- /**
- * <h3>bcht-its-das</h3>
- * <p></p>
- *
- * @author : ZFX
- * @date : 2021-10-12 11:20
- **/
- @Configuration
- public class MyBatisMapperScannerConfig {
- @Bean
- public MapperScannerConfigurer mapperScannerConfigurer() {
- MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
- mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
-
- //com.bcht.its.das.mapper 这个包名是所有的Mapper.java文件所在的路径,该包下面的子包里面的文件同样会扫描到。
- //此包名与具体的应用的名称相关
- mapperScannerConfigurer.setBasePackage("com.bcht.its.das.mapper");
-
- return mapperScannerConfigurer;
- }
-
-
- /**
- * 目的防止驼峰命名规则
- *
- * @return
- */
- @Bean
- public ConfigurationCustomizer configurationCustomizer() {
- return new ConfigurationCustomizer() {
- @Override
- public void customize(org.apache.ibatis.session.Configuration configuration) {
- configuration.setMapUnderscoreToCamelCase(true);
- }
- };
- }
-
- }

- package com.bcht.its.das.mapper;
-
- import com.bcht.its.das.entity.ThirdWeatherEntity;
- import org.apache.ibatis.annotations.Select;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- /**
- * <h3>bcht-its-das</h3>
- * <p></p>
- *
- * @author : ZFX
- * @date : 2021-10-12 11:22
- **/
- @Service
- public interface WeatherMapper {
-
- @Select("SELECT * FROM 表名")
- List<ThirdWeatherEntity> selectAll();
-
- }

- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
- import org.springframework.scheduling.annotation.EnableScheduling;
-
- /**
- * <h3>bcht-its-das</h3>
- * <p>数据抽取</p>
- *
- * @author : ZFX
- * @date : 2021-10-11 10:58
- **/
- @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
- @EnableScheduling
- public class Application {
-
- public static void main(String[] args) {
- SpringApplication.run(Application.class,args);
- }
-
- }

使用的方式就和其他数据源使用MyBatis一样
如果项目构建完成,发现idea启动报错无法建立到数据库的connection连接,大概率是IDEA没有SQLITE的插件,安装下插件即可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。