Java Code Examples for org.mybatis.generator.api.MyBatisGenerator#getGeneratedJavaFiles()

The following examples show how to use org.mybatis.generator.api.MyBatisGenerator#getGeneratedJavaFiles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapperAnnotationPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试配置Repository
 * @throws Exception
 */
@Test
public void testWithRepository() throws Exception{
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/MapperAnnotationPlugin/mybatis-generator-with-repository.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof Interface && compilationUnit.getType().getShortName().endsWith("Mapper")) {
            Interface interfaze = (Interface) compilationUnit;

            Assert.assertEquals(interfaze.getAnnotations().size(), 2);
            Assert.assertEquals(interfaze.getAnnotations().get(0), "@Mapper");
            Assert.assertEquals(interfaze.getAnnotations().get(1), "@Repository");
            Assert.assertTrue(interfaze.getImportedTypes().contains(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper")));
            Assert.assertTrue(interfaze.getImportedTypes().contains(new FullyQualifiedJavaType("org.springframework.stereotype.Repository")));
        }
    }
}
 
Example 2
Source File: MapperAnnotationPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试默认配置
 */
@Test
public void testDefault() throws Exception{
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/MapperAnnotationPlugin/mybatis-generator.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof Interface && compilationUnit.getType().getShortName().endsWith("Mapper")) {
            Interface interfaze = (Interface) compilationUnit;

            Assert.assertEquals(interfaze.getAnnotations().size(), 1);
            Assert.assertEquals(interfaze.getAnnotations().get(0), "@Mapper");
            Assert.assertTrue(interfaze.getImportedTypes().contains(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper")));
        }
    }
}
 
Example 3
Source File: LombokPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试具体生成(只有keys的特殊情况,尽量使用Builder)
 */
@Test
public void testGenerateWithOnlyKeys() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator-with-only-keys.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof TopLevelClass) {
            TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
            String name = topLevelClass.getType().getShortName();
            if (name.equals("TbOnlyKeysKey")){
                Assert.assertTrue(topLevelClass.getAnnotations().contains("@Builder"));
            }
        }
    }
}
 
Example 4
Source File: BugFixedTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试domainObjectRenamingRule和
 */
@Test
public void bug0004() throws Exception {
    DBHelper.createDB("scripts/BugFixedTest/bug-0004.sql");
    // 规则 ^T 替换成空,也就是去掉前缀
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/bug-0004.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        String name = file.getCompilationUnit().getType().getShortName();
        if (!name.matches("B.*")) {
            Assert.assertTrue(false);
        }
        if (name.endsWith("Example")) {
            Assert.assertEquals(file.getCompilationUnit().getType().getPackageName(), "com.itfsw.dao.example");
        }
    }
}
 
Example 5
Source File: CommentPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试配置了模板参数转换
 */
@Test
public void testGenerateWithOutComment() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/CommentPlugin/mybatis-generator-without-comment.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    // java中的注释
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        if (file.getFileName().equals("Tb.java")) {
            TopLevelClass topLevelClass = (TopLevelClass) file.getCompilationUnit();
            // addJavaFileComment
            Assert.assertEquals(topLevelClass.getFileCommentLines().size(), 0);
            // addFieldComment
            Field id = topLevelClass.getFields().get(0);
            Assert.assertEquals(id.getJavaDocLines().size(), 0);
            // addGeneralMethodComment
            Method cons = topLevelClass.getMethods().get(0);
            Assert.assertEquals(cons.getJavaDocLines().size(), 0);
            // addSetterComment
            Method setter = topLevelClass.getMethods().get(5);
            Assert.assertEquals(setter.getJavaDocLines().size(), 0);
        }
    }
}
 
Example 6
Source File: BugFixedTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试批量batchUpsert存在主键的情况
 * https://github.com/itfsw/mybatis-generator-plugin/issues/77
 * @throws Exception
 */
@Test
public void issues81() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/issues-81.xml");
    MyBatisGenerator myBatisGenerator = tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/issues-81.sql"));

    // 是否在使用系统默认模板
    int count = 0;
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        if (file.getFormattedContent().indexOf(MergeConstants.NEW_ELEMENT_TAG) != -1) {
            count++;
        }
    }
    Assert.assertTrue(count == 0);
}
 
Example 7
Source File: BugFixedTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 表重命名配置插件生成的大小写错误
 * @throws Exception
 */
@Test
public void issues63() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/issues-63.xml");
    MyBatisGenerator generator = tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/issues-63.sql"));
    for (GeneratedJavaFile file : generator.getGeneratedJavaFiles()) {
        String fileName = file.getFileName();
        if (fileName.startsWith("Repaydetail")) {
            Assert.assertTrue("官方自己的问题", true);
        }
    }
}
 
