因为使用SpringBoot 3.2.1 出了一些问题,下面改成2.5.14
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.14version>
<relativePath/>
parent>
<groupId>org.damgroupId>
<artifactId>increment-backup-serveartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>increment-backup-servename>
<description>increment-backup-servedescription>
<properties>
<java.version>17java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.xerialgroupId>
<artifactId>sqlite-jdbcartifactId>
<version>3.21.0.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
使用Macbook pro运行项目,会报如下错误
Caused by: java.lang.Exception: No native library is found for os.name=Mac and os.arch=aarch64. path=/org/sqlite/native/Mac/aarch64
解决方案,修改版本
<dependency>
<groupId>org.xerialgroupId>
<artifactId>sqlite-jdbcartifactId>
<version>3.32.3.3version>
dependency>
我的开发机是mac系统,自动了sqlite3,如果你们没有的话,要去安装,可以参考官方文档的快速开始:https://www.sqlite.org/quickstart.html
mac@MacdeMac-Pro ~ % sqlite3
SQLite version 3.39.4 2022-09-07 20:51:41
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
从一个目录进入终端,创建数据库
mac@MacdeMac-Pro sql % sqlite3 increment-backup.db;
SQLite version 3.39.4 2022-09-07 20:51:41
Enter ".help" for usage hints.
查看数据库是否创建成功
sqlite> .databases
main: /Volumes/MacSpan/Projects/increment-backup/sql/DatabaseName.db r/w
创建成功,出现如下文件
创建数据表
sqlite> create table user
...> (
...> id INTEGER not null primary key autoincrement,
...> name varchar(20)
...> );
查看数据表
sqlite> .tables
user
连接成功
连接成功
生成成功
创建如下项目结构,并粘贴所生成的代码过去
注意,mapper.xml的实体类引用要和你项目的一致
因为只是非常简单的案例,这里先不使用service包下的代码
package org.dam.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;
import java.io.Serializable;
/**
*
* @TableName user
*/
@TableName(value ="user")
@Data
public class User implements Serializable {
/**
*
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
*
*/
private String name;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
User other = (User) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", name=").append(name);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
package org.dam.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.dam.entity.User;
/**
* @author mac
* @description 针对表【user】的数据库操作Mapper
* @createDate 2024-01-18 21:12:12
* @Entity generator.entity.User
*/
public interface UserMapper extends BaseMapper<User> {
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.dam.mapper.UserMapper">
<resultMap id="BaseResultMap" type="org.dam.entity.User">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
resultMap>
<sql id="Base_Column_List">
id,name
sql>
mapper>
package org.dam.controller;
import org.dam.entity.User;
import org.dam.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @Author dam
* @create 2024/1/18 20:37
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserMapper userMapper;
/**
* 增添数据
*/
@GetMapping("/insert")
public Object insert(String name) {
User user = new User();
user.setName(name);
return userMapper.insert(user);
}
/**
* 查询数据
*/
@GetMapping("/show")
public Object show() {
return userMapper.selectList(null);
}
/**
* 删除数据
*/
@DeleteMapping("/delete")
public Object delete(Integer id) {
return userMapper.deleteById(id);
}
/**
* 修改数据
*/
@GetMapping("/update")
public Object update(Integer id, String name) {
User user = new User();
user.setId(id);
user.setName(name);
return userMapper.updateById(user);
}
}
注意url要对应sqlite数据库
# Tomcat
server:
port: 8899
#spring
spring:
datasource:
#引用项目中的数据库文件
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite::resource:static/sqlite/increment-backup.db
username:
password:
# 指定静态资源的路径
web:
resources:
static-locations: classpath:/static/
#mybatis:
# mapper-locations: classpath*:mapper/**/*.xml
IncrementBackupServeApplication
package org.dam;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("org.dam.mapper")
public class IncrementBackupServeApplication {
public static void main(String[] args) {
SpringApplication.run(IncrementBackupServeApplication.class, args);
}
}
我这边使用接口测试工具Apifox来进行测试,使用Postman等其他工具也是可以的,不过我强烈推荐Apifox,感觉非常好用
再查一次,修改成功
再查一遍,删除成功