背景:
mybatis-plus 出现那么久,多表查询这块一直没有进展, mybatis-flex它出现了
总结:mybatis-flex在链式调用没有mybatis-plus做得好,mp是key-value形式入参,mf分开了显得代码冗余,mf好在支持联表查询,还有数度这块官方5-10倍有虚夸成分,用的都是同样得技术,怎么会有这样大得差距,就是在忽悠人
如果想用用mybatis-plus 又想要链表查询,可以使用mybatis-plus-join-boot-starter
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
1、很轻量
MyBatis-Flex 整个框架只依赖 MyBatis,再无其他任何第三方依赖。
2、只增强
MyBatis-Flex 支持 CRUD、分页查询、多表查询、批量操作,但不丢失 MyBatis 原有的任何功能。
3、高性能
MyBatis-Flex 采用独特的技术架构、相比同类框架(比如 MyBatis-Plus),MyBatis-Flex 的在增删改查等方面的性能均超越其 5~10 倍或以上。
maven
javascript"><?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.13</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.ldj</groupId><artifactId>mybatis-flex</artifactId><version>0.0.1-SNAPSHOT</version><description>mybatis-flex-test</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--必须的3个--><dependency><groupId>com.mybatis-flex</groupId><artifactId>mybatis-flex-spring-boot-starter</artifactId><version>1.10.1</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><!--必须的3个--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.72</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
applicatiion.properties
javascript">spring.application.name=mybatis-flexspring.datasource.url=jdbc:mysql://192.168.208.200:3306/demo
spring.datasource.username=root
spring.datasource.password=rootlogging.level.com.ldj=debug
java">package com.ldj.mybatisflex.controller;import com.ldj.mybatisflex.model.dto.UserInfoReqDTO;
import com.ldj.mybatisflex.model.vo.UserInfoVO;
import com.ldj.mybatisflex.service.UserInfoService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;/*** User: ldj* Date: 2024/11/17* Time: 16:55* Description: No Description*/
@RestController
@RequestMapping("/userInfo")
@CrossOrigin(maxAge = 3600)
public class UserInfoController {@Autowiredprivate UserInfoService userInfoService;@PostMapping(value = "/selectOne")public UserInfoVO queryUserInfo(@RequestBody UserInfoReqDTO userInfoReqDTO){return userInfoService.queryUserInfo(userInfoReqDTO);}@PostMapping(value = "/update")public Boolean updateUserInfo(@RequestBody UserInfoReqDTO userInfoReqDTO){return userInfoService.updateUserInfo(userInfoReqDTO);}
}
java">package com.ldj.mybatisflex;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.ldj.mybatisflex.mapper")
public class MybatisFlexApplication {public static void main(String[] args) {SpringApplication.run(MybatisFlexApplication.class, args);}}
DAO
java">package com.ldj.mybatisflex.model.dao;import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;import java.io.Serializable;
import java.math.BigDecimal;/*** User: ldj* Date: 2024/11/17* Time: 16:57* Description: No Description*/
@Getter
@Setter
@Accessors(chain = true)
@Table("tb_user_info")
public class UerInfoDAO implements Serializable {private static final long serialVersionUID = 7032308989090502865L;@Id(keyType = KeyType.Auto)private Long id;private String username;private String password;private Integer age;private String sex;private String addr;private Integer married;private BigDecimal salary;
}
Mapper
java">package com.ldj.mybatisflex.mapper;import com.ldj.mybatisflex.model.dao.UerInfoDAO;
import com.mybatisflex.core.BaseMapper;/*** User: ldj* Date: 2024/11/17* Time: 17:28* Description: No Description*/
public interface UserInfoMapper extends BaseMapper<UerInfoDAO> {
}
Service
java">package com.ldj.mybatisflex.service;import com.ldj.mybatisflex.model.dao.UerInfoDAO;
import com.ldj.mybatisflex.model.dto.UserInfoReqDTO;
import com.ldj.mybatisflex.model.vo.UserInfoVO;
import com.mybatisflex.core.service.IService;/*** User: ldj* Date: 2024/11/17* Time: 17:12* Description: No Description*/
public interface UserInfoService extends IService<UerInfoDAO> {UserInfoVO queryUserInfo(UserInfoReqDTO userInfoReqDTO);
}-----------------
package com.ldj.mybatisflex.service.impl;import com.alibaba.fastjson.JSON;import com.ldj.mybatisflex.mapper.UserInfoMapper;
import com.ldj.mybatisflex.model.dao.UerInfoDAO;
import com.ldj.mybatisflex.model.dto.UserInfoReqDTO;
import com.ldj.mybatisflex.model.vo.UserInfoVO;
import com.ldj.mybatisflex.service.UserInfoService;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** User: ldj* Date: 2024/11/17* Time: 17:17* Description: No Description*/
@Slf4j
@Service
public class UserInfoServiceImpl implements UserInfoService {@Autowiredprivate UserInfoMapper userInfoMapper;@Overridepublic BaseMapper<UerInfoDAO> getMapper() {return userInfoMapper;}@Overridepublic UserInfoVO queryUserInfo(UserInfoReqDTO userInfoReqDTO) {// 第一种写法UerInfoDAO uerInfoDAO1 = queryChain().select(UerInfoDAO::getUsername, UerInfoDAO::getSex, UerInfoDAO::getSalary).from("tb_user_info").where(UerInfoDAO::getUsername).eq(userInfoReqDTO.getUsername()).one();// 第二种写法QueryWrapper queryWrapper = queryChain().toQueryWrapper().select(UerInfoDAO::getUsername, UerInfoDAO::getSex, UerInfoDAO::getSalary).and(UerInfoDAO::getUsername).eq(userInfoReqDTO.getUsername());UerInfoDAO uerInfoDAO2 = userInfoMapper.selectOneByQuery(queryWrapper);// 封装VOUserInfoVO userInfoVO = new UserInfoVO();BeanUtils.copyProperties(uerInfoDAO2, userInfoVO);log.info("result:{}", JSON.toJSONString(userInfoVO));return userInfoVO;}@Overridepublic Boolean updateUserInfo(UserInfoReqDTO userInfoReqDTO) {log.info("入参:{}", JSON.toJSONString(userInfoReqDTO));return updateChain().set(UerInfoDAO::getMarried, userInfoReqDTO.getMarried()).where(UerInfoDAO::getUsername).eq(userInfoReqDTO.getUsername()).and(UerInfoDAO::getIsDeleted).eq("N").and(UerInfoDAO::getAge).eq(18).update();}
}
补充:mapper装配