本文博客地址:http://blog.youkuaiyun.com/soonfly/article/details/64499423 (转载请注明出处)
mybatis官方提供了一个逆向工程包,可以针对数据库表自动生成mybatis执行所需要的Pojo、Mapper xml文件、Mapper Interface接口文件。
mybatis-generator有很多种用法:命令行、eclipse/IDEA、Maven插件,其使用原理完全一样。
无论哪种方式,首先要准备两个组件包:mybatis-generator-core-1.X.X.jar 和MySQL-connector-Java-5.X.XX.jar (点击下载两个组件)
命令行方式
从这个入手,因为最方便。
1、新建任意目录(D:\A-TWM\Mybatis),把两个组件拷入目录。
2、新建配置文件,命名:config.xml
补充:下载好的jar包里面有帮助文档,打开后里面有配置文件的模板。
config.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>
<!-- 数据库驱动包位置 -->
<classPathEntry location="D:\A-TWM\Mybatis\mysql-connector-java-5.1.26-bin.jar" />
<context id="sqlGenerate" targetRuntime="MyBatis3">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/tangwenmingdb?characterEncoding=utf8"
userId="root" password="root">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成Pojo包名和位置 -->
<javaModelGenerator targetPackage="twm.mybatisdemo.pojo"
targetProject="D:\A-TWM\Mybatis\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成Mapper映射XML文件位置 -->
<sqlMapGenerator targetPackage="twm.mybatisdemo.mapper"
targetProject="D:\A-TWM\Mybatis\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Mapper接口文件位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="twm.mybatisdemo.mapper" targetProject="D:\A-TWM\Mybatis\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->
<!-- tableName:要生成的表名
domainObjectName:生成后的实例名
enableCountByExample:Count语句中加入where条件查询,默认为true开启
enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
-->
<table tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false" />
<table tableName="category" />
<table tableName="order"/>
<table tableName="product"/>
<table tableName="order_detail"/>
</context>
</generatorConfiguration>
如果table里边不配置property,默认将所有字段逆向生成为类属性。
如果有些字段并不想生成为类属性,可以用ignoreColumn标签:
<ignoreColumn column="FRED" />//忽略字段
还可以指定逆向生成时,字段到属性的转换对应关系
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。
3、通过cmd打开命令窗口
运行:java -jar mybatis-generator-core-1.3.2.jar -configfile config.xml -overwrite
出现MyBatis Generator finished successfully.表示运行成功,将指定生成位置(这里是src)的源码拷入工作项目中即可。
Eclipse方式
1、新建工程、将组件和将配置文件config.xml放到对应的目录
2、在main函数中写代码运行
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);
}
3、以application的方式运行就可以了
本文博客地址:http://blog.youkuaiyun.com/soonfly/article/details/64499423 (转载请注明出处)