zoukankan      html  css  js  c++  java
  • MyBatis Generator

    使用Mybatis-Generator插件

    来源数据库之旅(一)

    1.在pom文件里加入Mybatis-Generator插件

    <build>
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <executions>
                        <execution>
                            <id>mybatis-generator</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <!-- Mybatis-Generator 工具配置文件的位置 -->
                        <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.46</version>
                        </dependency>
                        <dependency>
                            <groupId>org.mybatis.generator</groupId>
                            <artifactId>mybatis-generator-core</artifactId>
                            <version>1.3.2</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <classifier>exec</classifier>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    2.创建Mybatis-Generator配置文件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>
    <!--执行generator插件生成文件的命令: call mvn mybatis-generator:generate -e -->
    <!-- 引入配置文件 -->
    <properties resource="application.properties"/>
    <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->

    <!-- 已经在pom.xml 的plugin中单独依赖Mysql驱动包,则可以不配置此项-->
    <!--<classPathEntry location="D:generator_mybatismysql-connector-java-5.1.24-bin.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="false"/><!-- 是否取消注释 -->
    <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
    </commentGenerator>

    <!-- jdbc连接 -->
    <jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}" />
    <!-- 类型转换 -->
    <javaTypeResolver>
    <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
    <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>

    <!-- 需要自行配置 需要自行配置 需要自行配置-->
    <!-- 生成实体类地址 -->
    <javaModelGenerator targetPackage="com.springboot.entity" targetProject="${mybatis.project}" >
    <property name="enableSubPackages" value="false"/>
    <property name="trimStrings" value="true"/>
    </javaModelGenerator>
    <!-- 生成mapxml文件 -->
    <sqlMapGenerator targetPackage="mapper" targetProject="${mybatis.resources}" >
    <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>
    <!-- 需要自行配置 需要自行配置 需要自行配置 -->
    <!-- 生成mapxml对应client,也就是接口dao -->
    <javaClientGenerator targetPackage="com.springboot.dao" targetProject="${mybatis.project}" type="XMLMAPPER" >
    <property name="enableSubPackages" value="false" />
    </javaClientGenerator>
    <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 -->
    <table tableName="user" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
    <property name="useActualColumnNames" value="false" />
    <!-- 数据库表主键 -->
    <generatedKey column="id" sqlStatement="Mysql" identity="true" />
    </table>
    </context>
    </generatorConfiguration>

    3.Mybatis-Generator项目配置文件(项目配置文件就是写在默认配置文件里的)

    #mapper xml位置
    mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
    
    #数据库配置
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    spring.datasource.username=root
    spring.datasource.password=123456abc
    
    #mybatis generator配置
    #dao类(或mapper类)和实体类位置
    mybatis.project =src/main/java
    #mapper文件位置
    mybatis.resources=src/main/resources

    4.运行生成

    右键项目run as—>maven build:goal里输入mybatis-generator:generate  -e

  • 相关阅读:
    py.turtle学习笔记(简单图形绘制)
    eclipse Network Connections
    EntityFramework 6 使用注意事项汇总
    Web发展过程中的一些设计思想和软硬件系统构建方式的一段话
    Fody is only supported on MSBuild 16 and above. Current version: 15
    .net 程序优化的原则-C#语言元素相关
    .net 事务
    关于IIS部署网站后 浏览器HTTP 错误 404.7 请求筛选模块被配置为拒绝该文件扩展名。
    准备学习的书籍列表
    在本地搭建Git厂库并把自己得代码上传到远程厂库
  • 原文地址:https://www.cnblogs.com/xc-xinxue/p/12464286.html
Copyright © 2011-2022 走看看