用mybatis生成插件自动生成配置文件
Posted 玉曲风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用mybatis生成插件自动生成配置文件相关的知识,希望对你有一定的参考价值。
1.在当前的maven项目的pom.xml中添加插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version></plugin>
</plugins>
</build>
2.然后在classpath下添加配置文件generatorConifig
<?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>
<!--数据库驱动包路径 -->
<classPathEntry location="/home/mars/.m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar"/>
<context id="MySQLTables" targetRuntime="MyBatis3">
<!--关闭注释 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test_database" userId="root"
password="123456">
</jdbcConnection>
<!--生成的model 包路径 -->
<javaModelGenerator targetPackage="model" targetProject="src/main/java">
<property name="enableSubPackages" value="ture"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成xml mapper文件 路径 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="ture"/>
</sqlMapGenerator>
<!-- 生成的Dao接口 的包路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="src/main/java">
<property name="enableSubPackages" value="ture"/>
</javaClientGenerator>
<!--对应数据库表名 -->
<table tableName="aa"></table>
</context>
</generatorConfiguration>
3.使用mybatis-generator:generate命令来执行配置文件
在eclipse下先安装插件mybatis-generator1.3.5插件.然后配置生成文件配置文件
<?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>
<!--数据库驱动包路径 -->
<classPathEntry location="/home/mars/.m2/repository/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35.jar"/>
<context id="MySQLTables" targetRuntime="MyBatis3">
<!--关闭注释 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://23.83.232.178:3306/yxyun" userId="root"
password="123456">
</jdbcConnection>
<!--生成的model 包路径 -->
<javaModelGenerator targetPackage="com.yxyun.entity" targetProject="yxyun/src/main/java">
<property name="enableSubPackages" value="ture"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成xml mapper文件 路径 -->
<sqlMapGenerator targetPackage="mapper" targetProject="yxyun/src/main/resources">
<property name="enableSubPackages" value="ture"/>
</sqlMapGenerator>
<!-- 生成的Dao接口 的包路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yxyun.dao" targetProject="yxyun/src/main/java">
<property name="enableSubPackages" value="ture"/>
</javaClientGenerator>
<!--对应数据库表名 -->
<table tableName="t_user"></table>
</context>
</generatorConfiguration>
然后右击配置文件,点击
以上是关于用mybatis生成插件自动生成配置文件的主要内容,如果未能解决你的问题,请参考以下文章
maven插件mybatis-generator自动生成代码
使用maven的mybatis-generator代码生成器插件生成实体类mapper配置文件和mapper接口(使用idea)