MyBatis generator - 简单使用(1)

一、官方相关文档

  • github:https://github.com/mybatis/generator
  • 文档:http://mybatis.org/generator/index.html

二、使用

2.1 添加依赖

pom.xml新增:

  • mybatis-generator-core,目前最新版本是1.4.0
  • 数据库是mysql的话,再加个mysql依赖
<!-- mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.36</version>
</dependency>

<!-- mybatis generator -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>

2.2 配置

官方示例:http://mybatis.org/generator/configreference/xmlconfig.html

简单的配置如下:

  • mysql数据源(db2…)
  • 插件配置,如分页插件
  • 注释等属性配置
  • 配置Bean、Dao、Mapper.xml包名和存放目录
  • 表的配置,表名必填,其余字段选填,如实体类名,是否生成xxxByExample查询等

示例:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "src/main/org/mybatis/generator/config/xml/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
	<!-- 上下文,可配置多个,id唯一 -->
    <context id="context1" targetRuntime="MyBatis3">

        <!--<plugin type="org.mybatis.org.mybatis.generator.plugins.MySqlPaginationPlugin"/>-->

		<!-- 不生成注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 2.修改mysql 数据源 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test"
                        userId="root" password="root"/>

        <!-- 3.修改Bean、Dao、xml存放目录 -->
        <!-- 生成po(bean)类,targetProject指定生成java文件位置,相对位置:当前项目下,绝对路径:如/Users/test/java/test/src/main/java,可以生成到指定项目中 -->
        <javaModelGenerator targetPackage="com.momo.test.biz.trade.po" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成 xxxMapper.xml -->
        <sqlMapGenerator targetPackage="com.momo.test.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成mapper(Dao),xxxMapper.java -->
        <javaClientGenerator targetPackage="com.momo.test.biz.order.mapper" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>


        <!-- 4.填写表名和实体类名 -->
        <!-- tableName:表名,domainObjectName:实体类名 -->
        <table tableName="product_item" domainObjectName="ProductItem"></table>
		
		<!-- 不生成Example -->
		<!--
		<table tableName="trade_product_item" domainObjectName="ProductItem" enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false"></table>
        -->
		
		<!-- tableName:表名,domainObjectName:实体类名 -->
        <table tableName="order" domainObjectName="Order">
            <property name="useActualColumnNames" value="true"/>
            <columnOverride column="DATE_FIELD" property="startDate" />
            <ignoreColumn column="FRED" />
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        </table>
        
    </context>
</generatorConfiguration>

2.3 生成

(1)生成bean、dao、xml:

public class MybatisGenerator {

    public static void main(String[] args) throws Exception {
		// 读取配置文件
		List<String> warnings = new ArrayList<>();
        InputStream configIn = MybatisGenerator.class.getResourceAsStream("mybatis-generator-cfg.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configIn);
        configIn.close();
        DefaultShellCallback callback = new DefaultShellCallback(overWrite);
        // 生成
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

(2)生成的文件如下所示:

  • 正常生成bean、dao、xml等文件

注:

  • 配置了不生成任何注释,所以所有bean、dao都没有注释的(主要是默认的注释不符合规范,可以通过自定义注释来解决)
  • 没配置分页插件
  • 生成 OrderExample、ProductItemExample两个实体类,以及对应的xxxMapper.java和xxxMapper.xml也会多出一些countByExample()、selectByExample()等方法
    • 如果需要用到这类封装的动态查询,可以保留
    • 不需要的话,修改配置文件中的<table>标签中相关属性即可
      <table tableName="product_item" domainObjectName="ProductItem" 
      		enableCountByExample="false" enableDeleteByExample="false"
      		enableSelectByExample="false" enableUpdateByExample="false">
      </table>
    

生成的文件如下图所示:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值