Springboot + Sqlite3 + MybatisPlus简单集成

sqlite3真的很方便,随手写个小应用太方便了。本着极简(懒)的态度,做一个最简集成。

window环境
首先sqlite3 去官网下一个预编译的版本的,再下一个工具包(exe执行文件),然后

sqlite3 test.db

创建一个数据库。
建表

create table user
(
  id   INTEGER not null
    primary key
  autoincrement,
  name varchar(20)
);

这里表名取成了user,因为懒。

maven pom.xml:



    4.0.0

    cn.jesseyang
    sqlite-demo
    0.0.1-SNAPSHOT
    jar

    sqlite-demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.1.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.xerial
            sqlite-jdbc
            3.23.1
        

        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.6
        

    






application.yml

这里的::resource 的意思是相对路径。绝对路径可以不要这个resource.这里的db文件夹我建在了resources目录下。

spring:
  datasource:
    url: jdbc:sqlite::resource:db/test.db
    driver-class-name: org.sqlite.JDBC
    username:
    password:

dommain

用了lombok 省了getter setter

@Data
public class User {
    private String name;
}

usermapper

感谢mybatisplus就是这么简单

@Mapper
public interface UserMapper extends BaseMapper {
}

试一把

直接写在了Application类里面测试,妥妥的

@SpringBootApplication
@RestController
public class SqliteDemoApplication {

    @Autowired
    UserMapper userMapper;

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



    @RequestMapping("/insert")
    public Object insert(String name){
        User user = new User();
        user.setName(name);
        userMapper.insert(user);
        return user;
    }

    @RequestMapping("/show")
    public Object show(){

        return userMapper.selectList(null);
    }
}

地址:
https://gitee.com/jesseyang/springboot-sqlite3-mybatisplus-demo

你可能感兴趣的:(Springboot + Sqlite3 + MybatisPlus简单集成)