深入Mybatis generator core增加功能

      众所周知, Mybatis generator作为mybatis 在eclipse上最受欢迎的插件, 怕做ssm的同学人手一件吧。由于我自已早前写的框架是基于ssh的, 有一个容器用于记录entity的信息, 如entity属性对应的字段等(就是@Column了), 但转为ssm之后, 由于mybatis mapper用到的只是pojo,没有这些注解,所以打算改造这个生成器, 让其也给我加上。

      首先下载mybatis generator core原代码(我下的是最新的1.4.0),就是下图这个了,然后建一个名为“mybatis-generator”的maven项目,将代码扔进去。 但比较奔溃的是源代码自带的pom.xml没有指定依赖的版本, 我为这个匹配版本花了一晚时间。。。

 

所以在这里贴出:

<!-- http://mvnrepository.com/ 想要什麽,通過這個URL查找 -->
<!-- -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.mybatis.generator</groupId>
	<version>1.4.0-SNAPSHOT</version>


	<artifactId>mybatis-generator-core</artifactId>
	<packaging>jar</packaging>
	<name>MyBatis Generator Core</name>

	<properties>
		<checkstyle.config>${project.basedir}/../checkstyle-override.xml</checkstyle.config>
		<module.name>org.mybatis.generator</module.name>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-site-plugin</artifactId>
				<configuration>
					<skipDeploy>false</skipDeploy>
				</configuration>
			</plugin>
			<!-- create the Source JAR and JavaDoc a bit earlier so it is ready for 
				the assembly -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<executions>
					<execution>
						<phase>prepare-package</phase>
						<goals>
							<goal>jar-no-fork</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<executions>
					<execution>
						<phase>prepare-package</phase>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>org.mybatis.generator.api.ShellRunner</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptors>
						<descriptor>${project.basedir}/src/main/assembly/src.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>bundle</id>
						<goals>
							<goal>single</goal>
						</goals>
						<phase>package</phase>
					</execution>
				</executions>
			</plugin>


		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<scope>provided</scope>
			<optional>true</optional>
			<version>1.2.17</version>
		</dependency>
		

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<scope>provided</scope>
			<version>2.11.1</version>
			<optional>true</optional>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.26</version>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<scope>provided</scope>
			<optional>true</optional>
			<version>1.1.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.ant</groupId>
			<artifactId>ant</artifactId>
			<scope>provided</scope>
			<optional>true</optional>
			<version>1.10.4</version>
		</dependency>

		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>5.5.0-M1</version>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-params</artifactId>
			<version>5.5.0-M1</version>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.5.0-M1</version>
		</dependency>
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-launcher</artifactId>
			<version>1.5.0-M1</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.assertj</groupId>
			<artifactId>assertj-core</artifactId>
			<version>3.12.0</version>
		</dependency>
		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>2.4.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.github.javaparser</groupId>
			<artifactId>javaparser-core</artifactId>
			<version>3.13.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.github.javaparser/javaparser-symbol-solver-core -->
	
		 <dependency>
            <groupId>com.github.javaparser</groupId>
            <artifactId>javaparser-symbol-solver-core</artifactId>
            <version>3.13.2</version>
        </dependency>
        <dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>9.4.1209</version>
		</dependency>

		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-stdlib</artifactId>
			<version>1.3.21</version>
		</dependency>
	</dependencies>

	<distributionManagement>
		<site>
			<id>gh-pages</id>
			<name>Mybatis Generator GitHub Pages</name>
			<url>git:ssh://git@github.com/mybatis/generator.git?gh-pages#</url>
		</site>
	</distributionManagement>

	<description>MyBatis Generator - a code generator for MyBatis.</description>
	<scm>
		<url>https://github.com/mybatis/generator</url>
		<tag>HEAD</tag>
	</scm>
</project>

 

好了, 将原项目的generatorConfig.xml复制到mybatis-generator\target\test-classes\scripts下并改名为test1.xml,并将javaModelGenerator这节加上genEntity="true",没错, 就是增加一个genEntity开关, 如果为true时就会增加entity的注解。

 

当然,光是改xml配置文件是不行的, 必须要修改代码:

      1、首先, 让配置参数识别这个新的开关, 那么需要修改JavaModelGeneratorConfiguration.java,增加genEntity属性并getter/setter.

2、修改MyBatisGeneratorConfigurationParser.java,在parseJavaModelGenerator方法中加上更新genEntity开关的功能

 

3、如果配置文件中的targetproject指向的路径不存在, 就会报“The specified target project directory does not exist”这种异常, 反正动手术了, 也不在乎多改一点, 盘他 :

     修改DefaultShellCallback.java:

4、修改PrimaryKeyGenerator.java,

 

5、修改BaseRecordGenerator.java,

6、修改mybatis-generator-config_1_0.dtd

修改完毕:

 

    修改JavaCodeGenerationTest.java, 增加一个测试项:

@Test
    public void testMyTest() throws Exception {
        //createDatabase();
    	//List<GeneratedJavaFile>  lvLst=generateJavaFiles("/scripts/test1.xml");
    	//Assert.assertNotNull(lvLst);
    	List<String> warnings=new ArrayList<String>();  
        try {  
//      导入配置表mybatis-generator.xml  
        String configFile="/scripts/test1.xml";  
//      解析  
        ConfigurationParser cp=new ConfigurationParser(warnings);  
        //Configuration config=cp.parseConfiguration(configFile);
        Configuration config = cp.parseConfiguration(JavaCodeGenerationTest.class.getResourceAsStream(configFile));
//      是否覆盖  
        DefaultShellCallback dsc=new DefaultShellCallback(true);  
        MyBatisGenerator mg=new MyBatisGenerator(config, dsc, warnings);  
        mg.generate(null);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }

生成:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值