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 -->
<context id="infoGuardian">
<!-- 注释 -->
<commentGenerator >
<property name="suppressAllComments" value="true"/><!-- 生成代码的时候是否生成注释,true是取消注释,false会生成注释 -->
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/sms"
userId="root"
password="root" />
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 默认为false,可以把数据库中的decimal以及numeric类型解析为Integer,为true时会解析为java.math.BigDecimal) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.sms.pojo"
targetProject=".\src" >
<!-- 是否在当前路径下新加一层schema,如果为fase路径com.sms.pojo, 为true:com.sms.pojo.[schemaName] 这个情况主要是oracle中有,mysql中没有schema -->
<property name="enableSubPackages" value="false"/>
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="com.sms.dao"
targetProject=".\src" >
<!-- 是否在当前路径下新加一层schema,如果为fase路径com.sms.dao, 为true:com.shop.dao.[schemaName] 这个情况主要是oracle中有,mysql中没有schema -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="com.sms.dao"
targetProject=".\src" type="XMLMAPPER" >
<!-- 是否在当前路径下新加一层schema,如果为fase路径com.shop.dao, 为true:com.shop.dao.[schemaName] 这个情况主要是oracle中有,mysql中没有schema -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!--有几个表生成配置几个 domainObjectName是对应类名-->
<!-- 配置表信息 -->
<table schema="" tableName="student"
domainObjectName="Student" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
<!-- 配置表信息 -->
<table schema="" tableName="teacher"
domainObjectName="Teacher" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
pom.xml导入依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
运行方法
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("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);
}