步骤如下:
1、创建一个Maven工程testMBG。
(2)编写pom.xml文件添加依赖
复制代码
<?xml version="1.0" encoding="UTF-8"?> <!--指定特定数据库的jdbc驱动jar包的位置--> <classPathEntry location="${jdbc.driverLocation}"/> <context id="mysql" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mydb?useSSL=true" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.test.model" targetProject="\SpringDemo\testMBG\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.test.xml" targetProject="\SpringDemo\testMBG\src\main\java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="\SpringDemo\testMBG\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table schema="mydb" tableName="student" domainObjectName="Student" > </table> </context> 复制代码 (3)在resource目录下增加generator.properties文件配置数据库驱动jdbc.driverLocation=C:\Users\yinwe\.m2\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar jdbc.driverClass=com.mysql.jdbc.Driver jdbc.connectionURL=jdbc:mysql://localhost:3306/mydb?useSSL=false jdbc.userId=root jdbc.password=123456 (4)在resource目录下增加generatorConfig.xml进行generator相关配置
复制代码
<?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"> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.test.model" targetProject="\SpringDemo\testMBG\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.test.xml" targetProject="\SpringDemo\testMBG\src\main\java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="\SpringDemo\testMBG\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table schema="mydb" tableName="student" domainObjectName="Student" > </table> 复制代码 (5)点击Run,点击Edit Configuration,点击+,双击Maven。出现如下页面:在命令行输入mybatis-generator:generate,点击OK。
(6)运行Maven的generator。
(7)运行完成信息
(8)生成的目录及文件 转载自博客园 作者:yinweifeng 原文地址:https://www.cnblogs.com/yinweifeng/p/11283210.html