zoukankan      html  css  js  c++  java
  • mybatis 逆向工程(通过数据库表针对单表自动生成mybatis执行所需要的代码)

    mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo…),可以让程序员将更多的精力放在繁杂的业务逻辑上。

            企业实际开发中,常用的逆向工程方式:由数据库的表生成java代码。

            之所以强调单表两个字,是因为Mybatis逆向工程生成的Mapper所进行的操作都是针对单表的,也许你可能会觉得那这就有点鸡肋了,但是在大型项目中,很少有复杂的多表关联查询,所以作用还是很大的。

    这是项目最终的效果:

    一、注入maven依赖  ==》

    二、生成  generatorConfig.xml ==》 

     

     三、运行

     

    源码:

    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     <context id="test" targetRuntime="MyBatis3">
     7         <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
     8         <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
     9         <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
    10         <commentGenerator>
    11             <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
    12             <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
    13             <property name="suppressDate" value="true" />
    14             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
    15             <property name="suppressAllComments" value="true" />
    16         </commentGenerator>
    17         <!--数据库链接URL,用户名、密码 -->
    18         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    19                         connectionURL="jdbc:mysql://localhost:3310/mybatis" userId="root"
    20                         password="123456">
    21         </jdbcConnection>
    22         <javaTypeResolver>
    23             <!-- This property is used to specify whether MyBatis Generator should
    24                 force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
    25             <property name="forceBigDecimals" value="false" />
    26         </javaTypeResolver>
    27         <!-- 生成模型的包名和位置 -->
    28         <javaModelGenerator targetPackage="com.etc.entity"
    29                             targetProject="target">
    30             <property name="enableSubPackages" value="true" />
    31             <property name="trimStrings" value="true" />
    32         </javaModelGenerator>
    33         <!-- 生成映射文件的包名和位置 -->
    34         <sqlMapGenerator targetPackage="com.etc.dao"
    35                          targetProject="target">
    36             <property name="enableSubPackages" value="true" />
    37         </sqlMapGenerator>
    38         <!-- 生成DAO的包名和位置 -->
    39         <javaClientGenerator type="XMLMAPPER"
    40                              targetPackage="com.etc.dao"
    41                              targetProject="target">
    42             <property name="enableSubPackages" value="true" />
    43         </javaClientGenerator>
    44 
    45         <!-- 要生成哪些表 -->
    46         <table tableName="t_house" domainObjectName="House"></table>
    47 
    48     </context>
    49 </generatorConfiguration>

    pom.xml:

     1    <plugins>
     2       <plugin>
     3         <groupId>org.mybatis.generator</groupId>
     4         <artifactId>mybatis-generator-maven-plugin</artifactId>
     5         <version>1.3.2</version>
     6         <dependencies>
     7           <dependency>
     8             <groupId>mysql</groupId>
     9             <artifactId>mysql-connector-java</artifactId>
    10             <version>5.1.22</version>
    11           </dependency>
    12         </dependencies>
    13         <configuration>
    14           <!--配置文件的路径-->
    15           <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
    16           <overwrite>true</overwrite>
    17         </configuration>
    18       </plugin>
    19     </plugins>
  • 相关阅读:
    uoj#214. 【UNR #1】合唱队形
    「集训队作业2018」复读机
    WPF进阶技巧和实战01-小技巧
    03 依赖注入--01控制反转、IoC模式
    01 ASP.NET Core 3 启动过程(一)
    05-IdentityServer4
    IdentityServer4系列[6]授权码模式
    04-授权策略
    03-Jwt在.netcore中的实现
    02-token
  • 原文地址:https://www.cnblogs.com/LiuOOP/p/11242570.html
Copyright © 2011-2022 走看看