SpringBoot3 整合 SQLite3 + MybatisPuls

!! 前置要求 !!

  1. 假设你已经掌握 SpringBoot3、Maven、Mybaits、MybatisPuls。
  2. 假设你已经新建好SQLite3测试库。如果没有可以按此结构新建,保存到任意位置。
    image

1. 项目结构

image

2. 依赖

注意SpringBoot3版本这边用最新的打包失败,所以改用3.1.2

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>gateway</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

        <!-- sqlite -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.36.0.3</version>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3. 配置

datasource.url改为你自己的路径,发布的时候建议用绝对路径,不要以资源的方式导入,不然重启应用会导致数据库没有更新上去。

application.yml

# Web
server:
  port: 80

# Sqlite
spring:
  datasource:
    url: jdbc:sqlite:C:\\Users\\xxing\\Desktop\\ayiot.db # 改为数据库路径
    driver-class-name: org.sqlite.JDBC

# MyBatis-Plus
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 开启SQL日志打印

4. 编写是实体类

注意需要跟表对应。

image

package com.ayiot.gateway.entity;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    Integer id;

    @TableField("username")
    String username;

    @TableField("password")
    String password;
}

5. 编写Mapper

继承mp给我们提供的Mapper

package com.ayiot.gateway.mapper;

import com.ayiot.gateway.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    
}

6. 测试

新建一个测试类

package com.ayiot.gateway;

import com.ayiot.gateway.entity.User;
import com.ayiot.gateway.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class GatewayWeb {

    @Autowired
    UserMapper userMapper;

    @Test
    public void test(){
        User user = userMapper.selectById(1);
        System.out.println(user);
    }
}

image

posted @ 2024-02-01 20:49  xXinG9199  阅读(1148)  评论(0编辑  收藏  举报