Mybatis-Generator

通过Mybatis提供的工具,自动的通过表名生成对应的
mapper.xml,dao接口,Model类

MyBatis Generator官方介绍

MyBatis Generator (MBG) 可以通过以下方式运行:

从 命令提示符 使用 XML 配置文件
作为 Ant 任务 使用 XML 配置文件
作为 Maven Plugin
从另一个 Java 程序 使用 XML 配置文件

先使用两种方式:1,命令行方式。2,java程序。

1,命令行方式:

cmd命令:
java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml

java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml -overwrite

这里写图片描述

配置文件:mybatis_generator.xml
自动生成代码的jar包:mybatis-generator-core-1.3.2.jar
mysql连接jar包:mysql-connector-java-5.1.20.jar

mybatis_generator.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="mysql-connector-java-5.1.20.jar" />

    <context id="db2pojo" targetRuntime="MyBatis3">
 <!-- suppressAllComments 是否包含注释 -->
        <!-- suppressDate 是否生成日期 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
         </commentGenerator>

<!-- mysql 数据库 连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/leying?useUnicode=true&amp;"
            userId="root" password="123456">
        </jdbcConnection>
        <!--生成Model类存放位置 -->
        <!--代码路径:src/main/java/com/axure/entity 目录下 -->
        <javaModelGenerator targetPackage="main.java.com.axure.entity"
            targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--生成映射文件存放位置 -->
        <sqlMapGenerator targetPackage="main.resource.mybatis.sqlmapping"
            targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!--生成mapper类存放位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="main.java.com.axure.web.dao" targetProject="src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="movie" domainObjectName="Movie"
            enableCountByExample="false" 
            enableUpdateByExample="false" 
            enableDeleteByExample="false" 
            enableSelectByExample="false" 
            selectByExampleQueryId="false">

            <property name="useActualColumnNames" value="true" />
            <generatedKey column="id" sqlStatement="MySql" identity="true" />

        </table>

    </context>
</generatorConfiguration>

这里写图片描述

2,java 代码方式:

public class MybatisGenerator {

    public static void main(String[] args){

        try {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
//          D:\workspace\spring_leying\src\main\resources\mybatis
            // 加/ 表示从d盘找。 不加/ 表示从D:\workspace\spring_leying\路径下找
            File configFile = new File("src/main/resources/mybatis_generator/mybatis_generator.xml");
            System.out.println("path = "+ configFile.getAbsolutePath());
            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);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1,mysql连接jar是全路径
2,targetProject 和 targetPackage的写法
这里写图片描述

mybatis_generator.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:/workspace/spring_leying/src/main/resources/mybatis_generator/mysql-connector-java-5.1.20.jar" />

    <context id="db2pojo" targetRuntime="MyBatis3">

        <!-- suppressAllComments 是否包含注释 -->
        <!-- suppressDate 是否生成日期 -->
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- mysql 数据库 连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/leying?useUnicode=true&amp;"
            userId="root" password="123456">
        </jdbcConnection>
        <!--生成Model类存放位置 -->
        <javaModelGenerator targetPackage="com.axure.entity"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--生成映射文件存放位置 -->
        <sqlMapGenerator targetPackage="main.resources.mybatis.sqlmapping.movie"
            targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!--生成mapper类存放位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.axure.web.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="movie" domainObjectName="Movie"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">

            <property name="useActualColumnNames" value="true" />
            <generatedKey column="id" sqlStatement="MySql" identity="true" />

        </table>

    </context>
</generatorConfiguration>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值