经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Fluent Mybatis实际开发中的优势对比
来源:jb51  时间:2021/8/4 17:56:03  对本文有异议

之前文章介绍过了Fluent基本框架等,其中有几个重要的方法用到了IQuery和IUpdate对象。 这2个对象是FluentMybatis实现复杂和动态sql的构造类,通过这2个对象fluent mybatis可以不用写具体的xml文件, 直接通过java api可以构造出比较复杂的业务sql语句,做到代码逻辑和sql逻辑的合一。下面接着介绍如何通过IQuery和IUpdate定义强大的动态SQL语句。

表结构 假如有学生成绩表结构如下:

  1. create table `student_score`
  2. (
  3. id bigint auto_increment comment '主键ID' primary key,
  4. student_id bigint not null comment '学号',
  5. gender_man tinyint default 0 not null comment '性别, 0:女; 1:男',
  6. school_term int null comment '学期',
  7. subject varchar(30) null comment '学科',
  8. score int null comment '成绩',
  9. gmt_create datetime not null comment '记录创建时间',
  10. gmt_modified datetime not null comment '记录最后修改时间',
  11. is_deleted tinyint default 0 not null comment '逻辑删除标识'
  12. ) engine = InnoDB default charset=utf8;

统计2000年到2019年, 三门学科(‘英语', ‘数学', ‘语文')分数按学期,学科统计最低分,最高分和平均分,统计结果按学期和学科排序
SQL:

  1. select school_term, subject, count(score), min(score), max(score), avg(score)
  2. from student_score
  3. where school_term between 2000 and 2019
  4. and subject in ('英语', '数学', '语文')
  5. and is_deleted = 0
  6. group by school_term, subject
  7. order by school_term, subject
  1. 通过FluentMybatis来具体实现
  2. 在StudentScoreDao类上定义接口
  1. @Data
  2. public class ScoreStatistics {
  3. private int schoolTerm;
  4. private String subject;
  5. private long count;
  6. private Integer minScore;
  7. private Integer maxScore;
  8. private BigDecimal avgScore;
  9. }
  1. public interface StudentScoreDao extends IBaseDao<StudentScoreEntity> {
  2. /**
  3. * 统计从fromYear到endYear年间学科subjects的统计数据
  4. *
  5. * @param fromYear 统计年份区间开始
  6. * @param endYear 统计年份区间结尾
  7. * @param subjects 统计的学科列表
  8. * @return 统计数据
  9. */
  10. List<ScoreStatistics> statistics(int fromYear, int endYear, String[] subjects);
  11. }

在StudentScoreDaoImpl上实现业务逻辑

  1. @Repository
  2. public class StudentScoreDaoImpl extends StudentScoreBaseDao implements StudentScoreDao {
  3. @Override
  4. public List<ScoreStatistics> statistics(int fromSchoolTerm, int endSchoolTerm, String[] subjects) {
  5. return super.listPoJos(ScoreStatistics.class, super.query()
  6. .select.schoolTerm().subject()
  7. .count("count")
  8. .min.score("min_score")
  9. .max.score("max_score")
  10. .avg.score("avg_score")
  11. .end()
  12. .where.isDeleted().isFalse()
  13. .and.schoolTerm().between(fromSchoolTerm, endSchoolTerm)
  14. .and.subject().in(subjects)
  15. .end()
  16. .groupBy.schoolTerm().subject().end()
  17. .orderBy.schoolTerm().asc().subject().asc().end()
  18. );
  19. }
  20. }
  1. DaoImpl实现中,除了根据条件返回统计结果,还讲结果按照下划线转驼峰的规则自动转换为ScoreStatistics对象返回。
  2. 测试
  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(classes = QuickStartApplication.class)
  3. public class StudentScoreDaoImplTest {
  4. @Autowired
  5. private StudentScoreDao dao;
  6.  
  7. @Test
  8. public void statistics() {
  9. List<ScoreStatistics> list = dao.statistics(2000, 2019, new String[]{"语文", "数学", "英语"});
  10. System.out.println(list);
  11. }
  12. }

查看控制台输出结果:

DEBUG - ==>  Preparing: SELECT school_term, subject, count(*) AS count, MIN(score) AS min_score, MAX(score) AS max_score, AVG(score) AS avg_score
    FROM student_score
    WHERE is_deleted = ?
    AND school_term BETWEEN ? AND ?
    AND subject IN (?, ?, ?)
    GROUP BY school_term, subject
    ORDER BY school_term ASC, subject ASC 
DEBUG - ==> Parameters: false(Boolean), 2000(Integer), 2019(Integer), 语文(String), 数学(String), 英语(String)
DEBUG - <==      Total: 30
[ScoreStatistics(schoolTerm=2000, subject=数学, count=17, minScore=1, maxScore=93, avgScore=36.0588),
 ...
 ScoreStatistics(schoolTerm=2009, subject=语文, count=24, minScore=3, maxScore=100, avgScore=51.2500)]

到此这篇关于Fluent Mybatis实际开发中的优势的文章就介绍到这了,更多相关Fluent Mybatis开发内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号