1、创建数据库
2、创建表
DROP TABLE IF EXISTS "tb_user";
CREATE TABLE "tb_user" (
"user_id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"user_name" TEXT,
"user_age" integer DEFAULT 0
);
3、添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</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>
4、配置文件
spring.application.name=server-sqlite-demo
server.port=9090
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.datasource.url=jdbc:sqlite:E:\\opt\\sqlite3\\test.db
spring.datasource.username=
spring.datasource.password=
5、用户实体类
package com.shenmazong.serversqlitedemo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @author 军哥
* @version 1.0
* @description: 用户实体类
* @date 2021/9/1 20:35
*/
@Data
@TableName(value = "tb_user")
public class TbUser {
@TableId(value = "user_id", type = IdType.AUTO)
private Integer userId;
private String userName;
private Integer userAge;
}
6、创建mapper
package com.shenmazong.serversqlitedemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shenmazong.serversqlitedemo.entity.TbUser;
public interface TbUserMapper extends BaseMapper<TbUser> {
}
7、增加扫码注解
package com.shenmazong.serversqlitedemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(value = "com.shenmazong.serversqlitedemo.mapper")
public class ServerSqliteDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ServerSqliteDemoApplication.class, args);
}
}
8、编写测试类
package com.shenmazong.serversqlitedemo;
import com.shenmazong.serversqlitedemo.entity.TbUser;
import com.shenmazong.serversqlitedemo.mapper.TbUserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ServerSqliteDemoApplicationTests {
@Autowired
TbUserMapper tbUserMapper;
@Test
void contextLoads() {
}
@Test
void testNew() {
TbUser tbUser = new TbUser();
tbUser.setUserName("zhaoyun");
tbUser.setUserAge(66);
tbUserMapper.insert(tbUser);
}
}