maven(18)-mybatis generator插件



generator的作用

使用mybatis框架,在初始项目或修改数据库时,相应的要在JAVA项目中去写一些数据模型文件,DAO,映射XML等配置,而这个插件的作用就是自动生成这些文件,以节省大量时间


pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                  <!-- 在控制台打印执行日志 -->
                  <verbose>true</verbose> 
                  <!-- 重复生成时会覆盖之前的文件-->  
                  <overwrite>true</overwrite>
                  <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>  
                </configuration>
            </plugin>
        </plugins>
    </build>

generatorConfig.xml

默认在src/main/resources目录配置这个文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
	<!-- mysql的jar文件路径 -->
	<classPathEntry location="D:\xxx\xxx.jar" />
	<context id="Mysql" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
			<property name="suppressDate" value="true" />
		</commentGenerator>
		
		<!-- 数据库相关配置 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"	connectionURL="jdbc:mysql://localhost/test?useSSL=false" userId="root" password="123456"/>

		<!-- 配置pojo目录 -->
		<javaModelGenerator targetPackage="mybatis.pojo" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</javaModelGenerator>
		
		<!-- 配置xml映射目录 -->
		<sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		
		<!-- 配置dao目录 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="mybatis.dao" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		
		<!-- tableName是数据库中的表名,domainObjectName是生成的JAVA模型名,后面的参数不用改,要生成更多的表就在下面继续加table标签 -->
		<table tableName="demo" domainObjectName="demo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
	</context>
</generatorConfiguration>
有注释的地方需要改成自己的环境配置

执行
执行命令:mvn mybatis-generator:generate

以上三个文件都是自动生成的

posted @ 2017-06-03 09:02  free_java  阅读(210)  评论(0编辑  收藏  举报