使用官方网站的mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和mapper映射文件。

一、mapper生成配置文件

在generatorConfig.xml中配置mapper生成的详细信息,注意以下几点:

1、添加要生成的数据库表

2、po文件所在包路径

3、mapper文件所在包路径

示例:

 <?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>
<!--数据库驱动 -->
<!-- 如果IDE(eclipse或者idea) 项目里导入了jar包,那么就不需要配置了jar包的绝对路径了 <classPathEntry
location="e:/project/mybatis/lib/mysql-connector-java-5.0.8-bin.jar"/> -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!--数据库链接地址账号密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/travel" userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成Model类存放位置 -->
<javaModelGenerator targetPackage="po"
targetProject="src">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="src">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--生成Dao类存放位置 --> <javaClientGenerator type="XMLMAPPER"
targetPackage="mapper" targetProject="src">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!--生成对应表及类名 -->
<table tableName="cars" domainObjectName="Category"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
</table>
<table tableName="flights"></table>
<table tableName="hotels"></table>
<table tableName="reservations"></table>
<table tableName="customers"></table>
<!-- <table tableName="product_" domainObjectName="Product" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table> -->
</context>
</generatorConfiguration>

二、使用java类生成mapper文件

 package test;

 import java.io.File;
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.internal.DefaultShellCallback; public class GenerateMap { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("generateConfig.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); }
public static void main(String[] args) throws Exception {
try {
GenerateMap generatorSqlmap = new GenerateMap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
} } }

运行程序后,自动生成的po类和mapper文件:

Mybatis逆向工程使用方法的更多相关文章

  1. MyBatis逆向工程生成的Example类的方法总结

    很早之前就在项目开发中多次使用MyBatis逆向工程生成的Example类,但一直没有对其下的方法做一个简单的总结,现总结如下:一.mapper接口中的方法解析mapper接口中的部分常用方法及功能如 ...

  2. Mybatis逆向工程构建项目实例.

    2016/11/06更新: 因为有博友可能需要这份代码, 所以我就直接发到百度云上面和大家共享, 如果链接失效请大家留言提示即可.下载地址: http://pan.baidu.com/s/1i57E8 ...

  3. mybatis0212 mybatis逆向工程 (MyBatis Generator)

    1mybatis逆向工程 (MyBatis Generator) .1什么是mybatis的逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括生成 :mapper.xml.m ...

  4. 【JAVA - SSM】之MyBatis逆向工程的使用

    MyBatis逆向工程可以方便的从数据库中将表自动映射到JAVA POJO类,并同时生成Mapper.xml和Mapper接口,方便实用.下面介绍一下逆向工程的使用方法. 使用逆向工程,我们最好是新建 ...

  5. JAVAEE——Mybatis第二天:输入和输出映射、动态sql、关联查询、Mybatis整合spring、Mybatis逆向工程

    1. 学习计划 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If标签 b) Where标签 c) Sql片段 d) Foreach标签 3.关联查询 a) 一对 ...

  6. mybatis框架(7)---mybatis逆向工程

    mybatis逆向工程 ​ 逆向工程的目的就是缩减了我们的开发时间.所谓mybatis逆向工程,就是mybatis会根据我们设计好的数据表,自动生成pojo.mapper以及mapper.xml. 接 ...

  7. 学习笔记01(mybatis逆向工程)

    今天来看看一个常用的小功能,就是mybatis的逆向工程.(数据库是mysql) 什么是逆向工程呢?看名字就知道反方向的一个什么工程! 其实啊,如果是平常我们自己学习实践一些小项目的时候,应该是先瞎写 ...

  8. mybatis逆向工程,实现join多表查询,避免多表相同字段名的陷阱

    ​ mybatis逆向工程,实现join多表查询,避免多表相同字段名的陷阱 ​ 前言:使用 mybatis generator 生成表格对应的pojo.dao.mapper,以及对应的example的 ...

  9. IDEA创建一个Mybatis逆向工程

    Mybatis逆向工程简介: MyBatis Generator (MBG) 是一个Mybatis的代码生成器 MyBatis 和 iBATIS. 他可以生成Mybatis各个版本的代码,和iBATI ...

随机推荐

  1. 获取Linux磁盘分区的UUID

    在设置fstab自动挂载时,需要填写如下的信息: # <file system> <mount point> <type> <options> < ...

  2. Python的lambda函数与排序

    Python的lambda函数与排序 2010-03-02 15:02 2809人阅读 评论(0) 收藏 举报 lambdapythonlistlispclass工作   目录(?)[+]   前几天 ...

  3. 20160225.CCPP体系具体解释(0035天)

    程序片段(01):CircleList.h+CircleList.c+main.c 内容概要:环形链表 ///CircleList.h #pragma once #include <stdio. ...

  4. git 在一台机器上配置多个账户

    前提: 必须知道怎样配置git账户,请參考git官方教程:https://help.github.com/articles/generating-ssh-keys 这个教程能教你怎样生成ssh-key ...

  5. python generator iterator和iterable object

    1 iterable object list.dict.set.tuple.file(在每行上iterate)等都是iterable object,但是它们不是iterator.但是它们可以转换成it ...

  6. http使用代理

    直接上代码: HttpUrlConnection使用代理: private static HttpURLConnection getConnection(URL url, String method, ...

  7. Linux/Android——输入子系统input_event传递 (二)【转】

    本文转载自:http://blog.csdn.net/jscese/article/details/42099381 在前文Linux/Android——usb触摸屏驱动 - usbtouchscre ...

  8. regulator_get 调用过程【转】

    本文转载自:http://blog.csdn.net/u012719256/article/details/52083961 Touch panel DTS 分析(MSM8994平台,Atmel 芯片 ...

  9. 5 Application 对象

    5.1鸟瞰Application对象 5.2 必须了解的面向显示特性 5.2.1 使用ScreenUpdating改进和完善执行性能 代码清单5.1:实现屏幕更新的性能 '代码清单5.1: 实现屏幕更 ...

  10. HTML5中File

    一 File对象与FileList对象 当将input元素的type类型设置为file时,web页面上会显示一个选择文本按钮和一个文本显示框,单击文件按钮可以选择一个文件,文本显示框中会显示选中的文件 ...