mybatis自动生成代码有三种方式:命令行、eclipse插件、maven插件。在这里主要介绍比较方便使用的一种方式–maven插件,它可以在eclipse、idea中通用。
在pom.xml文件中配置mybatis-generator插件:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!-- 配置文件的位置 -->
<configurationFile>generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
逆向生成代码需要的配置文件generatorConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定连接数据库的JDBC驱动,指定到本机的完整路径(可以指定到maven仓库中的jar路径) -->
<classPathEntry
location="D:/Workspaces/maven_repos/mysql/mysql-connector-java/5.1.8/mysql-connector-java-5.1.8.jar"/>
<!-- 配置table表信息内容体,targetRuntime指定采用mybatis3的版本 -->
<context id="my" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文版的,可以不让它生成 -->
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/springdb" userId="root"
password="123456"/>
<!-- 生成model类,targetPackage指定model类的包名,targetProject指定生成的model放在哪个工程中 -->
<javaModelGenerator targetPackage="com.cui.springboot.model"
targetProject="D:/Workspaces/IdeaProjects/springboot-mybatis/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mybatis的mapper.xml文件,targetPackage指定mapper.xml的包名,targetProject指定mapper.xml在哪个工程目录下 -->
<sqlMapGenerator targetPackage="com.cui.springboot.mapper"
targetProject="D:/Workspaces/IdeaProjects/springboot-mybatis/src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成mybatis的Mapper接口类文件,targetPackage指定Mapper接口类文件的包名,targetProject指定Mapper接口类在哪个工程目录下 -->
<javaClientGenerator targetPackage="com.cui.springboot.mapper"
targetProject="D:/Workspaces/IdeaProjects/springboot-mybatis/src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>-->
<!-- 数据库表以及对应的java模型类名(如果是多个表的话,可以复制多份然后修改成对应的表以及需要生成的模型名) -->
<table tableName="student" domainObjectName="Student"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
</context>
</generatorConfiguration>
在上面的配置文件中已经将注释给出来了,需要注意的有以下几点:
- 在table标签中的tableName和domainObjectName是必须的,分别对应着数据库表名和实体类类名。其余的配置参数可以删除,一般默认是false。
- 创建数据表示时,字段名称可以使用下划线“_”将多个单词进行分割,比如:REC_ID这样的生成的实体类的属性是驼峰型,这样子比较好看一点。
- oracle中,数值形的字段,如果指定精度,比如Number(12,2),默认生成entity属性是BigDecimal型 ,如果不指定精度,比如:Number(9),指默认生成的是Long型
- oracle中的nvarchar/nvarchar2,mybatis-generator会识别成Object型,建议不要用nvarchar2,改用varchar2
弄完以上的步骤之后,就可以进行生成代码操作,如果idea的话,可以通过以下进行生成:
找到mybatis-generator:generate,然后双击操作,即可生成代码。
生成的目录结构如下:
还可以通过以下的方式进行执行mybatis-generator:
在该插件的目录下,按住shift键,选择“在此处打开命令窗口”选项,在弹出的窗口中输入以下的命令,然后回车(注意:需要将配置文件generatorConfig.xml文件也复制到该目录下):
java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
更多推荐
mybatis自动生成代码
发布评论