1、准备文件
1)下载Mybatis-Generator核心包(https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core/1.3.7)
2)mysql数据库的驱动jar包(https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.47)
3)generatorConfig.xml(配置文件,手动创建)
2、编辑generatorConfig.xml
<?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="E:/code/repository/ruidatielu/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ruidatielu?useUnicode=true&characterEncoding=UTF-8&useSSL=false" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.ruidatielu.entity" targetProject="Gen">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="Gen">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ruidatielu.dao" targetProject="Gen">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
注意的点:1)数据库驱动那里的location换成自己驱动包的位置,注意是/;
2) 数据库连接地址那里的connectionURL="jdbc:mysql://localhost:3306/数据库名字?useUnicode=true&characterEncoding=UTF-8&注意是&
3)下面的生成资源位置的targetPackage="com.ruidatielu.entity" targetProject="Gen"。注意的点是targetProject后面的路径一定要提前手动建好。targetProject后面的值可以是自己创建的任何名称,targetPackage后面的值关系到项目的包结构。
4)生成对应表及类名 <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)
3.生成文件的命令语句
管理员权限打开cmd窗口,进入到 mybatis-generator-core-1.3.7.jar所在的文件夹,运行下面的生成语句。
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
参考文章:https://www.cnblogs.com/smileberry/p/4145872.html