eclipse spring boot 用 mybatis-generator 自动生成代码

IDE:STS  

项目结构

eclipse spring boot 用 mybatis-generator 自动生成代码
第一步:
maven  添加插件

<plugin>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-maven-plugin</artifactId>

<configuration>

<verbose>true</verbose>

<overwrite>true</overwrite>

</configuration>

</plugin>

第二部:
新建generatorConfig.xml  配置文件 ,主要是告诉它我们的生存规则等信息 放到resources目录下!

<?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>

    <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->

    <classPathEntry location="/Users/wangqiang/.m2/repository/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar"/>

 

    <!-- 一个数据库一个context,defaultModelType="flat" 大数据字段,不分表 -->

    <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">

 

        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->

        <property name="autoDelimitKeywords" value="true"/>

 

        <!-- 生成的Java文件的编码 -->

        <property name="javaFileEncoding" value="utf-8"/>

 

        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->

        <property name="beginningDelimiter" value="`"/>

        <property name="endingDelimiter" value="`"/>

 

        <!-- 格式化java代码 -->

        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>

 

        <!-- 格式化XML代码 -->

        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>

        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

 

        <!-- 注释 -->

        <commentGenerator>

            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->

            <property name="suppressDate" value="false"/> <!-- 是否生成注释代时间戳-->

        </commentGenerator>

 

        <!-- jdbc连接-->

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

                        connectionURL="jdbc:mysql://localhost:3306/mybussiness" userId="root"

                        password="wangqiang"/>

 

        <!-- 类型转换 -->

        <javaTypeResolver>

            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->

            <property name="forceBigDecimals" value="false"/>

        </javaTypeResolver>

 

        <!-- 生成实体类地址 -->

        <javaModelGenerator targetPackage="com.freemaker.HelloSpringBoot.domain" targetProject="/Atomatic/java/SpringHomeWork/MybatiesGenerator/src/main/java">

            <!-- 是否让schema作为包的后缀 -->

            <property name="enableSubPackages" value="false"/>

            <!-- 从数据库返回的值去掉前后空格 -->

            <property name="trimStrings" value="true"/>

        </javaModelGenerator>

 

        <!-- 生成mapper.xml文件存放地址 -->

        <sqlMapGenerator targetPackage="mappers" targetProject="/Atomatic/java/SpringHomeWork/MybatiesGenerator/src/main/resources">

            <property name="enableSubPackages" value="false"/>

        </sqlMapGenerator>

 

        <!-- 生成接口dao -->

        <javaClientGenerator targetPackage="com.freemaker.HelloSpringBoot.dao" targetProject="/Atomatic/java/SpringHomeWork/MybatiesGenerator/src/main/java" type="XMLMAPPER">

            <property name="enableSubPackages" value="false"/>

        </javaClientGenerator>

 

        <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 enableSelectByPrimaryKey相应的配置表示是否生成相应的接口 -->

        <table tableName="mmall_cart" enableCountByExample="false" enableUpdateByExample="false"

               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

               enableDeleteByPrimaryKey="true">

            <property name="useActualColumnNames" value="true"/>

        </table>

        <table tableName="mmall_category" enableCountByExample="false" enableUpdateByExample="false"

               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

               enableDeleteByPrimaryKey="true">

            <property name="useActualColumnNames" value="true"/>

        </table>

        <table tableName="mmall_order" enableCountByExample="false" enableUpdateByExample="false"

               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

               enableDeleteByPrimaryKey="true">

            <property name="useActualColumnNames" value="true"/>

        </table>

        <table tableName="mmall_order_item" enableCountByExample="false" enableUpdateByExample="false"

               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

               enableDeleteByPrimaryKey="true">

            <property name="useActualColumnNames" value="true"/>

        </table>

        <table tableName="mmall_pay_info" enableCountByExample="false" enableUpdateByExample="false"

               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

               enableDeleteByPrimaryKey="true">

            <property name="useActualColumnNames" value="true"/>

        </table>

        <table tableName="mall_shipping" enableCountByExample="false" enableUpdateByExample="false"

               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

               enableDeleteByPrimaryKey="true">

            <property name="useActualColumnNames" value="true"/>

        </table>

        <table tableName="mmall_cart" enableCountByExample="false" enableUpdateByExample="false"

               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

               enableDeleteByPrimaryKey="true">

            <property name="useActualColumnNames" value="true"/>

        </table>

        <table tableName="mmall_user" enableCountByExample="false" enableUpdateByExample="false"

               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

               enableDeleteByPrimaryKey="true">

            <property name="useActualColumnNames" value="true"/>

        </table>

    </context>

</generatorConfiguration>

第三步:
在 application.properties 加入
## Mybatis 配置

## Mybatis

mybatis.typeAliasesPackage=com.freemaker.HelloSpringBoot.domain 

mybatis.mapperLocations=classpath:mappers/*.xml

第四步 :
右击 项目 run as   --->    maven build...   ---> 你会看到这个界面

eclipse spring boot 用 mybatis-generator 自动生成代码

eclipse spring boot 用 mybatis-generator 自动生成代码

   在Goals    复制粘贴  :  mybatis-generator:generate  -e

最后点击 run。

完成之后  项目目录为:

eclipse spring boot 用 mybatis-generator 自动生成代码

提示:需要创建对应的数据库以及数据库中的表  数据库一定是开启状态