MyBatis代码自动生成器Mybatis-Generator使用教程

  • MyBatis代码生成器Mybatis-Generator的配置和使用。
  • 注:项目介绍
  • 编译器:Intellij IDEA
  • 项 目:SpringBoot项目
  • 讲道理现在MyBatis-plus出来了。省去了再写许多繁琐的xml文件。也大大简化了开发压力。类似于SpringData-JPA(也挺好用的)。MyBatis-plus也有相关的代码生成器。后面有时间博主再去踩一下回来再整理。

1、首先我们有一个建好的现成项目。

在这里插入图片描述

  • 可以看到什么都还没有加进去,那我们就从连接数据库到代码自动生成演示一下。使用Mybatis-Generator

  • 1、首先我们要添加一个配置文件,这个也是最关键的文件。
    在这里插入图片描述

  • 配置文件代码:mybatis-generator-cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
    	<!-- 要生成哪些表  orders是我的表名,Orders是生成的类名,比如我的映射类为Order,映射接口OrderMapper, 映射文件为OrderMapper.xml,可以添加多个表,里面的几个配置大概意思就是是否允许生成example文件和支持selectByExample。用过Mybatis的应该知道selectByExample,对于一些简单查询用这个还是比较方便的。哈哈、话有点多,记得删除 -->
    	<table tableName="orders" domainObjectName="Orders"
    		   enableCountByExample="true" enableUpdateByExample="true"
    		   enableDeleteByExample="true" enableSelectByExample="true"
    		   selectByExampleQueryId="true"></table>
    	<!-- 要生成哪些表  -->
    	<table tableName="products" domainObjectName="Products"
    		   enableCountByExample="true" enableUpdateByExample="true"
    		   enableDeleteByExample="true" enableSelectByExample="true"
    		   selectByExampleQueryId="true"></table>
    </context>
    
  • 2、其次就是pom里面添加配置。加载plugins里面!注:红圈的路径对应的就是刚才添加的配置文件。

在这里插入图片描述

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/mybatis-generator-cfg.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <!-- <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> -->
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
            </plugin>
  • 3、添加运行配置、运行文件(对了记得吧application.properties后缀改为yml。不然会找不到yml。或者在之前的那个配置文件把yml改为properties,都可以。)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 懒人专用:请copy
    mybatis-generator:generate -e

  • 4、选中刚才配置的直接运行就ok了。是不是很简单。由于Mybatis要写很多很多的xml文件、和Sql语句。这个代码生成器会直接生成诸多常用的增删查改。

  • 看到以下提示、说明你很幸运,直接成功了。你可真是太优秀了!博主开始用的时候可是遇到了太多的坑了。
    在这里插入图片描述

  • 我们再到刚才配置生成的路径下看看文件。已经帮我们直接生成了我们想要的基础文件。
    在这里插入图片描述

  • 随便例举一个Mapper接口吧, 为了让大家更直观的看到、我把后面自动生成的注释删了。注释的作用主要用于表格变动之类需要再次生成时识别是否为自动生成的代码(比如第一个countByExample)。会默认覆盖自动生成的代码。没有注释的会保留下来。:

    public interface OrdersMapper {
    /**
    * This method was generated by MyBatis Generator.
    * This method corresponds to the database table orders
    *
    * @mbg.generated
    */
    long countByExample(OrdersExample example);

    int deleteByExample(OrdersExample example);
    
    int deleteByPrimaryKey(String id);
    
    int insert(Orders record);
    
    int insertSelective(Orders record);
    
    List<Orders> selectByExample(OrdersExample example);
    
    Orders selectByPrimaryKey(String id);
    
    int updateByExampleSelective(@Param("record") Orders record, @Param("example") OrdersExample example);
    
    int updateByExample(@Param("record") Orders record, @Param("example") OrdersExample example);
    
    int updateByPrimaryKeySelective(Orders record);
    
    int updateByPrimaryKey(Orders record);
    

    }

重点

以下提几点需要注意的问题。

  • 1、注意mysql的版本问题,不能超过5 。博主遇到过问题超过五就报错。太久了忘了记录了。跟我一样选一样默认依赖。新建SpringBoot项目后MySql-Connector默认是8.几。所以加一个版本号就行。
    在这里插入图片描述

  • 2、配置文件里面的连接依赖和项目配置的依赖路径一致。不然也会报错。可直接右键jar包->find in path -> copy路径到右边配置文件对应位置即可。
    在这里插入图片描述

  • 3、还是开始提的pom里面的generator.xml路径一定要配对。

  • 最后就附上我的pom.xml文件吧

    <?xml version="1.0" encoding="UTF-8"?>


    4.0.0

    org.springframework.boot
    spring-boot-starter-parent
    2.3.0.RELEASE


    com.springbootMybatis
    sbm
    0.0.1-SNAPSHOT
    sbm
    Demo project for Spring Boot

    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
    
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/mybatis-generator-cfg.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    
  • 注:此博客基础博客。博主比较菜。常用的是SSM,这个是博主搭建SpringBoot测试项目的时候记录的。可能不是很规范,望见谅。如有错漏还望指正。谢谢谢哈!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值