1、创建javaSE项目mybatis-generator,导入以下jar包,项目结构如图所示:
2、在类路径下添加log4j.properties文件
1 log4j.rootLogger=DEBUG, Console 2 #Console 3 log4j.appender.Console=org.apache.log4j.ConsoleAppender 4 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 5 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n 6 log4j.logger.java.sql.ResultSet=INFO 7 log4j.logger.org.apache=INFO 8 log4j.logger.java.sql.Connection=DEBUG 9 log4j.logger.java.sql.Statement=DEBUG 10 log4j.logger.java.sql.PreparedStatement=DEBUG
3、创建GeneratorSqlmap类
1 import java.io.File; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 import org.mybatis.generator.api.MyBatisGenerator; 6 import org.mybatis.generator.config.Configuration; 7 import org.mybatis.generator.config.xml.ConfigurationParser; 8 import org.mybatis.generator.internal.DefaultShellCallback; 9 10 public class GeneratorSqlmap { 11 public void generator() throws Exception{ 12 List<String> warnings = new ArrayList<String>(); 13 boolean overwrite = true; 14 //指定 逆向工程的配置文件 15 File configFile = new File("generatorConfig.xml"); 16 ConfigurationParser cp = new ConfigurationParser(warnings); 17 Configuration config = cp.parseConfiguration(configFile); 18 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 19 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 20 myBatisGenerator.generate(null); 21 } 22 23 public static void main(String[] args) throws Exception { 24 try { 25 GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); 26 generatorSqlmap.generator(); 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 } 31 32 }
4、类路径下创建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> <!-- context元素用于指定生成一组对象的环境。targetRuntime:此属性用于指定生成的代码的运行时环境。MyBatis3:*这是默认值*--> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxx.xxx.xxx.xxx:3306/hfyz_spoc?useUnicode=true&characterEncoding=UTF-8" userId="root" password="xxxx"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer, 为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="true" /> </javaTypeResolver> <!-- targetProject:生成实体类的位置 --> <javaModelGenerator targetPackage="xxxxxx" targetProject="./src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="false" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="/mybatis" targetProject="./src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="xxxxxx" targetProject="./src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 和实体类对象的名称 字段名称默认会用驼峰命名方式 enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:去除自动生成的例子 --> <!-- 一张表对应一个table --> <table tableName="course_price" domainObjectName="CoursePrice" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <!-- 有些表的字段需要指定java类型 <table schema="" tableName=""> <columnOverride column="" javaType="" /> </table> --> </context> </generatorConfiguration>