Backend Development 9 min read

Comparative Implementation of Student Score Statistics Using Fluent Mybatis, Mybatis, and Mybatis‑Plus

This article demonstrates how to use Fluent Mybatis, native Mybatis, and Mybatis‑Plus to implement a complex student‑score aggregation query, compares their code structures, generation settings, and highlights the advantages and drawbacks of each approach for backend development.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Comparative Implementation of Student Score Statistics Using Fluent Mybatis, Mybatis, and Mybatis‑Plus

Fluent Mybatis allows developers to build complex business SQL statements through a Java API, eliminating the need for separate XML mapper files and unifying code and SQL logic.

The example uses a student_score table and a requirement to calculate, for the year 2000 onward, the count, minimum, maximum, and average scores of three subjects (English, Math, Chinese) per term, filtering out records with scores below 60 and ensuring at least two samples per group.

The raw SQL for this requirement is presented, followed by three implementations:

Fluent Mybatis implementation

Code snippets (shown as images in the original article) illustrate the fluent API usage and IDE rendering.

Native Mybatis implementation

public interface MyStudentScoreMapper {
    List
> summaryScore(SummaryQuery paras);
}

@Data
@Accessors(chain = true)
public class SummaryQuery {
    private Integer schoolTerm;
    private List
subjects;
    private Integer score;
    private Integer minCount;
}
select school_term,
           subject,
           count(score) as count,
           min(score) as min_score,
           max(score) as max_score,
           avg(score) as max_score
    from student_score
    where school_term >= #{schoolTerm}
      and subject in
      <foreach collection="subjects" item="item" open="(" close=")" separator=",">
          #{item}
      </foreach>
      and score >= #{score}
      and is_deleted = 0
    group by school_term, subject
    having count(score) > #{minCount}
    order by school_term, subject
@RunWith(SpringRunner.class)
@SpringBootTest(classes = QuickStartApplication.class)
public class MybatisDemo {
    @Autowired
    private MyStudentScoreMapper mapper;

    @Test
    public void mybatis_demo() {
        SummaryQuery paras = new SummaryQuery()
            .setSchoolTerm(2000)
            .setSubjects(Arrays.asList("英语", "数学", "语文"))
            .setScore(60)
            .setMinCount(1);
        List
> summary = mapper.summaryScore(paras);
        System.out.println(summary);
    }
}

The native Mybatis approach requires manual XML mapping and is relatively verbose.

Mybatis‑Plus implementation

Mybatis‑Plus simplifies the code but still relies heavily on string literals for field names, which can cause maintenance issues.

Code generation comparison

Fluent Mybatis code generator

public class AppEntityGenerator {
    static final String url = "jdbc:mysql://localhost:3306/fluent_mybatis_demo?useSSL=false&useUnicode=true&characterEncoding=utf8";

    public static void main(String[] args) {
        FileGenerator.build(Abc.class);
    }

    @Tables(
        url = url, username = "root", password = "password",
        basePack = "cn.org.fluent.mybatis.springboot.demo",
        srcDir = "spring-boot-demo/src/main/java",
        daoDir = "spring-boot-demo/src/main/java",
        gmtCreated = "gmt_create", gmtModified = "gmt_modified", logicDeleted = "is_deleted",
        tables = @Table(value = {"student_score"})
    )
    static class Abc {}
}

Mybatis‑Plus code generator

public class CodeGenerator {
    static String dbUrl = "jdbc:mysql://localhost:3306/fluent_mybatis_demo?useSSL=false&useUnicode=true&characterEncoding=utf8";

    @Test
    public void generateCode() {
        GlobalConfig config = new GlobalConfig();
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
            .setUrl(dbUrl)
            .setUsername("root")
            .setPassword("password")
            .setDriverName(Driver.class.getName());
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true)
            .setEntityLombokModel(false)
            .setNaming(NamingStrategy.underline_to_camel)
            .setColumnNaming(NamingStrategy.underline_to_camel)
            .setEntityTableFieldAnnotationEnable(true)
            .setFieldPrefix(new String[]{"test_"})
            .setInclude(new String[]{"student_score"})
            .setLogicDeleteFieldName("is_deleted")
            .setTableFillList(Arrays.asList(
                new TableFill("gmt_create", FieldFill.INSERT),
                new TableFill("gmt_modified", FieldFill.INSERT_UPDATE)));
        config.setActiveRecord(false)
            .setIdType(IdType.AUTO)
            .setOutputDir(System.getProperty("user.dir") + "/src/main/java/")
            .setFileOverride(true);
        new AutoGenerator().setGlobalConfig(config)
            .setDataSource(dataSourceConfig)
            .setStrategy(strategyConfig)
            .setPackageInfo(new PackageConfig()
                .setParent("com.mp.demo")
                .setController("controller")
                .setEntity("entity"))
            .execute();
    }
}

Both generators produce entity and DAO code, but Fluent Mybatis offers tighter integration between Java and SQL, while Mybatis‑Plus relies more on conventions and string‑based configurations.

Fluent Mybatis feature overview

An image in the original article lists the key capabilities of Fluent Mybatis, such as type‑safe query building, automatic mapper generation, and support for logical deletion.

Conclusion

After reviewing the three frameworks for the same functional requirement, the author concludes that Fluent Mybatis provides the most concise and type‑safe solution, Mybatis‑Plus is simpler but prone to hard‑coded strings, and native Mybatis is the most verbose and error‑prone.

Readers are encouraged to share the article, join the architecture community, and explore the provided source code links.

JavaSQLbackend developmentMyBatisORMMyBatis-PlusFluent MyBatis
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.