SpringBoot整合Mybatis-flex

本文介绍了Mybatis-flex,一个基于Mybatis的增强框架,它提供了更灵活、高性能的特性。文章详细步骤包括添加依赖、配置文件设置、实体类和Mapper接口的使用,以及测试类的示例。
摘要由CSDN通过智能技术生成

📑前言

本文主要是【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

📑文章末尾

在这里插入图片描述

  • 56
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以回答这个问题,以下是一个简单的SpringBoot整合mybatis-plus的示例: 1. 首先,在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> ``` 2. 在application.properties文件中添加以下配置: ``` # 数据库配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root # MyBatis-Plus配置 mybatis-plus.mapper-locations=classpath:/mapper/*.xml mybatis-plus.type-aliases-package=com.example.demo.entity ``` 3. 创建实体类和Mapper接口 ``` // 实体类 @Data public class User { private Long id; private String name; private Integer age; } // Mapper接口 @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 4. 编写Service层和Controller层 ``` // Service层 @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } // Controller层 @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getById(id); } @PostMapping("/") public boolean addUser(@RequestBody User user) { return userService.save(user); } } ``` 以上就是一个简单的SpringBoot整合mybatis-plus的示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听风与他

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值