mybatis-generator 代码自动生成工具(maven方式)

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,mybatis-gennerator插件帮我们自动生成mybatis所需要的dao、bean、mapper xml文件。

这里主要通过eclipse工具,来讲解实现;

1、建表语句

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

2、新建测试工程

选择maven工程

选择create a simple project就行

点击finish,测试项目就建完了

3、在maven配置文件pom.xml中添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>mybatis_generator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mybatis_generator</name>

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                    <version>3.3</version>
                </plugin>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <dependencies>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.35</version>
                        </dependency>
                    </dependencies>
                    <configuration>                         <!--配置文件的路径-->
                         <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

4、在resources下,创建一个generatorConfig.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>
    <context id="test" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
         <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
        <commentGenerator>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost/mytest" userId="root" password="123456">
            </jdbcConnection>
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should
                force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.test.pojo"
            targetProject="target">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="com.test.mapping"
            targetProject="target">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.test.dao" implementationPackage="com.test.dao.impl"  targetProject="target">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成哪些表 -->
        <table tableName="user" domainObjectName="user"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

5、下载maven依赖包,update project

这个要稍微等一下,需要时间~~~

6、执行mabatis-generator:generate命令,生成文件

在控制台 显示 build success,说明已经成功了:

7、在项目上F5刷新,target目录下呢就会出现对应的文件

这是想要的文件自动生成了。

时间: 2024-08-01 22:46:53

mybatis-generator 代码自动生成工具(maven方式)的相关文章

mybaits generator 代码自动生成工具使用

mybaits generator 代码自动生成工具使用MyBatis Generator (MBG) 是一个Mybatis的代码生成器,它可以帮助我们根据数据库中表的设计生成对应的实体类,xml Mapper文件,接口以及帮助类(也就是我们可以借助该类来进行简单的CRUD操作),这样就避免了我们每使用到一张表的数据就需要手动去创建对应的类和xml文件,这就帮我们节约了大量的时间去开发和业务逻辑有关的功能,但是如果对联合查询和存储过程您仍然需要手写SQL和对象. 生成方式主要有 Maven 和

Mybatis Generator代码自动生成(实体类、dao层、映射文件)

写了一段时间增删改查有点厌烦,自己找了下网上的例子鼓捣了下自动生成. 首先得有一个配置文件: generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"   

MyEclipse下安装MyBatis Generator代码反向生成工具

一.离线方式: 在http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/下载 features/ plugins/ 里面所有的jar包,新建一个mybatis-generator文件夹,把features跟plugins都丢到mybatis-generator文件夹中,把mybatis-generator文件夹移到D:\MyEclipse10_7\MyEclipse 10\dropins

Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展

Asp.net mvc 5 CRUD代码自动生成工具 -Visual Studio.net2013 Saffolding功能扩展 上次做过一个<Asp.net webform scaffolding结合Generic Unit of Work & (Extensible) Repositories Framework代码生成向导> 是生存Web Form的. 这次看到网上有生成MVC Saffolding扩展原作者的代码 https://github.com/robinli/MVC5-

STM32代码自动生成工具使用说明

1.什么是"代码自动生成工具" 为了降低开发者的开发门槛,缩短开发周期,降低开发资源投入,机智云推出了代码自动生成服务.云端会根据产品定义的数据点生成对应产品的设备端代码. 自动生成的代码实现了机智云通信协议的解析与封包.传感器数据与通信数据的转换逻辑,并封装成了简单的API,且提供了多种平台的实例代码.当设备收到云端或APP端的数据后,程序会将数据转换成对应的事件并通知到应用层,开发者只需要在对应的事件处理逻辑中添加传感器的控制函数,就可以完成产品的开发. 使用自动生成的代码开发产品

代码自动生成工具,2小时搞定智能硬件产品Demo

常见的智能硬件设备多是由单片机.微处理器.微控制器等构成的嵌入式系统,通过WIFI.蓝牙.GPRS等无线通信技术连接云服务器,传统的开发方式需要开发人员根据自己的产品功能完成MCU 通过无线通信/芯片模组与云服务器交互的协议,而通过MCU代码自动生成工具,云端会根据产品定义的数据点生成对应产品的设备端代码.使用自动生成的代码开发产品,就不必再处理协议相关的部分,开发者可以将节省出来的精力集中在产品的核心功能开发上,不必重复"造轮子". 使用MCU.手机APP代码自动生成工具,2小时搞定

iBatis 代码自动生成工具 iBator 及 Example 使用

iBator的下载和安装 官方下载地址:http://people.apache.org/builds/ibatis/ibator/ 安装:见<Eclipse 插件安装> 安装完成后,“File” —> "New" —> "Other..." iBatis 代码自动生成工具 iBator - 低调的华丽 - 辉色空间 选择项目名 —> "New" —> "Other..." —> “N

JAVA入门[7]-Mybatis generator(MBG)自动生成mybatis代码

一.新建测试项目 新建Maven项目MybatisDemo2,修改pom.xml引入依赖.dependencies在上节基础上新增 <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency>

记录下 mybatis.generator 插件自动生成mapper+entity+mapper.xml 少走点坑

Demo  springBoot+Mybatis+oracle 1.创建项目  ...... 2. 配置 generator 文件 在resource 下创建 generatorConfig.xml 文件 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator C