0.在Intellij IDEA创建maven项目
1. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件
<build> <finalName>xxx</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
2. 在maven项目下的src/main/resources 目录下建立名为 generatorConfig.xml的配置文件,作为mybatis-generator-maven-plugin 插件的执行目标,模板如下:
<?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> <!--导入属性配置 --> <properties resource="generator.properties"></properties> <!--指定特定数据库的jdbc驱动jar包的位置 --> <classPathEntry location="${jdbc.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!--jdbc的数据库连接 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection> <!--生成entity类存放位置--> <javaModelGenerator targetPackage="org.louis.hometutor.po" targetProject="src/main/java"> <!-- 是否对model添加 构造函数 --> <property name="constructorBased" value="true"/> <!-- 是否对类CHAR类型的列的数据进行trim操作 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="org.louis.hometutor.domain" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!--生成Dao类存放位置--> <javaClientGenerator targetPackage="com.foo.tourist.dao" targetProject="src/main/java" type="MIXEDMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="student" domainObjectName="Student"> <generatedKey column="id" sqlStatement="select uuid_short()" identity="false"/> <!-- 用来修改表中某个列的属性,MBG会使用修改后的列来生成domain的属性; column:要重新设置的列名; 注意,一个table元素中可以有多个columnOverride元素哈~ --> <columnOverride column="user_name"> <!-- 使用property属性来指定列要生成的属性名称 --> <property name="property" value="userName"/> <!-- javaType用于指定生成的domain的属性类型,使用类型的全限定名--> <property name="javaType" value="java.lang.String"/> <!-- jdbcType用于指定该列的JDBC类型--> <property name="jdbcType" value="VARCHAR"/> <!-- 参考table元素的delimitAllColumns配置,默认为false--> <property name="delimitedColumnName" value="true"/> </columnOverride> </table> <table tableName="teacher" domainObjectName="Teacher"> <generatedKey column="id" sqlStatement="select uuid_short()" identity="false"/> <!-- 用来修改表中某个列的属性,MBG会使用修改后的列来生成domain的属性; column:要重新设置的列名; 注意,一个table元素中可以有多个columnOverride元素哈~ --> <columnOverride column="user_name"> <!-- 使用property属性来指定列要生成的属性名称 --> <property name="property" value="userName"/> <!-- javaType用于指定生成的domain的属性类型,使用类型的全限定名--> <property name="javaType" value="java.lang.String"/> <!-- jdbcType用于指定该列的JDBC类型--> <property name="jdbcType" value="VARCHAR"/> <!-- 参考table元素的delimitAllColumns配置,默认为false--> <property name="delimitedColumnName" value="true"/> </columnOverride> </table> </context> </generatorConfiguration>
这里使用了外置的配置文件generator.properties,可以将一下属性配置到properties文件之中,增加配置的灵活性:
jdbc.driverLocation=D:\\myfolder\\maven_repository\\mysql\\mysql-connector-java\\8.0.15\\mysql-connector-java-8.0.15.jar jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.connectionURL=jdbc:mysql://localhost:3306/hxe_ant?useSSL=false&characterEncoding=UTF-8&serverTimezone=CTT&useAffectedRows=true&allowPublicKeyRetrieval=true jdbc.userId=root jdbc.password=root
项目目录如下:
3. 在Intellij IDEA添加一个“Run运行”选项,使用maven运行mybatis-generator-maven-plugin插件 :
之后弹出运行配置框,为当前配置配置一个名称,这里其名为"generator",然后在 “Command line” 选项中输入“mybatis-generator:generate -e”
这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题
如果添加成功,则会在run 选项中有“generator” 选项,如下:
点击运行,然后不出意外的话,会在控制台输出:
"C:\Program Files (x86)\Java\jdk1.8.0_73\bin\java.exe" -Dmaven.multiModuleProjectDirectory=D:\myfolder\code\ant-api -Dmaven.home=D:\myfolder\install\apache-maven-3.6.2 -Dclassworlds.conf=D:\myfolder\install\apache-maven-3.6.2\bin\m2.conf "-Dmaven.ext.class.path=D:\myfolder\install\IntelliJ IDEA 2019.2.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\myfolder\install\IntelliJ IDEA 2019.2.3\lib\idea_rt.jar=58980:D:\myfolder\install\IntelliJ IDEA 2019.2.3\bin" -Dfile.encoding=UTF-8 -classpath D:\myfolder\install\apache-maven-3.6.2\boot\plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version2019.2.3 -s D:\myfolder\install\apache-maven-3.6.2\conf\settings.xml -Dmaven.repo.local=D:\myfolder\maven_repository mybatis-generator:generate -e [INFO] Error stacktraces are turned on. [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.huixiaoer:ant-api:jar:1.0.0-SNAPSHOT [WARNING] 'dependencies.dependency.scope' for org.springframework.cloud:spring-cloud-dependencies:pom must be one of [provided, compile, runtime, test, system] but is 'import'. @ line 91, column 20 [WARNING] 'dependencies.dependency.scope' for org.springframework.boot:spring-boot-devtools:jar must be one of [provided, compile, runtime, test, system] but is 'true'. @ line 116, column 20 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] -----------------------< com.huixiaoer:ant-api >------------------------ [INFO] Building ant-api 1.0.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- mybatis-generator-maven-plugin:1.3.7:generate (default-cli) @ ant-api --- [INFO] Connecting to the Database [INFO] Introspecting table student [INFO] Introspecting table teacher [INFO] Generating Example class for table student [INFO] Generating Record class for table student [INFO] Generating Mapper Interface for table student [INFO] Generating SQL Map for table student [INFO] Generating Example class for table teacher [INFO] Generating Record class for table teacher [INFO] Generating Mapper Interface for table teacher [INFO] Generating SQL Map for table teacher [INFO] Saving file StudentMapper.xml [INFO] Saving file TeacherMapper.xml [INFO] Saving file StudentExample.java [INFO] Saving file Student.java [INFO] Saving file StudentMapper.java [INFO] Saving file TeacherExample.java [INFO] Saving file Teacher.java [INFO] Saving file TeacherMapper.java [WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete [WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.033 s [INFO] Finished at: 2019-10-31T10:40:32+08:00 [INFO] ------------------------------------------------------------------------
看到BUILD SUCCESS,则大功告成,如果有错误的话,由于添加了-e 选项,会把具体的详细错误信息打印出来的,根据错误信息修改即可
原文地址:https://www.cnblogs.com/liaojie970/p/7058543.html