Example 8
Source File: TableRenameConfigurationPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试exampleSuffix
 */
@Test
public void testExampleSuffix() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    boolean find = false;
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        String name = file.getCompilationUnit().getType().getShortName();
        if (name.equals("TbQuery")){
            find = true;
        }
    }
    Assert.assertTrue(find);
    // 执行一条语句确认其可用
    tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
        @Override
        public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
            ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));

            ObjectUtil tbQuery = new ObjectUtil(loader, packagz + ".TbQuery");
            ObjectUtil criteria = new ObjectUtil(tbQuery.invoke("createCriteria"));
            criteria.invoke("andIdLessThan", 4L);

            // sql
            String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbQuery.getObject());
            Assert.assertEquals(sql, "select id, field1, inc_f1, inc_f2, inc_f3 from tb WHERE ( id < '4' )");
            // 执行
            List list = (List) tbMapper.invoke("selectByExample", tbQuery.getObject());
            Assert.assertEquals(list.size(), 3);
        }
    });
}
 
Example 9
Source File: TableRenamePluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试替代 TablePrefixPlugin 插件
 */
@Test
public void testGenerateLikeTablePrefixPlugin() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenamePlugin/mybatis-generator-like-TablePrefixPlugin.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){
        String name = file.getCompilationUnit().getType().getShortName();
        Assert.assertTrue(name.matches("DB1.*"));
    }
}
 
Example 10
Source File: TableRenamePluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试具体生成
 */
@Test
public void testGenerate() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
    // 规则1 ^T 替换成 Test, 同时tb2 使用了tableOverride 替换成 TestOverride
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenamePlugin/mybatis-generator-rule1.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){
        String name = file.getCompilationUnit().getType().getShortName();
        if (name.matches(".*1.*")){
            Assert.assertTrue(name.matches("Testb1.*"));
        } else {
            Assert.assertTrue(name.matches("TestOverride.*"));
        }
    }
}
 
Example 11
Source File: ExampleTargetPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigPath() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleTargetPlugin/mybatis-generator.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    List<GeneratedJavaFile> list = myBatisGenerator.getGeneratedJavaFiles();
    for (GeneratedJavaFile file : list){
        if (file.getFileName().equals("TbExample.java")){
            Assert.assertEquals(file.getTargetPackage(), "com.itfsw.mybatis.generator.plugins.dao.example");
        }
    }
}
 
Example 12
Source File: ExampleTargetPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalPath() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    List<GeneratedJavaFile> list = myBatisGenerator.getGeneratedJavaFiles();
    for (GeneratedJavaFile file : list){
        if (file.getFileName().equals("TbExample.java")){
            Assert.assertEquals(file.getTargetPackage(), tool.getTargetPackage());
        }
    }
}
 
Example 13
Source File: CommentPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试配置了模板参数转换
 */
@Test
public void testGenerateWithTemplate() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/CommentPlugin/mybatis-generator.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    // java中的注释
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        if (file.getFileName().equals("Tb.java")) {
            TopLevelClass topLevelClass = (TopLevelClass) file.getCompilationUnit();
            // addJavaFileComment
            Assert.assertEquals(topLevelClass.getFileCommentLines().get(0), "TestAddJavaFileComment:Tb:" + new SimpleDateFormat("yyyy-MM").format(new Date()));
            // addFieldComment 同时测试 if 判断和 mbg
            Field id = topLevelClass.getFields().get(0);
            Assert.assertEquals(id.getJavaDocLines().get(0), "注释1");
            Assert.assertEquals(id.getJavaDocLines().get(1), MergeConstants.NEW_ELEMENT_TAG);
            // addGeneralMethodComment
            Method cons = topLevelClass.getMethods().get(0);
            Assert.assertEquals(cons.getJavaDocLines().get(0), "addGeneralMethodComment:Tb:tb");
            // addSetterComment
            Method setter = topLevelClass.getMethods().get(5);
            Assert.assertEquals(setter.getJavaDocLines().get(0), "addSetterComment:field1:field1");
        }
    }

    // xml注释
    ObjectUtil xml = new ObjectUtil(myBatisGenerator.getGeneratedXmlFiles().get(0));
    Document doc = (Document) xml.get("document");
    List<Element> els = ((XmlElement) (doc.getRootElement().getElements().get(0))).getElements();
    String comment = ((TextElement) els.get(0)).getContent();
    Assert.assertEquals(comment, "addComment:BaseResultMap");
}
 
Example 14
Source File: TablePrefixPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试具体生成
 */
