最近有个需求,是手机软件离线使用,
用的springboot + mybatis-plus + mysql,无法实现,于是考虑使用内嵌式轻量级的数据库SQLlite
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.3</version>
</dependency>
server:
port: 9090
spring:
datasource:
url: jdbc:sqlite:sqlite.db
driver-class-name: org.sqlite.JDBC
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: auto
type-aliases-package: com.example.sqlite.entity
此处的url折磨了一晚上,网上各种百度加resource目录下什么的,最后直接这样,启动后会在根目录下自动生成一个db文件。
1). 建表语句
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
`id` INTEGER NOT NULL,
`username` TEXT,
`password` TEXT,
PRIMARY KEY (`id`)
)
sqllite的数据类型和mysql不同,但同时也包容(不确定)
简单来说
整数用 INTEGER
字符串用 TEXT
浮点数 REAL
具体可参考该链接: SQLLite基本数据类型
2). 铺地数据
INSERT INTO `user` VALUES (1, '范大', '11111');
3). 添加Sqllite
建议方框内的值直接填写生成的db名即可,同时别忘记点击一个test Connection测试一下。
4). 执行sql
双击该数据库,
选择之前的sql执行,可以看日志
两个sql都run一下
OK, 我这边都成功了
package com.example.sqlite.controller;
import com.example.sqlite.entity.User;
import com.example.sqlite.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
@GetMapping("/get/{id}")
public User findOne(@PathVariable Integer id) {
return userService.getById(id);
}
@GetMapping("/save/{id}")
public void save(@PathVariable Integer id) {
User user = new User();
user.setId(id);
user.setUsername(id+":哈哈哈哈");
userService.save(user);
}
}
service
package com.example.sqlite.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.sqlite.entity.User;
public interface UserService extends IService<User> {
}
serviceImpl
package com.example.sqlite.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sqlite.entity.User;
import com.example.sqlite.mapper.UserMapper;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
}
Mapper
package com.example.sqlite.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sqlite.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
Entity
package com.example.sqlite.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String username;
private String password;
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.sqlite.mapper.UserMapper">
</mapper>
MybatisPlusConfig
package com.example.sqlite.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//注入配置springboot
@Configuration
@MapperScan("com.example.sqlite.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQLITE));
return interceptor;
}
}