一、下载相关的jar包并导入依赖
1.1、jar包可以从maven仓库中下载:https://mvnrepository.com/
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency>
注意:同时也要加上以下依赖:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.10</version> <scope>runtime</scope> </dependency>
<?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="database.properties"/> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="${driverClassName}" connectionURL="${url}" userId="${user}" password="${password}"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="com.guigu.chenmengfan.smbms.pojo" targetProject=".srcmainjava"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.guigu.chenmengfan.smbms.mapper" targetProject=".srcmainjava"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.guigu.chenmengfan.mapper" targetProject=".srcmainjava"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 指定数据库表 --> <table schema="" tableName="smbms_user" domainObjectName="User"> <property name="useActualColumnNames" value="true"/> </table> <table schema="" tableName="smbms_role" domainObjectName="Role"> <property name="useActualColumnNames" value="true"/> </table> <table schema="" tableName="smbms_provider" domainObjectName="Provider"> <property name="useActualColumnNames" value="true"/> </table> <table schema="" tableName="smbms_bill" domainObjectName="Bill"> <property name="useActualColumnNames" value="true"/> </table> <table schema="" tableName="smbms_address" domainObjectName="Address"> <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration>
注意:
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=UTF-8 user=root password=123
3.1、新建GeneratorSqlmap
package com.guigu.chenmengfan.smbms; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @author cmf * @version 1.0 */ public class GeneratorSqlmap { public void generator() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; // 指定 逆向工程配置文件 File configFile = new File("D:\InCommonUse\Guigu_altogether\Guigu\S3\code\day81_code\src\main\resources\generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
END
为了不错过每天的见面,请记得点击一下【关注】啊~
作者:javagril,00后女生,一个IT界冉冉升起的新星,想带你遨游缤纷多彩的编程世界。