本文主要是【Mybatis-flex】——SpringBoot整合Mybatis-flex的文章,如果有什么需要改进的地方还请大佬指出??
🎬作者简介:大家好,我是听风与他🥇
??博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
与Mybatis Plus类似,Mybatis Flex也是基于Mybatis的一个增强的ORM框架。但是相比之前更轻量,更灵活,性能更高。
<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>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/lianxi
username: root
password: XXXX
server:
port: 8080
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;
}
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);
}
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);
}
}
https://gitee.com/zhang-zilong_zzl/Mybatis_flex_Test