- 导入jar包
- 将Generator.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:数据库的JDBC驱动的jar包地址 --> <!-- <classPathEntry location="mysql-connector-java-5.1.21.jar" /> --> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是;false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/smbms" userId="root" password="123"> </jdbcConnection> <!-- 是否将JDBC DECIMAL 和 NUMERIC 类型解析为 java.math.BigDecimal true:转换为BigDecimal;false:转换为Integer --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 自动生成模型层代码(实体类) targetPackage:实体类文件所在的包名 targetProject:实体类文件存放的物理路径 --> <javaModelGenerator targetPackage="com.pojo" targetProject="./src"> <!-- enableSubPackages:是否让schema(数据库用户名)作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 自动生成实现业务接口的xml映射文件 enableSubPackages:是否需要将数据库用户名追加到包名最后 --> <sqlMapGenerator targetPackage="com.dao" targetProject="./src"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 自动生成业务接口代码 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="./src"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- tableName:用于自动生成代码的数据库表; domainObjectName:对应于数据库表的javaBean类名 schema:数据库用户名 tableName:表名 domainObjectName:实体类名 --> <table tableName="smbms_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <!-- <table schema="jack" tableName="grade" domainObjectName="Grade" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> --> </context> </generatorConfiguration>
-
- 编写运行类
-
package com.test; import java.io.File; import java.util.ArrayList; import java.util.List; 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; public class TestGenerator { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("generator.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); System.out.println("success"); } }
-