官方文档:http://mybatis.org/generator/.
github:https://github.com/mybatis/generator.
1、首先引入依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
2、创建配置文件mbg.xml
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--配置数据库-->
<context id="simple" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
userId="root"
password="123456"/>
<!--指定javabean生成的位置-->
<javaModelGenerator targetPackage="com.blizzard.bean" targetProject="./src/main/java"/>
<!--指定sql映射文件生成的位置-->
<sqlMapGenerator targetPackage="com.blizzard.dao" targetProject="./src/main/java"/>
<!--指定dao接口生成的位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.blizzard.dao" targetProject="./src/main/java"/>
<!--指定每个表的生成策略-->
<table tableName="tbl_emp" domainObjectName="Employee" />
<table tableName="tbl_dept" domainObjectName="Department" />
</context>
</generatorConfiguration>
3、创建代码,生成
public class MBGTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.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);
}
}
最后在附上一个配置详解: 配置详解.