首先需要几个jar包:
freemarker-2.3.23.jar log4j-1.2.16.jar mybatis-3.2.3.jar mybatis-generator-core-1.3.2.jar mysql-connector-java-5.1.28-bin.jar ojdbc14.jar
这些jar包网上都有下载的地方
xml配置文件: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> <!-- 生成复杂的xml文件 --> <context id="MySQLContext" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <!-- 配置前置分隔符和后置分隔符 --> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- 配置注释信息 --> <commentGenerator> <!-- 不生成注释 --> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> <property name="addRemarkComments" value="true"/> </commentGenerator> <!-- 数据库连接配置 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/logisticsDB" userId="root" password="123456"> </jdbcConnection> <!-- targetPackage:生成实体类存放的包名, targetProject:指定目标项目路径,可以使用相对路径或绝对路径 --> <javaModelGenerator targetPackage="com.entity" targetProject="src"> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 配置SQL映射器Mapper.xml文件的属性 --> <sqlMapGenerator targetPackage="com.dao" targetProject="src"/> <!-- type="XMLMAPPER":所有的方法都在XML中,接口调用依赖XML文件 --> <javaClientGenerator targetPackage="com.dao" type="XMLMAPPER" targetProject="src"/> <!-- 生成所有表的映射 --> <table tableName="%"></table> </context> </generatorConfiguration>
Java:Generator类
package generator; import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; 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.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; public class Generator { /* * targetRuntime="MyBatis3Simple", 不生成Example */ public void generateMyBatis() { // MBG执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); // 当生成的代码重复时,覆盖原代码 boolean overwrite = true; String generatorFile = "/generatorConfig.xml"; // String generatorFile = "/generator/generatorConfigExample.xml"; // 读取MBG配置文件 InputStream is = Generator.class.getResourceAsStream(generatorFile); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config; try { config = cp.parseConfiguration(is); DefaultShellCallback callback = new DefaultShellCallback(overwrite); // 创建MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); // 执行生成代码 myBatisGenerator.generate(null); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } for (String warning : warnings) { System.out.println(warning); } } public static void main(String[] args) { Generator generator = new Generator(); generator.generateMyBatis(); } }