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配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详细解读spring中的@Resource注解

    详细解读spring中的@Resource注解

    这篇文章主要介绍了详细解读spring中的@Resource注解,此注解来源于JSR规范(Java Specification Requests),其作用是找到依赖的组件注入到应用来,它利用了JNDI技术查找所需的资源,需要的朋友可以参考下
    2023-10-10
  • 使用ehcache三步搞定springboot缓存的方法示例

    使用ehcache三步搞定springboot缓存的方法示例

    本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-04-04
  • mybatis如何处理返回结果集

    mybatis如何处理返回结果集

    这篇文章主要介绍了mybatis如何处理返回结果集问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • java socket实现聊天室 java实现多人聊天功能

    java socket实现聊天室 java实现多人聊天功能

    这篇文章主要为大家详细介绍了java socket实现聊天室,java实现多人聊天功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Springboot整合SpringSecurity实现登录认证和鉴权全过程

    Springboot整合SpringSecurity实现登录认证和鉴权全过程

    这篇文章主要介绍了Springboot整合SpringSecurity实现登录认证和鉴权全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • springboot简单实现单点登录的示例代码

    springboot简单实现单点登录的示例代码

    本文主要介绍了springboot简单实现单点登录的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • Java FileInputStream读中文乱码问题解决方案

    Java FileInputStream读中文乱码问题解决方案

    这篇文章主要介绍了Java FileInputStream读中文乱码问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • java实现斗地主游戏

    java实现斗地主游戏

    这篇文章主要为大家详细介绍了java实现斗地主游戏,洗牌、发牌、看牌,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Spring Boot使用和配置Druid

    Spring Boot使用和配置Druid

    本篇文章主要介绍了Spring Boot使用和配置Druid,Druid号称是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能
    2017-04-04
  • java基础学习JVM中GC的算法

    java基础学习JVM中GC的算法

    这篇文章主要介绍了java基础学习JVM中GC的算法,通过图文加深对GC算法思路的理解。
    2017-11-11

最新评论