zoukankan      html  css  js  c++  java
  • MyBatis逆向工程

    MyBatis逆向工程

    一、前言

      MyBatis Generator简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写

      官方文档地址:http://www.mybatis.org/generator/

      官方工程地址:https://github.com/mybatis/generator/releases

    二、MBG使用

      编写MBG的配置文件(重要几处配置):

      ①jdbcConnection配置数据库连接信息
      ②javaModelGenerator配置javaBean的生成策略
      ③sqlMapGenerator配置sql映射文件生成策略
      ④javaClientGenerator配置Mapper接口的生成策略
      ⑤table配置要逆向解析的数据表,tableName:表名,domainObjectName:对应的javaBean名

      运行代码生成器生成代码

      注意:
      ①Context标签
      ②targetRuntime=“MyBatis3“可以生成带条件的增删改查
      ③targetRuntime=“MyBatis3Simple“可以生成基本的增删改查
      ④如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问题。

      MBG配置文件

     1 <generatorConfiguration>
     2     <!--
     3         targetRuntime="MyBatis3Simple":生成简单版的CRUD
     4         MyBatis3:豪华版
     5     
     6      -->
     7   <context id="DB2Tables" targetRuntime="MyBatis3">
     8       <!-- jdbcConnection:指定如何连接到目标数据库 -->
     9     <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    10         connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
    11         userId="root"
    12         password="123456">
    13     </jdbcConnection>
    14 
    15     <!--  -->
    16     <javaTypeResolver >
    17       <property name="forceBigDecimals" value="false" />
    18     </javaTypeResolver>
    19 
    20     <!-- javaModelGenerator:指定javaBean的生成策略 
    21     targetPackage="test.model":目标包名
    22     targetProject="MBGTestProjectsrc":目标工程
    23     -->
    24     <javaModelGenerator targetPackage="me.mybatis.bean" 
    25             targetProject=".src">
    26       <property name="enableSubPackages" value="true" />
    27       <property name="trimStrings" value="true" />
    28     </javaModelGenerator>
    29 
    30     <!-- sqlMapGenerator:sql映射生成策略: -->
    31     <sqlMapGenerator targetPackage="me.mybatis.dao"  
    32         targetProject=".conf">
    33       <property name="enableSubPackages" value="true" />
    34     </sqlMapGenerator>
    35 
    36     <!-- javaClientGenerator:指定mapper接口所在的位置 -->
    37     <javaClientGenerator type="XMLMAPPER" targetPackage="me.mybatis.dao"  
    38         targetProject=".src">
    39       <property name="enableSubPackages" value="true" />
    40     </javaClientGenerator>
    41 
    42     <!-- 指定要逆向分析哪些表:根据表要创建javaBean -->
    43       <table tableName="tbl_dept" domainObjectName="Department"/>
    44       <table tableName="tbl_employee" domainObjectName="Employee"/>
    45   </context>
    46 </generatorConfiguration>

      生成器代码

     1 publicstaticvoidmain(String[] args) throwsException {
     2     List<String> warnings = newArrayList<String>();
     3     booleanoverwrite = true;
     4     File configFile = newFile("mbg.xml");
     5     ConfigurationParser cp = newConfigurationParser(warnings);
     6     Configuration config = cp.parseConfiguration(configFile);
     7     DefaultShellCallback callback = newDefaultShellCallback(overwrite);
     8     MyBatisGenerator myBatisGenerator = newMyBatisGenerator(config,
     9     callback, warnings);
    10     myBatisGenerator.generate(null);
    11 }

      测试查询:QBC风格的带条件查询

     1 @Test
     2 publicvoidtest01(){
     3     SqlSession openSession = build.openSession();
     4     DeptMapper mapper = openSession.getMapper(DeptMapper.class);
     5     DeptExample example = newDeptExample();
     6     //所有的条件都在example中封装
     7     Criteria criteria = example.createCriteria();
     8     //select id, deptName, locAdd from tbl_dept WHERE
     9     //( deptName like ? and id > ? )
    10     criteria.andDeptnameLike("%部%");
    11     criteria.andIdGreaterThan(2);
    12     List<Dept> list = mapper.selectByExample(example);
    13     for(Dept dept : list) {
    14         System.out.println(dept);
    15     }
    16 }    

    如果,您对我的这篇博文有什么疑问,欢迎评论区留言,大家互相讨论学习。
    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
    如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
    如果,您对我的博文感兴趣,可以关注我的后续博客,我是【AlbertRui】。

    转载请注明出处和链接地址,欢迎转载,谢谢!

  • 相关阅读:
    用脚本保存prefab
    如何在Unity 3D中掷骰子
    转发收藏【原创】浅谈UGUI的ETC1+A的纹理压缩方案总结
    Unity鼠标拖拽控制人物的左右旋转
    蛋哥的学习笔记之-基于Unity的Shader编程:X-1 音乐水波特效
    xlua中hotfix简单实用
    tolua调用C#中的静态类
    scut和unity之间收发请求返回
    scut服务器unity配置
    HTTP网络请求
  • 原文地址:https://www.cnblogs.com/albertrui/p/8487572.html
Copyright © 2011-2022 走看看