mybatis逆向工程配置
MyBatis Generator: 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
1、 导入逆向工程的jar包
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
2、编写MBG的配置文件generatorConfig.xml
<!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="dbconfig.properties"/>
<context id="simple" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}"
userId="${jdbc.username}" password="${jdbc.password}">
</jdbcConnection>
<javaModelGenerator targetPackage="com.pqy.ssm.pojo" targetProject="maven_ssm/src/main/java"/>
<sqlMapGenerator targetPackage="com.pqy.ssm.mapper" targetProject="maven_ssm/src/main/resources"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.pqy.ssm.mapper" targetProject="maven_ssm/src/main/java"/>
<table tableName="item" domainObjectName="Item"/>
</context>
</generatorConfiguration>
3、测试
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("maven_ssm/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);
System.out.println("生成成功!");
}
|