zoukankan      html  css  js  c++  java
  • 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配置文件

    按 Ctrl+C 复制代码
    按 Ctrl+C 复制代码

    5、下载maven依赖包,update project

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

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

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

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

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

     github项目地址:https://github.com/JsonShare/mybatis_generator.git

  • 相关阅读:
    面试笔试题目集
    [vs2010]:fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "StdAfx.h"”?
    [数据库] SQLite常见问题解答
    安卓学习资料总结39
    Android 学习资料总结40
    python变量的定义和使用
    python运算符
    python的注释
    print输出函数
    python数据类型转换
  • 原文地址:https://www.cnblogs.com/fenglan/p/5956953.html
Copyright © 2011-2022 走看看