zoukankan      html  css  js  c++  java
  • 【串线篇】Mybatis拓展之MBG

    MBG-逆向工程

    一、介绍

    MBG:MyBatis Generator:代码生成器;

    MyBatis官方提供的代码生成器;帮我们逆向生成;

     

    正向:

    table----javaBean---BookDao---dao.xml---xxx

    逆向工程:

    根据数据表table,逆向分析数据表,自动生成javaBean---BookDao---dao.xml---xxx


    二、配置

    1、导包:mbg的核心包

    ….. mybatis-generator-core-1.3.2.jar

    2、编写mbg.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>
    
        <!--
        MyBatis3Simple:基础班CRUD
        MyBatis3:复杂版CRUD
         -->
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
            <!-- jdbcConnection:指导连接到哪个数据库 -->
            <jdbcConnection
    
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/mybatis_0325"
    
                userId="root"
    
                password="123456">
            </jdbcConnection>
    
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- javaModelGenerator:生成pojo
    
            targetPackage:生成的pojo放在哪个包
            targetProject:放在哪个工程下
            -->
            <javaModelGenerator targetPackage="com.atguigu.bean"
                targetProject=".src">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <!--sqlMapGenerator:sql映射文件生成器;指定xml生成的地方  -->
            <sqlMapGenerator targetPackage="com.atguigu.dao"
                targetProject=".conf">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
    
            <!-- javaClientGenerator:dao接口生成的地方 -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="com.atguigu.dao"
    
                targetProject=".src">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
    
            <!-- table:指定要逆向生成哪个数据表
            tableName="t_cat":表名
            domainObjectName="":这个表对应的对象名
             -->
            <table tableName="t_cat" domainObjectName="Cat"></table>
            <table tableName="t_employee" domainObjectName="Employee"></table>
            <table tableName="t_teacher" domainObjectName="Teacher"></table>
    
        </context>
    </generatorConfiguration>

    三、测试

    public class MBGTest {
    
        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);
            System.out.println("生成ok了!");
        }
    
    }
  • 相关阅读:
    Django框架第九篇--Django和Ajax、序列化组件(serializers)、自定义分页器、模型表choice参数
    Django框架之第五篇(模板层) --变量、过滤器(|)、标签(% %)、自定义标签、过滤器、inclusion_tag,模板的继承、模板的注入、静态文件
    Django框架学习易错和易忘点
    守护线程
    Thread其他属性和方法
    进程与线程的区别
    开启线程
    关闭屏幕输出分屏显示
    生产者消费者模型
    队列
  • 原文地址:https://www.cnblogs.com/yanl55555/p/11936872.html
Copyright © 2011-2022 走看看