MyBatis-Flex
官网:官网地址
快速上手:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.12</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>mybatis-flex-test</name><description>mybatis-flex-test</description><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.mybatis-flex</groupId><artifactId>mybatis-flex-spring-boot-starter</artifactId><version>1.7.5</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></dependency><dependency><groupId>com.mybatis-flex</groupId><artifactId>mybatis-flex-processor</artifactId><version>1.7.5</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.example.demo.DemoApplication</mainClass><skip>true</skip></configuration></plugin></plugins></build></project>
创建实体类 User
package com.example.demo.entity;import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;import java.util.Date;@Table("user")
public class User {@Id(keyType = KeyType.Auto)private Integer id;private String nickname;private String username;private String account;private String password;private String phone;private Integer sex;private String avatar;private String wechat;private Integer userType;private Integer integral;private Integer vip;private String reserve;private Integer sort;private Integer auth;private Integer status;private Boolean del;private Date createTime;private Date updateTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex = sex;}public String getAvatar() {return avatar;}public void setAvatar(String avatar) {this.avatar = avatar;}public String getWechat() {return wechat;}public void setWechat(String wechat) {this.wechat = wechat;}public Integer getUserType() {return userType;}public void setUserType(Integer userType) {this.userType = userType;}public Integer getIntegral() {return integral;}public void setIntegral(Integer integral) {this.integral = integral;}public Integer getVip() {return vip;}public void setVip(Integer vip) {this.vip = vip;}public String getReserve() {return reserve;}public void setReserve(String reserve) {this.reserve = reserve;}public Integer getSort() {return sort;}public void setSort(Integer sort) {this.sort = sort;}public Integer getAuth() {return auth;}public void setAuth(Integer auth) {this.auth = auth;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}public Boolean getDel() {return del;}public void setDel(Boolean del) {this.del = del;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getUpdateTime() {return updateTime;}public void setUpdateTime(Date updateTime) {this.updateTime = updateTime;}@Overridepublic String toString() {return "User{" +"id=" + id +", nickname='" + nickname + '\'' +", username='" + username + '\'' +", account='" + account + '\'' +", password='" + password + '\'' +", phone='" + phone + '\'' +", sex=" + sex +", avatar='" + avatar + '\'' +", wechat='" + wechat + '\'' +", userType=" + userType +", integral=" + integral +", vip=" + vip +", reserve='" + reserve + '\'' +", sort=" + sort +", auth=" + auth +", status=" + status +", del=" + del +", createTime=" + createTime +", updateTime=" + updateTime +'}';}
}
注意:可以使用Lombok 插件 但是可能会不兼容 导致项目无法正常启动 同时springboot版本不能太高2.7.x就可以了
创建Mapper接口
package com.example.demo.mapper;import com.example.demo.entity.User;
import com.mybatisflex.core.BaseMapper;public interface UserMapper extends BaseMapper<User> {
}
测试SQL脚本
/*Navicat Premium Data TransferSource Server : localhost_3306Source Server Type : MySQLSource Server Version : 50737Source Host : localhost:3306Source Schema : db_testTarget Server Type : MySQLTarget Server Version : 50737File Encoding : 65001Date: 21/12/2023 12:57:00
*/SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`nickname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '昵称',`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '真实用户名(身份证上的用户名)',`account` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '登录账号',`password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '密码',`phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '手机号',`sex` int(1) NULL DEFAULT 0 COMMENT '性别(0保密,1女,2男)',`avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '头像',`wechat` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '微信号',`user_type` int(1) NULL DEFAULT NULL COMMENT '用户类型(0超级管理员,1管理员,2系统用户)',`integral` int(11) NOT NULL DEFAULT 0 COMMENT '用户积分',`vip` int(1) NULL DEFAULT 3 COMMENT 'vip等级',`reserve` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备用字段',`sort` int(1) NULL DEFAULT 1 COMMENT '排序 数字越小越靠前',`auth` int(1) NOT NULL DEFAULT 0 COMMENT '用户是否认证(1已认证,0未认证)',`status` int(1) NOT NULL DEFAULT 0 COMMENT ' 用户状态(1,可用,0禁用)',`del` bigint(1) NULL DEFAULT NULL COMMENT '逻辑删除(1 删除 ,0 未删除)',`create_time` datetime NOT NULL COMMENT '创建时间',`update_time` datetime NOT NULL COMMENT '更新时间',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 294 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;SET FOREIGN_KEY_CHECKS = 1;
测试类
package com.example.demo;import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static com.example.demo.entity.table.UserTableDef.USER;@SpringBootTest
class DemoApplicationTests {@Autowiredprivate UserMapper userMapper;@Testvoid testFlex() {QueryWrapper queryWrapper = QueryWrapper.create().select().where(USER.ID.between(1,30));List<User> users = userMapper.selectListByQuery(queryWrapper);users.forEach(System.out::println);}
}
测试效果
至此完成项目搭建,其他细节看官方文档