mybatis逆向工程工具类的使用---mybatis generator
项目结构
配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <generatorConfiguration> 6 7 <context id="DB2Tables" targetRuntime="MyBatis3"> 8 <commentGenerator> 9 <property name="suppressAllComments" value="true" /> 10 </commentGenerator> 11 <!-- 配置数据库连接 --> 12 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 13 connectionURL="jdbc:mysql://localhost:3306/ssm_crud" userId="root" 14 password="root"> 15 </jdbcConnection> 16 17 <javaTypeResolver> 18 <property name="forceBigDecimals" value="false" /> 19 </javaTypeResolver> 20 21 <!-- 指定javaBean生成的位置 --> 22 <javaModelGenerator targetPackage="com.shao.demo.entity" 23 targetProject=".srcmainjava"> 24 <property name="enableSubPackages" value="true" /> 25 <property name="trimStrings" value="true" /> 26 </javaModelGenerator> 27 28 <!--指定sql映射文件生成的位置 --> 29 <sqlMapGenerator targetPackage="mapper" targetProject=".srcmain esources"> 30 <property name="enableSubPackages" value="true" /> 31 </sqlMapGenerator> 32 33 <!-- 指定dao接口生成的位置,mapper接口 --> 34 <javaClientGenerator type="XMLMAPPER" 35 targetPackage="com.shao.demo.dao" targetProject=".srcmainjava"> 36 <property name="enableSubPackages" value="true" /> 37 </javaClientGenerator> 38 39 40 <!-- table指定每个表的生成策略 --> 41 <table tableName="user" domainObjectName="User"></table> 42 <!-- <table tableName="tbl_emp" domainObjectName="Employee"></table> 43 <table tableName="tbl_dept" domainObjectName="Department"></table> --> 44 </context> 45 </generatorConfiguration>
测试工具类编写
1 package com.shao.test; 2 3 import java.io.File; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.mybatis.generator.api.MyBatisGenerator; 8 import org.mybatis.generator.config.Configuration; 9 import org.mybatis.generator.config.xml.ConfigurationParser; 10 import org.mybatis.generator.internal.DefaultShellCallback; 11 12 public class MBGTest { 13 14 public static void main(String[] args) throws Exception { 15 List<String> warnings = new ArrayList<String>(); 16 boolean overwrite = true; 17 File configFile = new File("mbg.xml"); 18 ConfigurationParser cp = new ConfigurationParser(warnings); 19 Configuration config = cp.parseConfiguration(configFile); 20 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 21 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, 22 callback, warnings); 23 myBatisGenerator.generate(null); 24 } 25 }