SpringBoot整合Mybatis-flex

发布时间:2024年01月16日

📑前言

本文主要是【Mybatis-flex】——SpringBoot整合Mybatis-flex的文章,如果有什么需要改进的地方还请大佬指出??

🎬作者简介:大家好,我是听风与他🥇
??博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见


这里介绍一款Mybatis增强框架,号称集成了MybatisPlus等多个框架的优点,那就是Mybatis-Flex。

1.Mybatis-flex是什么

与Mybatis Plus类似,Mybatis Flex也是基于Mybatis的一个增强的ORM框架。但是相比之前更轻量,更灵活,性能更高。

在这里插入图片描述

2.快速使用

1.添加pom.xml依赖

        <dependency>
            <groupId>com.mybatis-flex</groupId>
            <artifactId>mybatis-flex-spring-boot3-starter</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>com.mybatis-flex</groupId>
            <artifactId>mybatis-flex-processor</artifactId>
            <version>1.7.7</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>

2.配置文件配置(application.yml)

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/lianxi
    username: root
    password: XXXX
server:
  port: 8080

3.实体类User

package com.it.mybatis_flex_test.entity;

import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
@Table("user")
public class User {
	@Id(keyType = KeyType.Auto)
	int id;
	String name;
	int age;
	String password;
}

4.接口类Mapper

package com.it.mybatis_flex_test.mapper;

import com.it.mybatis_flex_test.entity.User;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper extends BaseMapper<User> {
	@Select("select * from user where id = #{id}")
	User selectById(@Param("id") int id);
}

5.测试类

package com.it.mybatis_flex_test;

import com.it.mybatis_flex_test.entity.User;
import com.it.mybatis_flex_test.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;

@SpringBootTest
class MybatisFlexTestApplicationTests {

	@Autowired
	private UserMapper mapper;

	@Test
	void contextLoads() {
		//查询全部
		List<User> users1 = mapper.selectAll();
		users1.forEach(System.out::println);
		//修改
		User user = new User(37,"ppx",20,"12345");
		mapper.update(user);
		//添加
		User user1 = new User(0,"ppx",18,"12345");
		mapper.insert(user1);
		//删除
		mapper.deleteById(36);
		//条件查询
//		Map<String,Object> condition = Map.of("id",37);
//		List<User> users = mapper.selectListByMap(condition);
//		System.out.println(users);
		//根据查询条件查询数据
		QueryWrapper wrapper = QueryWrapper.create()
				.from(User.class)
				.where(User::getAge).eq(18);
		List<User> users = mapper.selectListByQuery(wrapper);
		users.forEach(System.out::println);
	}

}

3.项目开源地址

https://gitee.com/zhang-zilong_zzl/Mybatis_flex_Test

📑文章末尾

在这里插入图片描述

文章来源:https://blog.csdn.net/weixin_61494821/article/details/135628994
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。