@Test
public void testGenerate() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
    // 全局规则增加 DB, tb2 单独规则增加Tt
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TablePrefixPlugin/mybatis-generator.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){
        String name = file.getCompilationUnit().getType().getShortName();
        if (name.matches(".*1.*")){
            Assert.assertTrue(name.matches("DB.*"));
        } else {
            Assert.assertTrue(name.matches("Tt.*"));
        }
    }
}
 
Example 15
Source File: LombokPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试具体生成
 */
@Test
public void testGenerateDefault() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator-default.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    List<String> comm = Arrays.asList("@Data");
    List<String> child = new ArrayList<>(Arrays.asList("@EqualsAndHashCode(callSuper = true)", "@ToString(callSuper = true)"));
    child.addAll(comm);

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof TopLevelClass) {
            TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
            String name = topLevelClass.getType().getShortName();
            if ("TbKeyBlobKey".equals(name)) {
                Assert.assertEquals(comm.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(comm.containsAll(topLevelClass.getAnnotations()));
            } else if ("TbKeyBlobWithBLOBs".equals(name)) {
                Assert.assertEquals(child.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(child.containsAll(topLevelClass.getAnnotations()));
            }

            // tb 没有继承
            if ("Tb".equals(name)) {
                Assert.assertEquals(comm.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(comm.containsAll(topLevelClass.getAnnotations()));
            }
        }
    }
}
 
Example 16
Source File: TableRenameConfigurationPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * 测试modelSuffix
 */
@Test
public void testModelSuffix() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-modelSuffix.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    boolean find = false;
    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        String name = file.getCompilationUnit().getType().getShortName();
        if (name.equals("TbEntity")){
            find = true;
        }
    }
    Assert.assertTrue(find);
    // 执行一条语句确认其可用
    tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
        @Override
        public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
            ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));

            ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
            ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
            criteria.invoke("andIdEqualTo", 4L);

            ObjectUtil tb = new ObjectUtil(loader, packagz + ".TbEntity");
            tb.set("id", 4L);
            tb.set("field1", "ts1");
            tb.set("incF1", 5L);

            // sql
            String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateByExample", tb.getObject(), tbExample.getObject());
            Assert.assertEquals(sql, "update tb set id = 4, field1 = 'ts1', inc_f1 = 5, inc_f2 = null, inc_f3 = null WHERE ( id = '4' )");
            // 执行
            int count = (int) tbMapper.invoke("updateByExample", tb.getObject(), tbExample.getObject());
            Assert.assertEquals(count, 1);
            // 执行结果查询
            List list = (List) tbMapper.invoke("selectByExample", tbExample.getObject());
            ObjectUtil result = new ObjectUtil(list.get(0));
            Assert.assertEquals(result.get("id"), 4L);
            Assert.assertEquals(result.get("field1"), "ts1");
            Assert.assertEquals(result.get("incF1"), 5L);
        }
    });
}
 
Example 17
Source File: LombokPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * 测试具体生成
 */
@Test
public void testGenerate() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    List<String> comm = Arrays.asList("@Data", "@NoArgsConstructor", "@AllArgsConstructor");
    List<String> child = new ArrayList<>(Arrays.asList("@EqualsAndHashCode(callSuper = true)", "@ToString(callSuper = true)"));
    child.addAll(comm);
    List<String> superBuilderParent = new ArrayList<>(Arrays.asList("@SuperBuilder"));
    superBuilderParent.addAll(comm);
    List<String> superBuilderChild = new ArrayList<>(Arrays.asList("@SuperBuilder"));
    superBuilderChild.addAll(child);
    List<String> builder = new ArrayList<>(Arrays.asList("@Builder"));
    builder.addAll(comm);

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof TopLevelClass) {
            TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
            String name = topLevelClass.getType().getShortName();
            if ("TbKeyBlobKey".equals(name)) {
                Assert.assertEquals(superBuilderParent.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(superBuilderParent.containsAll(topLevelClass.getAnnotations()));
            } else if ("TbKeyBlobWithBLOBs".equals(name)) {
                Assert.assertEquals(superBuilderChild.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(superBuilderChild.containsAll(topLevelClass.getAnnotations()));
            }

            // tb 没有继承
            if ("Tb".equals(name)) {
                Assert.assertEquals(builder.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(builder.containsAll(topLevelClass.getAnnotations()));
            }

            if ("TbKeysKey".equals(name)) {
                Assert.assertEquals(superBuilderParent.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(superBuilderParent.containsAll(topLevelClass.getAnnotations()));
            } else if ("TbKeys".equals(name)) {
                Assert.assertEquals(superBuilderChild.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(superBuilderChild.containsAll(topLevelClass.getAnnotations()));
            }
        }
    }
}