人家已经给你建议啦: https://gitee.com/mr_zhaojin/mybatis-plus-join https://gitee.com/best_handsome/mybatis-plus-join ` import com.mybatis.plus.entity.Score; import com.mybatis.plus.entity.User; import com.mybatis.plus.join.ConditionEnum; import com.mybatis.plus.service.IUserService; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static com.mybatis.plus.service.impl.BaseService.*; @RunWith(SpringRunner.class) @SpringBootTest public class Test { @Autowired private IUserService userService;
@org.junit.Test
public void testMaxScore() {
User user = userService.query(User.class)
.select(User::getName)
.innerJoin(Score.class)
.on(User::getId, Score::getUserId, ConditionEnum.EQ)
.eq(Score::getExamId, "1")
.select(max(Score::getScore), "id")
.groupBy(Score::getExamId)
.one();
System.out.println(user);
}
} 打印日志为:
2021-06-23 18:04:01.634 DEBUG 8260 --- [ main] c.m.p.mapper.UserMapper.selectOneJoin : ==> Preparing: SELECT t_user.name,max(t_score.score) as id FROM t_user INNER JOIN t_score ON t_user.id=t_score.user_id WHERE (t_score.exam_id = ?) GROUP BY t_score.exam_id
2021-06-23 18:04:01.653 DEBUG 8260 --- [ main] c.m.p.mapper.UserMapper.selectOneJoin : ==> Parameters: 1(String)
2021-06-23 18:04:01.669 DEBUG 8260 --- [ main] c.m.p.mapper.UserMapper.selectOneJoin : <== Total: 1
User(id=100, name=老王) `
有这样的功能,想必,会少很多工作量,目前写个接口要翻:dao接口/dao的xml/service这几个文件.繁杂苦不堪言
Comment From: qmdx
我们才苦不堪言,不想干的活都 issue 让我们干 ,伤心
Comment From: nancheung
我们才苦不堪言,不想干的活都 issue 让我们干 ,伤心
嘤嘤嘤
Comment From: Sdky
哈哈,我们之前的公司不允许使用 Mybatis-Plus、通用 Mappper,说因为出问题不好定位是哪里的 SQL,也不好优化,Code Review 时很难发现问题,只能手写 SQL。
Comment From: qmdx
哈哈,我们之前的公司不允许使用 Mybatis-Plus、通用 Mappper,说因为出问题不好定位是哪里的 SQL,也不好优化,Code Review 时很难发现问题,只能手写 SQL。
原生 xml 好定位问题吗? mp 就是自动生成 crud xml 而已有什么区别,按照你的道理说 jdbc 最好了
Comment From: qmdx
https://github.com/baomidou/mybatis-plus/issues/3572
Comment From: mengyongfeng
可以用view的形式来实现