• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Sqlite安装使用和springboot整合

武飞扬头像
Fran~
帮助1

SQLite介绍

什么是SQLite

SQLite是一款非常轻量级的关系数据库系统,支持多数SQL92标准。它不需要单独安装,类似于一个文本文件,又可以放置在项目内作为持久化数据的内嵌式数据库使用。使用SQLite一般只需要带上一个dll,就可以使用它的全部功能。

SQLite应用场景

存储手机App应用、小型的Web项目或者桌面应用的持久化数据

SQLite安装

Windows安装SQLite

1.官网下载地址: https://www.sqlite.org/download.html

2.下载如下两个压缩文件(根据系统不同下载文件略有差别)

学新通

 3.创建文件夹 D:\sqlite,并在此文件夹下解压上面两个压缩文件,将得到以下文件

学新通

4.将sqlite安装地址配置为环境变量:我的电脑右击->属性->高级系统设置->高级->环境变量->Path->编辑->新建->[你的安装目录]->ok,新建完毕后任一地址执行sqlite命令可进入命令行

Linux安装SQLite

1.使用下面的命令来检查您的机器上是否已经安装了 SQLite

  1.  
     
  2.  
    user@naviproc1:~$ sqlite
  3.  
     
  4.  
    Command 'sqlite' not found, but can be installed with:
  5.  
     
  6.  
    sudo apt install sqlite
  7.  
     

2.如未安装则使用命令安装,安装完毕进入敲sqlite3命令进入sqlite命令行

  1.  
    user@naviproc1:~$ sudo apt install sqlite3
  2.  
    user@naviproc1:~$ sqlite3
  3.  
    SQLite version 3.31.1 2020-01-27 19:55:54
  4.  
    Enter ".help" for usage hints.
  5.  
    Connected to a transient in-memory database.
  6.  
    Use ".open FILENAME" to reopen on a persistent database.
  7.  
    sqlite>

3.使用【.quit】或【.exit】命令退出sqlite3控制界面

数据库表创建测试

1.在数据库文件存放地址新建test.db(test为数据库名),例如在d:/project/db/新建test.db文件

学新通

2.打开数据库文件,打开的方式有两种:

第一种:先进入sqlite3再打开文件

sqlite> .open d:/project/db/test.db

第二种:直接打开文件

C:\Windows\system32>sqlite3 d:/project/db/test.db

3.执行建表语句,并插入部分测试数据

  1.  
    DROP TABLE IF EXISTS "user";
  2.  
    CREATE TABLE "user" (
  3.  
    "uid" text(36) NOT NULL,
  4.  
    "name" TEXT,
  5.  
    "sex" integer,
  6.  
    "email" TEXT,
  7.  
    PRIMARY KEY ("uid")
  8.  
    );

4.数据插入完毕后可执行查询语句

  1.  
    sqlite> select * from user;
  2.  
    700|jQsuV|1|
  3.  
    74|zgIvN|1|
  4.  
    648|fLhFT|1|
  5.  
    561|AycmK|1|
  6.  
    436|TnPxs|1|

5.结果显示正常则SQLite安装完成

SpringBoot 整合 Sqlite3

1.pom.xml引入所需jar包

  1.  
    <dependency>
  2.  
    <groupId>p6spy</groupId>
  3.  
    <artifactId>p6spy</artifactId>
  4.  
    <version>3.8.7</version>
  5.  
    </dependency>
  6.  
    <dependency>
  7.  
    <groupId>org.projectlombok</groupId>
  8.  
    <artifactId>lombok</artifactId>
  9.  
    <optional>true</optional>
  10.  
    </dependency>
  11.  
    <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
  12.  
    <dependency>
  13.  
    <groupId>org.xerial</groupId>
  14.  
    <artifactId>sqlite-jdbc</artifactId>
  15.  
    <version>3.28.0</version>
  16.  
    </dependency>
  17.  
    <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
  18.  
    <dependency>
  19.  
    <groupId>com.baomidou</groupId>
  20.  
    <artifactId>mybatis-plus-boot-starter</artifactId>
  21.  
    <version>3.2.0</version>
  22.  
    </dependency>
  23.  
     
  24.  
    <!--mybatis plus-->
  25.  
    <dependency>
  26.  
    <groupId>com.baomidou</groupId>
  27.  
    <artifactId>mybatis-plus-generator</artifactId>
  28.  
    <version>3.2.0</version>
  29.  
    </dependency>
学新通

2.application.yml配置数据库连接

  1.  
    server:
  2.  
    port: 8089
  3.  
    spring:
  4.  
    datasource:
  5.  
    url: jdbc:p6spy:sqlite:D:\project\db\test.db
  6.  
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
  7.  
    username:
  8.  
    password:

3.数据库表和实体类的对应;

  1.  
    import com.baomidou.mybatisplus.annotation.TableField;
  2.  
    import com.baomidou.mybatisplus.annotation.TableId;
  3.  
    import com.baomidou.mybatisplus.annotation.TableName;
  4.  
    import lombok.Data;
  5.  
     
  6.  
    @Data
  7.  
    @TableName
  8.  
    public class User {
  9.  
    @TableId
  10.  
    private String uid;
  11.  
    private String name;
  12.  
    private Integer sex;
  13.  
    private String email;
  14.  
    /*
  15.  
    * 数据库不存在的字段
  16.  
    * */
  17.  
    @TableField(exist = false)
  18.  
    private String nickName;
  19.  
    }
学新通

4.mapper接口,继承BaseMapper即可

  1.  
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  2.  
    import com.demo.sqlite.pojo.User;
  3.  
    import org.apache.ibatis.annotations.Mapper;
  4.  
     
  5.  
    @Mapper
  6.  
    public interface UserMapper extends BaseMapper<User> {
  7.  
    }

5.在启动类上加上扫描mapper文件夹的注解,以防找不到mapper映射

  1.  
    import org.mybatis.spring.annotation.MapperScan;
  2.  
    import org.springframework.boot.SpringApplication;
  3.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  4.  
    @MapperScan("com.demo.sqlite.mapper")
  5.  
    @SpringBootApplication
  6.  
    public class SqliteApplication {
  7.  
     
  8.  
    public static void main(String[] args) {
  9.  
    SpringApplication.run(SqliteApplication.class, args);
  10.  
    }
  11.  
     
  12.  
    }

6.接口访问实现基本功能

  1.  
    package com.demo.sqlite.controller;
  2.  
     
  3.  
    import java.util.List;
  4.  
    import java.util.Map;
  5.  
    import java.util.Random;
  6.  
     
  7.  
    import com.demo.sqlite.mapper.UserMapper;
  8.  
    import com.demo.sqlite.pojo.User;
  9.  
    import org.springframework.beans.factory.annotation.Autowired;
  10.  
    import org.springframework.web.bind.annotation.GetMapping;
  11.  
    import org.springframework.web.bind.annotation.RequestMapping;
  12.  
    import org.springframework.web.bind.annotation.RestController;
  13.  
     
  14.  
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  15.  
     
  16.  
    import javax.annotation.Resource;
  17.  
     
  18.  
     
  19.  
    @RequestMapping("/index")
  20.  
    @RestController
  21.  
    public class IndexController {
  22.  
     
  23.  
    @Resource
  24.  
    private UserMapper userMapper;
  25.  
     
  26.  
    /**
  27.  
    * 获取所有数据
  28.  
    * @return
  29.  
    */
  30.  
    @GetMapping
  31.  
    public Object index(){
  32.  
    return userMapper.selectList(null);
  33.  
    }
  34.  
     
  35.  
    /**
  36.  
    * 新增
  37.  
    * @return
  38.  
    */
  39.  
    @GetMapping("/insert")
  40.  
    public Object insert(){
  41.  
    User user = new User();
  42.  
    user.setName(randomString());
  43.  
    Random random = new Random();
  44.  
    user.setUid("" random.nextInt(1000));
  45.  
    user.setSex(1);
  46.  
    return userMapper.insert(user);
  47.  
    }
  48.  
     
  49.  
    /**
  50.  
    * 获取总数
  51.  
    * @return
  52.  
    */
  53.  
    @GetMapping("/count")
  54.  
    public Object index1(){
  55.  
    Integer userCount = userMapper.selectCount(null);
  56.  
    return userCount;
  57.  
    }
  58.  
     
  59.  
    /**
  60.  
    * 获取一个list集合
  61.  
    * 排序 orderByAsc("order_id")
  62.  
    * 不为null的判断 isNotNull("stove_code")
  63.  
    * 分组 groupBy("product_model")
  64.  
    * @return
  65.  
    */
  66.  
    @GetMapping("/male")
  67.  
    public Object index2(){
  68.  
    List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("sex", 1));
  69.  
    return users;
  70.  
    }
  71.  
     
  72.  
    /**
  73.  
    * 获取一个map集合
  74.  
    * @return
  75.  
    */
  76.  
    @GetMapping("/map")
  77.  
    public Object index3(){
  78.  
    List<Map<String,Object>> users = userMapper.selectMaps(new QueryWrapper<User>()
  79.  
    .select("uid,name")
  80.  
    .eq("sex", 1));
  81.  
    return users;
  82.  
    }
  83.  
     
  84.  
    /**
  85.  
    * 随机生成字母
  86.  
    * @return
  87.  
    */
  88.  
    public String randomString(){
  89.  
    StringBuilder s = new StringBuilder(5);
  90.  
    Random random = new Random();
  91.  
    for( int i = 0; i < 5; i ) {
  92.  
    int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写还是小写
  93.  
    s.append((char)(choice random.nextInt(26)));
  94.  
    }
  95.  
    return s.toString();
  96.  
    }
  97.  
    }
学新通

参考链接:

https://www.jianshu.com/p/8462e5fa34c3

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhekfj
系列文章
更多 icon
同类精品
更多 icon
继续加载