Backend Development 9 min read

Comparing Fluent Mybatis, Native Mybatis, and Mybatis‑Plus for Student Score Statistics

This article demonstrates how to implement a statistical query on a student_score table using three ORM frameworks—Fluent Mybatis, native Mybatis, and Mybatis‑Plus—showing code examples, configuration steps, and a side‑by‑side comparison of their ease of use and generated code.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Comparing Fluent Mybatis, Native Mybatis, and Mybatis‑Plus for Student Score Statistics

The article introduces three Java ORM frameworks—Fluent Mybatis, native Mybatis, and Mybatis‑Plus—and demonstrates how to implement a statistical query on a student_score table that aggregates count, minimum, maximum, and average scores for three subjects across terms, with a sample size greater than one.

First, the required SQL statement is presented, filtering by term (>=2000), subjects (English, Math, Chinese), passing score (>=60), and non‑deleted records, then grouping and ordering the results.

Fluent Mybatis implementation : The fluent API allows building the query directly in Java without XML. Example code shows how to create the query using the fluent builder, execute it, and print the results.

public class FluentDemo {
    public static void main(String[] args) {
        SummaryQuery query = new SummaryQuery()
            .setSchoolTerm(2000)
            .setSubjects(Arrays.asList("英语", "数学", "语文"))
            .setScore(60)
            .setMinCount(1);
        List
> result = fluentMapper.summaryScore(query);
        System.out.println(result);
    }
}

Native Mybatis implementation : The article defines a Mapper interface, a parameter POJO, an XML mapper file, and a JUnit test class to execute the same query.

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 id="summaryScore" resultType="map" parameterType="cn.org.fluent.mybatis.springboot.demo.mapper.SummaryQuery">
    SELECT school_term, subject,
           COUNT(score) AS count,
           MIN(score) AS min_score,
           MAX(score) AS max_score,
           AVG(score) AS avg_score
    FROM student_score
    WHERE school_term >= #{schoolTerm}
      AND subject IN
        <foreach collection="subjects" item="item" open="(" separator="," close=")">#{item}</foreach>
      AND score >= #{score}
      AND is_deleted = 0
    GROUP BY school_term, subject
    HAVING COUNT(score) > #{minCount}
    ORDER BY school_term, subject
</select>
@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);
    }
}

Mybatis‑Plus implementation : The article points out that Mybatis‑Plus requires more hard‑coded strings and shows a code‑generator configuration class that sets up database connection, package paths, table fill strategies, and execution of the generator.

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

    @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();
    }
}

The article also provides a feature overview of Fluent Mybatis (image omitted) and a comparison table (image omitted) that summarises the advantages and drawbacks of each framework, concluding that Fluent Mybatis offers a more concise API while Mybatis‑Plus involves more string literals.

In conclusion, the author encourages readers to try Fluent Mybatis, explore its source code, and follow the provided links for additional resources and sample projects.

BackendJavaSQLMyBatisORMMyBatis-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.