【配置文件内容】:
<?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>
<!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径,需确保本地路径下存在该jar -->
<classPathEntry location="D:\jar\mysql-connector-java-5.1.47/mysql-connector-java-5.1.47.jar"/>
<!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!--序列化-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!--以下需要插件 -->
<!--
插入成功后返回ID
<plugin type="cn.doity.common.generator.plugin.InsertAndReturnKeyPlugin"/>
分页查询功能
<plugin type="cn.doity.common.generator.plugin.SelectByPagePlugin"/>
生成带有for update后缀的select语句插件
<plugin type="cn.doity.common.generator.plugin.SelectForUpdatePlugin"/> -->
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.12.203:3306/test"
userId="root"
password="root">
</jdbcConnection>
<!-- 生成model类,targetPackage指定model类的包名, targetProject指定生成的model放在eclipse的哪个工程下面-->
<javaModelGenerator targetPackage="com.bj.springboot.model" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪个工程下面 -->
<sqlMapGenerator targetPackage="com.bj.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, targetProject指定生成的Mapper接口放在eclipse的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.bj.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名及对应的Java模型类名 -->
<table tableName="student"
domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
【运行操作】:
找到idea项目右侧的maven列,打开Plugins->mybatis-generator->mybatis-generatorgenerate,如下图所示:
注:若找不到maven列,菜单栏File->Settings->Appearance->找到show tool window bars 打勾,如下图所示:
【结果】:
运行成功后,项目自动生成代码:
1、根据配置文件中设置的targetPackage=“com.bj.springboot.model”,domainObjectName=“Student”:
生成model目录下的以Student为模型类名的类,如下图所示:
2、根据targetPackage=“com.bj.springboot.mapper”,domainObjectName=“Student”
生成mapper目录下的以Student为模型类名的Mapper接口类,如下图所示:
3、根据targetPackage=“com.bj.springboot.mapper”,domainObjectName=“Student”
生成mapper目录下的以Student为模型类名的Mapper.xml文件,如下图所示:
版权声明:本文为xiaomin1328原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。