1.加载依赖

      <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>${mybatis.generator.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>

2. 在src/main/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>
  <!--JDBC驱动jar包的位置
    <classPathEntry location="C:/workspace/project/learning/mybatis/lib/mysql-connector-java-5.1.6.jar"/> -->
    <!--数据库驱动包路径 -->
   <classPathEntry location="C:\Users\xxxx\.m2\repository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar"/>
    <context id="mysql" targetRuntime="MyBatis3">
        <!--关闭注释 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3307/test?characterEncoding=utf-8"
                        userId="root" password="123456">
        </jdbcConnection>
        <!--生成的model 包路径 -->
        <javaModelGenerator targetPackage="com.example.demo.user.model" targetProject="src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="true"/>
        </javaModelGenerator>
        <!--生成xml mapper文件 路径 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis">
            <property name="enableSubPackages" value="ture"/>
        </sqlMapGenerator>

<!-- 生成的Dao接口 的包路径 -->
        <!-- 生成易于使用的针对Model对象和XML配置文件的代码
        type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象
        type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
        type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.user.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="ture"/>
        </javaClientGenerator>
        <!--对应数据库表名 -->
        <table tableName="role" domainObjectName="Role" enableCountByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="permission" domainObjectName="Permission" enableCountByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="role_permission" domainObjectName="RolePermission" enableCountByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

3. 写java类自动生成mapper的xml(单表的增删改查)和数据库对应实体类

package com.example.demo.test.util;

import org.mybatis.generator.api.ShellRunner;

public class RunMybatisGeneratorShell {
    public static void main(String[] args) {
        String config = RunMybatisGeneratorShell.class.getClassLoader()
                .getResource("generatorConfig.xml").getFile();
            String[] arg = { "-configfile", config, "-overwrite" };
            ShellRunner.main(arg);
    }

}

4.运行结果

Existing file git\demo\src\main\java\com\example\demo\user\model\Role.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\dao\RoleMapper.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\model\Permission.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\dao\PermissionMapper.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\model\UserRole.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\dao\UserRoleMapper.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\model\RolePermission.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\dao\RolePermissionMapper.java was overwritten

MyBatis Generator finished successfully, there were warnings.

mybatis自动生成数据库对应的mapper接口,xml和实体类相关推荐

  1. java实体类生成mysql表_springboot+mybatis通过实体类自动生成数据库表的方法

    前言 本章介绍使用mybatis结合mysql数据库自动根据实体类生成相关的数据库表. 首先引入相关的pom包我这里使用的是springboot2.1.8.RELEASE的版本 org.mybatis ...

  2. 简单的利用IDEA搭建SpringBoot+Maven+Mybatis+自动生成代码

    最近在系统的学习SpringBoot框架,并且要用该框架做个项目--网上也大大小小看了很多教程,感觉很多写文章的人都不太负责任,只知道搬运,大概都没有实际操作过,问题也是有很多,所以自己写一篇文章记录 ...

  3. 【MyBatis】MyBatis自动生成代码之查询爬坑记

    前言 项目使用SSM框架搭建Web后台服务,前台后使用restful api,后台使用MyBatisGenerator自动生成代码,在前台使用关键字进行查询时,遇到了一些很宝贵的坑,现记录如下.为展示 ...

  4. eclipse maven 搭建 SSM(Spring+SpringMVC+MyBatis)开发环境 和 MyBatis 自动生成的 maven 插件配置

    最近研究java web开发,首先需要配置好开发环境,查阅网上各位大神的文章后搭建完成,记录下来作为备忘. 1.安装Maven Maven下载地址:http://maven.apache.org/do ...

  5. Spring+SpringMVC+Mybatis(开发必备技能)04、mybatis自动生成mapper_dao_model(包含工具与视频讲解) 纯绿色版本、配套使用视频,100%运行成功

    Spring+SpringMVC+Mybatis(开发必备技能) 04.mybatis自动生成mapper_dao_model(包含工具与视频讲解) 纯绿色版本.配套使用视频,100%运行成功 百度网 ...

  6. Mybatis自动生成代码插件generator

    Mybatis自动生成代码插件generator 1.pom maven依赖 <dependencies><dependency><groupId>org.myba ...

  7. mybatis generator 生成数据库注释等问题

    mybatis代码生成器生成数据库的注释,找了半天没有找到非常详细可用的,于是我打算自己整理一份,分享出来,以下是本人亲身经历的问题处理流程,实践有效. 前提:可以使用一般的mybatis gener ...

  8. Mybatis自动生成的Example类的使用与解析

    在上篇文章我有讲到mybatis如何自动生成我们所需的dao代码,今天我们把上篇文章遗留的问题给大家讲解一下.个人拙见,欢迎补充. 上篇文章中我有说过利用Mybatis自动生成的Example类可以满 ...

  9. Mybatis自动生成实体类等代码

    Mybatis自动生成实体类等代码 具体步骤 具体步骤 在本机随便找个目录存放以下文件,如图(mybatis-generator-core-1.3.5.jar 和 mysql-connector-ja ...

最新文章

  1. 【Codeforces】1136C Nastya Is Transposing Matrices (矩阵转置)
  2. TinyMind 汉字书法识别竞赛开启总决赛啦!!
  3. CS231n 2016 通关 第三章-SVM 作业分析
  4. Linux 下qt 程序打包发布(使用linuxdelpoyqt ,shell 脚本)
  5. 软定时器的原理与创建
  6. [上海站] 微软Azure AspNetCore微服务实战
  7. FireMonkey 源码学习(2)
  8. 蚂蚁集团官宣启动上市计划,上交所、港交所表示热烈欢迎...
  9. 成都五月花计算机网教,【强调】成都五月花计算机学校网址是什么
  10. android 小米读写权限,Android 小米手机的权限问题
  11. 新能源汽车入局不易 传第三张牌照花落前途汽车
  12. 【转】中国只有俩导演,一个叫贾樟柯,一个叫姜文
  13. 计算机英语领域有哪些构词法,计算机专业英语的构词方法(共2969字).doc
  14. 华为鸿蒙推送机型,华为鸿蒙系统开始推送,这15款机型可率先升级,有你的吗?...
  15. 重装系统,找不到gpedit.msc的解决办法
  16. mql5的include库文件中自定义enum类型在指标文件中的调用方式
  17. 一位4年的JAVA工程师的面试总结:面试应该先从注意整体的节奏,然后从这些地方下手(数据结构、算法、JVM、多线程、数据库)
  18. 你应该知道的——微信公众号配上机器人回复(微信对话开放平台)
  19. 智慧魔珠金字塔(类似俄罗斯方块)的所有情况 python
  20. 顶尖电子秤ls6恢复出厂_顶尖电子秤常见故障处理方法

热门文章

  1. maven项目中无Java文件(转载)
  2. 操作手册和用户手册的区别
  3. (附源码)SSM校园疫情防控志愿服务JAVA计算机毕业设计项目
  4. 【每日一题】最少货币数
  5. Golang安装 linux
  6. UR机器人:位姿表示以及相关移动
  7. 期刊论文发表为什么不能一稿多投
  8. 《终身成长》笔记七——建设性的批评
  9. 分享一个很好用的外卖红包小程序源码(可三级裂变分销,绑定公众号)
  10. 【转】我害怕阅读的人