Springboot整合Sqlite3

整合步骤如下:

1、新建springboot工程,选择web工程。

2、添加依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.7</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.28.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>

3、创建一个sqlite数据库,保存在工程更目录下

image-20210420213251490

创建user表

CREATE TABLE "main"."Untitled" (
  "id" INTEGER NOT NULL,
  "name" TEXT,
  "age" text,
  PRIMARY KEY ("id")
);

4、修改application.yml

server:
  port: 8089
spring:
  datasource:
    url: jdbc:p6spy:sqlite:D:\Tools\sqlite\db\test.db
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    username:
    password:

logging:
  level: debug
  level.com.dalianpai: debug
  file:
    path: logs:/

5、写代码

/**
 * @author WGR
 * @create 2021/4/20 -- 21:06
 */
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/uinsert")
    public String insert(){
        User user=new User();
        user.setName("abc");
        user.setAge("50");
        int result=userMapper.insert(user);
        System.out.println(result);
        return "插入结果:"+result;
    }
}

/**
 * @author WGR
 * @create 2021/4/20 -- 21:03
 */
public interface UserMapper extends BaseMapper<User> {
}


@SpringBootApplication
@MapperScan("com.dalianpai.sqlite3.dao")
public class Sqlite3Application {

    public static void main(String[] args) {
        SpringApplication.run(Sqlite3Application.class, args);
    }

}

p6spy配置文件

#3.2.1以上使用
#modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#3.2.1以下使用或者不配置
#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2

6、运行一下看看

http://localhost:8089/uinsert

image-20210420213524163

posted @ 2021-04-20 21:36  天宇轩-王  阅读(5651)  评论(3编辑  收藏  举报