zoukankan      html  css  js  c++  java
  • Mybatis的generator自动生成代码

    mybatis-generator有三种用法:命令行、ide插件、maven插件。本次使用maven生成

    环境:IDEA,mysql8,maven

    (1):新建项目,本次以SpringBoot项目为例

    (2)在maven中添加依赖:

     1 <plugin>
     2                 <groupId>org.mybatis.generator</groupId>
     3                 <artifactId>mybatis-generator-maven-plugin</artifactId>
     4                 <version>1.3.7</version>
     5                 <configuration>
     6                     <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
     7                     <verbose>true</verbose>
     8                     <overwrite>true</overwrite>
     9                 </configuration>
    10 </plugin>
    mybatis-generator-maven-plugin

    (3)添加配置文件,项目结构如图:

    (4)在application.yml文件中添加mybatis, 需要注意的是mysql8和之前的Driver不一样,而且需要设置时区,不然会报错(这一步暂时不需要)

     1 server:
     2   port: 8080
     3 
     4 spring:
     5   datasource:
     6     name: test
     7     url: jdbc:mysql://localhost:3306/test?serverTimezone=CTT&characterEncoding=utf-8
     8     username: root
     9     password: 123456
    10     driver-class-name: com.mysql.jdbc.Driver
    11 #    页面加载
    12   thymeleaf:
    13     cache: false
    14 
    15 mybatis:
    16   mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
    17   type-aliases-package: com.example.mybatis.entity  # 注意:对应实体类的路径
    View Code

    (5)在resource文件夹下创建generator文件夹,并在文件夹中创建generatorConfig.xml文件,自行修改包的关系,连接信息等

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE generatorConfiguration
     3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
     4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
     5 <generatorConfiguration>
     6     <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
     7     <classPathEntry
     8             location="C:UsersFlyuz.m2
    epositorymysqlmysql-connector-java5.1.46mysql-connector-java-5.1.46.jar"/>
     9     <context id="DB2Tables" targetRuntime="MyBatis3">
    10         <commentGenerator>
    11             <property name="suppressDate" value="true"/>
    12             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
    13             <property name="suppressAllComments" value="true"/>
    14         </commentGenerator>
    15         <!--数据库连接驱动类,URL,用户名、密码 -->
    16         <jdbcConnection
    17                 driverClass="com.mysql.jdbc.Driver"
    18                 connectionURL="jdbc:mysql://localhost:3306/test"
    19                 userId="root"
    20                 password="123456">
    21             <property name="nullCatalogMeansCurrent" value="true"/>
    22         </jdbcConnection>
    23         <javaTypeResolver>
    24             <property name="forceBigDecimals" value="false"/>
    25         </javaTypeResolver>
    26         <!-- 生成(实体)模型的包名和位置-->
    27         <javaModelGenerator targetPackage="com.example.mybatis.entity" targetProject="src/main/java">
    28             <property name="enableSubPackages" value="true"/>
    29             <property name="trimStrings" value="true"/>
    30         </javaModelGenerator>
    31         <!-- 生成XML映射文件的包名和位置-->
    32         <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
    33             <property name="enableSubPackages" value="true"/>
    34         </sqlMapGenerator>
    35         <!-- 生成DAO接口的包名和位置-->
    36         <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mybatis.mapper" targetProject="src/main/java">
    37             <property name="enableSubPackages" value="true"/>
    38         </javaClientGenerator>
    39         <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
    40         <!--当表有主键时,根据primaryKey生成select、update、delete方法-->
    41         <!--Text类型的字段时,generator会生成WithBLOBS.java文件并继承-->
    42         <table tableName="%" domainObjectName="User"
    43                enableSelectByPrimaryKey="true"
    44                enableUpdateByPrimaryKey="true"
    45                enableDeleteByPrimaryKey="true"
    46                enableCountByExample="false" enableUpdateByExample="false"
    47                enableDeleteByExample="false" enableSelectByExample="false"
    48                selectByExampleQueryId="false">
    49             <columnOverride column="title" javaType="java.lang.String" jdbcType="VARCHAR"/>
    50             <columnOverride column="content" javaType="java.lang.String" jdbcType="VARCHAR"/>
    51         </table>
    52     </context>
    53 </generatorConfiguration>
    View Code

    测试发现sqlMapGenerator的位置应该放在src/main/resources下,否则会出现找不到绑定的错误

    自动生成:

    结果在下面命令行中显示:

     如果遇到什么问题,根据显示结果修改配置文件

    如何使用看下一篇

  • 相关阅读:
    ==和equals区别
    如何创建一个不可变类
    mysql用户的创建和授权
    事务
    Java知识点检测
    Redis
    正则表达式中match的用法
    rfind的用法
    找出文件夹里所有的文件路径
    合并多个pdf文件
  • 原文地址:https://www.cnblogs.com/flyuz/p/10896468.html
Copyright © 2011-2022 走看看