MyBatis中,可以使用Generator自动生成代码,包括DAO层、 MODEL层 、MAPPING SQL映射文件。

第一步:
配置好自动生成代码所需的XML配置文件,例如(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:数据库的JDBC驱动-->
  <classPathEntry location="F:\Jansun\workspace\wicketAjax\ojdbc14-10.2.0.1.jar" />
 
  <context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 去除自动生成的注释 -->
<commentGenerator>
 <property name="suppressAllComments" value="true" />
</commentGenerator>
 
    <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
        connectionURL="jdbc:oracle:thin:@10.87.61.233:1521:orcl11g"
        userId="ibis"
        password="ibis">
    </jdbcConnection>
 
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
 
<!-- targetProject:自动生成代码的位置 -->
    <javaModelGenerator targetPackage="com.test.model" targetProject="F:\Jansun\workspace\wicketAjax\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
 
    <sqlMapGenerator targetPackage="com.test.mapping"  targetProject="F:\Jansun\workspace\wicketAjax\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
 
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao"  targetProject="F:\Jansun\workspace\wicketAjax\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
 
<!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
    <table schema="ibis" tableName="ibis_info" domainObjectName="Ibis" >
      
    </table>
 
  </context>
  
</generatorConfiguration>
 
第二步:下载mybatis-generator-core-1.3.1.jar包。
 
第三步:进入XML配置文件(generator.xml)所在的的目录并执行命令:
 
java -jar F:\mybatis-generator-core-1.3.1\lib\mybatis-generator-core-1.3.1.jar -configfile genrator.xml -overwrite
 
(PS:F:\mybatis-generator-core-1.3.1\lib\mybatis-generator-core-1.3.1.jar,是你所下载的mybatis-generator-core-1.3.1.jar包所在的目录
//--------------------------------------------------------------------------------------
 

Mybatis-generator-------------Mybatis的代码生成器

使用Hibernate时, 可以很方便的生成model,dao,和映射配置文件.在mybatis里, 也有生成器,即mybatis generator, 简称MBG. 下面为大家介绍一下MBG的使用.

Mybatis-generator的思想是编写一个配置文件,然后运行该配置文件,就会得到相应的java代码。那么重点就是这个配置文件,配置文件中都需要些什么呢?必须包括的有数据库的datasource,Mybatis的版本信息,至少一个的数据表,还有是需要生成的代码所在的包的信息。

1.下载类包

下载mybatis-generator-core-1.3.1-bundle.zip之后, 解压得到mybatis-generator-core-1.3.1.jar, 即生成器的jar包, 将mybatis-3.0.6.jar和mybatis-generator-core-1.3.1.jar加入到项目的lib里, 然后编写一个生成器的配置文件generatorConfig.xml.

2. mybatis-generator的配置文件

mybatis-generator最重要的就是一个配置文件,配置文件中配置了生成的.dao 与pojo生成的package路径等。

介绍一下genernatorConfig.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration 
 
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 
 
 
   
 
<generatorConfiguration
 
    <properties resource="com/yuan/mybatis/mbg/util/generatorConfig.properties" /> 
 
    <classPathEntry location="${classPath}" /> 
 
    <context id="MBG" targetRuntime="MyBatis3" defaultModelType="conditional"
 
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> 
 
        <plugin type="org.mybatis.generator.plugins.MapperConfigPlugin"
 
            <property name="fileName" value="GeneratedMapperConfig.xml" /> 
 
            <property name="targetPackage" value="com.yuan.mybatis.mbg.util" /> 
 
            <property name="targetProject" value="${targetProject}" /> 
 
        </plugin
 
        <commentGenerator
 
            <property name="suppressAllComments" value="true" /> 
 
        </commentGenerator
 
        <jdbcConnection driverClass="${driverClass}" 
 
            connectionURL="${connectionURL}" userId="${userId}" password="${password}"
 
        </jdbcConnection
 
        <javaTypeResolver
 
            <property name="forceBigDecimals" value="false" /> 
 
        </javaTypeResolver
 
   
 
        <javaModelGenerator targetPackage="${modelPackage}" 
 
            targetProject="${targetProject}"
 
            <property name="enableSubPackages" value="true" /> 
 
        </javaModelGenerator
 
   
 
        <sqlMapGenerator targetPackage="${sqlMapperPackage}" 
 
            targetProject="${targetProject}"
 
            <property name="enableSubPackages" value="true" /> 
 
        </sqlMapGenerator
 
   
 
        <javaClientGenerator type="XMLMAPPER" 
 
            targetPackage="${daoMapperPackage}" targetProject="${targetProject}"
 
            <property name="enableSubPackages" value="true" /> 
 
        </javaClientGenerator
 
        <table schema="minghan" tableName="sys_group" domainObjectName="Group"
 
            <generatedKey column="groupId" sqlStatement="mysql" 
 
                identity="true" /> 
 
        </table
 
        <table schema="minghan" tableName="sys_admin" domainObjectName="Admin" 
 
            enableCountByExample="false" enableUpdateByExample="false" 
 
            enableDeleteByExample="false" enableSelectByExample="false" 
 
            selectByExampleQueryId="false"
 
        </table
 
   
 
    </context
 
</generatorConfiguration>

其中的properties元素里引用了一个generatorConfig.properties配置文件是为了方便移植的时候,只需修改resource的路径值和generatorConfig.properties里的值即进行生成操作.

generatorConfig.properties代码如下:

Properties代码  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classPath=D\:/ylink/myeclipse/MBG/lib/mysql-connector-java-5.0.7-bin.jar 
 
targetProject=D\:/ylink/myeclipse/MBG/src 
 
driverClass=com.mysql.jdbc.Driver 
 
connectionURL=jdbc\:mysql\://127.0.0.1\:3306/minghan?useUnicode\=true&amp;characterEncoding\=UTF-8 
 
userId=root 
 
password=root 
 
modelPackage=com.yuan.mybatis.mbg.model 
 
sqlMapperPackage=com.yuan.mybatis.mbg.model.mapper 
 
daoMapperPackage=com.yuan.mybatis.mbg.dao.mapper

其中, targetProject是工程的源代码存放位置

classPath是数据库驱动包的位置, modelPackage是存放model实体的包, 对应数据库表, sqlMapperPackage是存放实体映射的XML配置文件, daoMapperPackage是存放Mapper接口的地方.

注意: 要事先建立这些包才可以生成.

如果不想生成mybatis的注释信息, 可以将commentGenerator里的suppressAllComments的值设为true,

table元素对应数据库表.

<table schema="minghan" tableName="sys_admin" domainObjectName="Admin"

enableCountByExample="false" enableUpdateByExample="false"

enableDeleteByExample="false" enableSelectByExample="false"

selectByExampleQueryId="false">

</table>

schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类, 如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true, 这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时, 就不会生成对应的Example类了.

3.运行

配置文件配好了之后,进行生成代码, 既可以使用命令的方式, 也可以自已写一个带main的类来运行, 下面提供了一个带main的运行类

MyBatisGeneratorTool.java

Java代码  

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    package com.yuan.mybatis.mbg.util; 
       
     
    import java.io.File; 
     
    import java.io.IOException; 
     
    import java.sql.SQLException; 
     
    import java.util.ArrayList; 
     
    import java.util.List; 
     
       
     
    import org.mybatis.generator.api.MyBatisGenerator; 
     
    import org.mybatis.generator.config.Configuration; 
     
    import org.mybatis.generator.config.xml.ConfigurationParser; 
     
    import org.mybatis.generator.exception.InvalidConfigurationException; 
     
    import org.mybatis.generator.exception.XMLParserException; 
     
    import org.mybatis.generator.internal.DefaultShellCallback; 
     
       
     
    public class MyBatisGeneratorTool { 
     
        public static void main(String[] args) { 
     
            List<String> warnings = new ArrayList<String>(); 
     
            boolean overwrite = true
     
            String genCfg = "/generatorConfig.xml"; //src的一级目录下 
     
            File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile()); 
     
            ConfigurationParser cp = new ConfigurationParser(warnings); 
     
            Configuration config = null
     
            try
     
                config = cp.parseConfiguration(configFile); 
     
            } catch (IOException e) { 
     
                e.printStackTrace(); 
     
            } catch (XMLParserException e) { 
     
                e.printStackTrace(); 
     
            
     
            DefaultShellCallback callback = new DefaultShellCallback(overwrite); 
     
            MyBatisGenerator myBatisGenerator = null
     
            try
     
                myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 
     
            } catch (InvalidConfigurationException e) { 
     
                e.printStackTrace(); 
     
            
     
            try
     
                myBatisGenerator.generate(null); 
     
            } catch (SQLException e) { 
     
                e.printStackTrace(); 
     
            } catch (IOException e) { 
     
                e.printStackTrace(); 
     
            } catch (InterruptedException e) { 
     
                e.printStackTrace(); 
     
            
     
        
     
    }

运行此类即可生成对应于数据库表的实体类, Mapper配置文件, Mapper接口, 生成后可以测试其可用性.

使用命令的方式运行:

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

4.其他学习网址推荐

推荐一个比较好的mybatis-generator的教学网址:

http://www.cnblogs.com/harrychinese/archive/2012/01/09/learn_mybatis.html

MyBatis使用Generator自动生成代码的更多相关文章

  1. 【Mybatis】MyBatis之Generator自动生成代码(九)

    MyBatis Generator 简介 MyBatis Generator 连接数据库表并生成MyBatis或iBatis文件.这有助于最大限度地减少使用MyBatis时为数据库文件创建简单CRUD ...

  2. Mybatis的generator自动生成代码

    mybatis-generator有三种用法:命令行.ide插件.maven插件.本次使用maven生成 环境:IDEA,mysql8,maven (1):新建项目,本次以SpringBoot项目为例 ...

  3. SpringBoot 添加mybatis generator 自动生成代码插件

    自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...

  4. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  5. idea中mybatis generator自动生成代码配置 数据库是sqlserver

    好长时间没有写博客了,最近公司要用java语言,开始学习java,属于初学者,今天主要记录一下mybatis generator自动生成代码,首先在如下图的目录中新建两个文件,如下图 generato ...

  6. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  7. IDEA Maven Mybatis generator 自动生成代码

    IDEA Maven Mybatis generator 自动生成代码 一.安装配置maven以及在Idea中配置maven 安装过程步骤可以看上面的博文,里面介绍得很详细. 二.建数据表 DROP ...

  8. IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)

    IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...

  9. generator自动生成代码

    idea设置generator自动生成代码: http://blog.csdn.net/sunny243788557/article/details/45166397

随机推荐

  1. VS 2012 插件卸载(删除自己安装的插件)

    给VS 装了一个插件,装完之后感觉别扭,所以想卸载,[工具]--> [扩展和更新]-->[找到想要卸载的插件点击一下就会出现禁用或卸载]

  2. Mac OS X 快捷键(完整篇)

    不少朋友提出要求,希望有个「高质量」的列表.其实这样的资源真是太多,平果官网就有 快捷键文档(多国语言版本).于是花了20分钟,浏览了一些网站,整理了点资源放过来供大家参考. 快捷键是通过按下键盘上的 ...

  3. node.js NPM 使用

    n=NPM是一个Node包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准.有了NPM,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.npms安装: 下载npm源 ...

  4. InputStream和OutputStream

    1.在java中stream代表一种数据流(源),javaio的底层数据元,---(想像成水龙头) 2.任何有能力产生数据流(源)的javaio对象就可以看作是一个InputStream对象既然它能产 ...

  5. Linux下 fcntl 函数用法说明

    功能描述:根据文件描述词来操作文件的特性. 文件控制函数         fcntl -- file control LIBRARY         Standard C Library (libc, ...

  6. omnibus方式部署gitlab

    omnibus方式部署gitlab Posted on 2015 年 1 月 10 日   4233 Views 这几天折腾搭建git服务器,选择了比较流行的gitlab,一开始就直奔一键安装脚本去了 ...

  7. Struts2之自定义局部类型转换器、全局类型转换器

    Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器  如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用 ...

  8. kobox: key_proc.c -v1 怎样使用proc文件系统调试驱动

    使用proc文件系统能够非常方便调试驱动.查看驱动中的一些数据 平台:TQ2440 系统版本号: root@ubuntu:/mnt/shared/kobox# uname -a Linux ubunt ...

  9. grep -v grep 代表在查询的最终结果中去掉grep命令本身

    grep -v grep 代表在查询的最终结果中去掉grep命令本身

  10. JavaScript创建对象的模式

    /** * Created by W_YH on 2016/3/14. */ /* 对象的创建方式 */ //------->第一种创建方式------创建Object的实例 var perso ...