MyBatis Generator配置入门

 更新时间:2023年07月20日 14:57:39   作者:yanessa_yu  
本文主要介绍了MyBatis Generator配置入门,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在日常开发中使用mybatis作为持久层框架要写entity,dao,mapper接口,mapper.xml做CRUD这些重复操作,mybatis官方提供了MyBatis Generator为我们做这些工作。
由于使用该插件有很多方式,目前介绍一种使用maven插件的方式。

引入pom配置文件

<!-- mybatis-generator -->
<dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-core</artifactId>
   <version>1.3.7</version>
   <scope>compile</scope>
   <optional>true</optional>
</dependency>

配置MyBatis 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>
    <!--配置数据库连接的位置-->
<!--    <properties url="metersphere.properties"/>-->
    <!-- 设置mysql驱动路径 -->
    <!--<classPathEntry location="/Users/test/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar"/>-->
    <!-- 此处指定生成针对MyBatis3的DAO -->
    <context id="mysql" targetRuntime="MyBatis3">
        <!-- 字段带`,解决列表跟关键字冲突问题 -->
        <property name="autoDelimitKeywords" value="true" />
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
        <!-- 用来除去时间信息的,这在配合类似subversion的代码管理工具时使用很有效,因为可以减少没有必要的注释迁入 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!--  关闭自动生成的注释  -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- jdbc连接信息 --> <!-- EduLoanManage EduTestDataBase -->
        <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.20.180:3306/fit2cloud"-->
        <!--userId="root" password="Fit2cloud2015!" />-->
<!--        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"-->
<!--                        connectionURL="${spring.datasource.url}&amp;nullCatalogMeansCurrent=true"-->
<!--                        userId="${spring.datasource.username}" password="${spring.datasource.password}"/>-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://*.*.*:3306/metersphere?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"
                        userId="root" password="****"/>
        <!-- javaTypeResolver式类型转换的信息 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 模型对象 -->
        <javaModelGenerator targetPackage="io.metersphere.base.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- XML映射文件 -->
        <sqlMapGenerator targetPackage="io.metersphere.base.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 接口 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="io.metersphere.base.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--要生成的数据库表 -->
        <table tableName="table_name_1"/>
        <table tableName="table_name_2"/>
<!--        表名和关键字冲突-->
<!--                <table tableName="group" delimitIdentifiers="true"></table>-->
    </context>
<!--    <settings>-->
<!--        &lt;!&ndash; 打印sql日志 &ndash;&gt;-->
<!--        <setting name="logImpl" value="STDOUT_LOGGING" />-->
<!--    </settings>-->
</generatorConfiguration>

编写启动类

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;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class MybatisGeneratorMain {
   public static void main(String[] args) throws Exception {
      List<String> warnings = new ArrayList<>();
      ConfigurationParser cp = new ConfigurationParser(warnings);
      Configuration config = cp.parseConfiguration(new File("src/main/resources/mybatis-generator.xml"));
       //Configuration config = cp.parseConfiguration(ClassLoader.getSystemResourceAsStream("generatorConfig.xml"));
      DefaultShellCallback callback = new DefaultShellCallback(true);
      MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
      myBatisGenerator.generate(null);
      for (String warning : warnings) {
         System.out.println(warning);
      }
   }
}

到此这篇关于MyBatis Generator配置入门的文章就介绍到这了,更多相关MyBatis Generator配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringMVC体系分层模式原理图解

    SpringMVC体系分层模式原理图解

    这篇文章主要介绍了SpringMVC体系分层模式原理图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • java 实现迷宫回溯算法示例详解

    java 实现迷宫回溯算法示例详解

    这篇文章主要介绍了java 实现迷宫回溯算法示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • SpringBoot整合AOP及使用案例实战

    SpringBoot整合AOP及使用案例实战

    本文详细介绍了Spring AOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以及环绕通知的实现,感兴趣的朋友跟随小编一起看看吧
    2025-12-12
  • Java模仿微信实现零钱通简易功能(两种版本)

    Java模仿微信实现零钱通简易功能(两种版本)

    本文主要介绍了使用Java开发零钱通项目, 模仿微信实现简易功能,可以完成收益入账,消费,查看明细,退出系统等功能。文中一共介绍了两种实现方法,快来学习吧
    2021-12-12
  • Spring Boot + EasyExcel + SqlServer 进行批量处理数据的高效方法

    Spring Boot + EasyExcel + SqlServer 进行批量处理数据的高效方法

    在日常开发和工作中,我们可能要根据用户上传的文件做一系列的处理,本篇文章就以Excel表格文件为例,主要介绍了Spring Boot + EasyExcel + SqlServer 进行批量处理数据的高效方法,需要的朋友可以参考下
    2024-06-06
  • SpringCloud Stream 整合RabbitMQ的基本步骤

    SpringCloud Stream 整合RabbitMQ的基本步骤

    这篇文章主要介绍了SpringCloud Stream 整合RabbitMQ的基本步骤,从项目介绍到生产者结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • 利用Java的Struts框架实现电子邮件发送功能

    利用Java的Struts框架实现电子邮件发送功能

    这篇文章主要介绍了利用Java的Struts框架实现电子邮件发送功能,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
    2015-12-12
  • Spring事件监听基本原理与使用详解

    Spring事件监听基本原理与使用详解

    这篇文章主要介绍了Spring事件监听基本原理与使用详解,Spring的事件监听机制和发布订阅机制是很相似的:发布了一个事件后,监听该类型事件的所有监听器会触发相应的处理逻辑,需要的朋友可以参考下
    2024-01-01
  • springboot对接微信支付的完整流程(附前后端代码)

    springboot对接微信支付的完整流程(附前后端代码)

    最近在做支付平台的项目,承接公司业务系统与第三方支付平台的对接任务,主要涉及微信支付、支付宝支付以及理房通支付等第三方平台,这篇文章主要给大家介绍了关于springboot对接微信支付的完整流程,需要的朋友可以参考下
    2021-08-08
  • SpringBoot AOP实现钉钉+企业微信双渠道异常告警

    SpringBoot AOP实现钉钉+企业微信双渠道异常告警

    本文主要介绍了SpringBoot AOP实现钉钉+企业微信双渠道异常告警,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-04-04

最新评论