This commit is contained in:
2026-01-23 17:41:45 +08:00
parent a146fd68a9
commit 13c45dcedb
15 changed files with 897 additions and 76 deletions

View File

@@ -0,0 +1,132 @@
package com.example.demo.util;
import com.example.demo.entity.Author;
import com.example.demo.entity.Book;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertTrue;
class MyBatisGeneratorUtilsTest {
@Test
void testGenerateSql_Simple() {
List<String> fields = Arrays.asList("title", "isbn");
String sql = MyBatisGeneratorUtils.generateSql(Book.class, fields);
System.out.println("=== Simple SQL ===");
System.out.println(sql);
assertTrue(sql.contains("SELECT"));
// Root fields should NOT have prefix (to match standard ResultMap)
// book.title AS title
assertTrue(sql.contains("book.title AS title"));
assertTrue(sql.contains("FROM book book"));
}
@Test
void testGenerateSql_OneToMany_List() {
// Author -> List<Book>
// Request: author.name, author.books.title
List<String> fields = Arrays.asList("name", "books.title");
String sql = MyBatisGeneratorUtils.generateSql(Author.class, fields);
System.out.println("=== OneToMany List SQL ===");
System.out.println(sql);
// Root: author
assertTrue(sql.contains("FROM author author"));
assertTrue(sql.contains("author.name AS name"));
// Check Forced ID
assertTrue(sql.contains("author.id AS id"));
// Join: author -> book (OneToMany)
// mappedBy="author" in Author means Book has "author" field.
// Join condition should be: author.id = books.author_id
assertTrue(sql.contains("LEFT JOIN book author$books"));
// Correct Join Condition for OneToMany
// author.id = author$books.author_id (assuming default convention or @JoinColumn on Book.author)
// Book.java: @JoinColumn(name = "author_id") private Author author;
assertTrue(sql.contains("author.id = author$books.author_id"));
// Check Child Columns
assertTrue(sql.contains("author$books.title AS books$title"));
// Check Child Forced ID
assertTrue(sql.contains("author$books.id AS books$id"));
}
@Test
void testGenerateMapperXml() {
List<String> fields = Arrays.asList("title", "author.name", "author.books");
String xml = MyBatisGeneratorUtils.generateMapperXml(Book.class, "com.example.demo.mapper.BookMapper", fields);
System.out.println("=== Generated Mapper XML ===");
System.out.println(xml);
assertTrue(xml.contains("<mapper namespace=\"com.example.demo.mapper.BookMapper\">"));
assertTrue(xml.contains("<resultMap id=\"BookMap\""));
assertTrue(xml.contains("<resultMap id=\"AuthorMap\""));
// Verify BookMap -> AuthorMap association
assertTrue(xml.contains("<association property=\"author\" resultMap=\"AuthorMap\" columnPrefix=\"author$\"/>"));
// Verify AuthorMap -> Books collection
assertTrue(xml.contains("<collection property=\"books\" resultMap=\"BookMap\" columnPrefix=\"books$\"/>"));
// Verify SQL is included
assertTrue(xml.contains("SELECT"));
// Use column name in alias (publish_month)
assertTrue(xml.contains("book$author$books.publish_month AS author$books$publish_month"));
}
@Test
void testGenerateSql_NestedJoin() {
List<String> fields = Arrays.asList("title", "author.name", "author.region.name");
String sql = MyBatisGeneratorUtils.generateSql(Book.class, fields);
System.out.println("=== Nested Join SQL ===");
System.out.println(sql);
// Verify Joins
assertTrue(sql.contains("LEFT JOIN author book$author"));
assertTrue(sql.contains("LEFT JOIN region book$author$region"));
// Verify Columns
// Child fields SHOULD have relative prefix
// book$author.name AS author$name
assertTrue(sql.contains("book$author.name AS author$name"));
// book$author$region.name AS author$region$name
assertTrue(sql.contains("book$author$region.name AS author$region$name"));
}
@Test
void testGenerateSql_ExpandObject() {
// "periodical" is an object, should expand to periodical.name, periodical.publishMonth etc.
List<String> fields = Arrays.asList("title", "periodical");
String sql = MyBatisGeneratorUtils.generateSql(Book.class, fields);
System.out.println("=== Expand Object SQL ===");
System.out.println(sql);
assertTrue(sql.contains("LEFT JOIN periodical book$periodical"));
assertTrue(sql.contains("book$periodical.name AS periodical$name"));
// Use column name in alias: publish_month
assertTrue(sql.contains("book$periodical.publish_month AS periodical$publish_month"));
}
@Test
void testGenerateSql_CompositeKeyJoin() {
List<String> fields = Arrays.asList("title", "periodical.name");
String sql = MyBatisGeneratorUtils.generateSql(Book.class, fields);
System.out.println("=== Composite Key Join SQL ===");
System.out.println(sql);
// Verify Composite Join Condition (publisher_id AND publish_month)
assertTrue(sql.contains("LEFT JOIN periodical book$periodical"));
assertTrue(sql.contains("book.publisher_id = book$periodical.publisher_id"));
assertTrue(sql.contains("book.publish_month = book$periodical.publish_month"));
}
}