MybatisGenerator简单使用
官方文档:http://mybatis.org/generator/quickstart.html
这里的是Java+mysql
1.先导入maven依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
2.在项目下创建generator的配置文件mgb.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="MyBatis3Simple">
<!-- 不生成注释,这要配置在前边,不然可能报错 -->
<commentGenerator>
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
userId="root"
password="090057"
/>
<!-- 指定JavaBean生成的位置 targetPackage,生成在哪个项目targetProject-->
<javaModelGenerator targetPackage="com.xxx.crud.bean" targetProject="./src/main/java"/>
<!-- 指定sql映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"/>
<!-- 指定dao接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.crud.dao" targetProject="./src/main/java"/>
<!-- table指定每个表生成的策略,tableName数据库表名,domainObjectName生成的类名-->
<table tableName="tbl_emp" domainObjectName="Employee"></table>
<table tableName="tbl_dept" domainObjectName="Department"></table>
</context>
</generatorConfiguration>
3.在test类里写个方法运行就生成出来了
package com.xxx.crud.test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MGBTest {
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);
}
}