mybatis三剑客之mybatis-generator配置

7mybatis三剑客之mybatis-generator配置生成pojo数据表实体

mybatis-generator根据数据库自动生成pojo,即数据表字段的属性

7.1mybatis-generator配置安装

在项目的pom.xml文件的plugin中配置,引入依赖包:

<plugin>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-maven-plugin</artifactId>
  <version>1.3.2</version>
  <configuration>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
  </configuration>
</plugin>

7.2配置mybatis-generator,主要是连接数据等一些操作:

数据库配置文件:datasource.properties 和 mybatis-generator的配置文件 generatorConfig.xml

---------------datasource.properties----------
db.driverLocation=D:/development_eve/mysql-connector-java-5.1.6-bin.jar   
db.driverClassName=com.mysql.jdbc.Driver

#db.url=jdbc:mysql://192.168.74.1:3306/mmall?characterEncoding=utf-8
db.url=jdbc:mysql://127.0.0.1:3306/mmall?characterEncoding=utf-8
db.username=root
db.password=Smaluv*******

db.initialSize = 20
db.maxActive = 50
db.maxIdle = 20
db.minIdle = 10
db.maxWait = 10
db.defaultAutoCommit = true
db.minEvictableIdleTimeMillis = 3600000
-------------------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>
    <!--导入数据库相关属性配置文件-->
    <properties resource="datasource.properties"></properties>

    <!--指定特定数据库的jdbc驱动jar包的位置-->
    <classPathEntry location="${db.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">

        <!-- optional,旨在创建class时,对注释进行控制 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接 -->
        <jdbcConnection
                driverClass="${db.driverClassName}"
                connectionURL="${db.url}"
                userId="${db.username}"
                password="${db.password}">
        </jdbcConnection>


        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <!--<javaModelGenerator targetPackage="com.mshop.pojo" targetProject=".\src\main\java">-->
        <javaModelGenerator targetPackage="com.mshop.pojo" 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="false"/>
        </javaModelGenerator>

        <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\resources">-->
        <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->

        <!-- targetPackage:mapper接口dao生成的位置 -->
        <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mshop.dao" targetProject=".\src\main\java">-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mshop.dao" targetProject="./src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!--配置数据库db所有的表,即有多少表就要配置多少个-->
        <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <!--数据库字段属性改变-text>varchar-->
            <columnOverride column="detail" jdbcType="VARCHAR" />
            <columnOverride column="sub_images" jdbcType="VARCHAR" />
        </table>
        <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>


        <!-- geelynote mybatis插件的搭建 -->
    </context>
</generatorConfiguration>

7.3 生成mybatis-generator的generatorConfiguration配置的db文件

IDEA右边的Maven-Plugins -> Mybatis-generator 执行生成db数据库对应的dao/pojo/mapper对象。

7.4 优化时间戳设置,免去业务代码中创建和更新时间。优化Mybatis-Generator生成的DB数据表中,关于表中createTime和updateTime时间戳优化的写法,主要是因为这两个时间需要根据表中数据插入的时间和修改的时间获取相对应的时间需要优化,确定何时插入时间要记录插入时间和修改时间,用now()获取。

方法:从Mybatis-Generator的生成的Mapper的各个数据表,找到所有表中创建insert和更新update操作,将对应的now方法加入db的sql语句:

insert时:createTime和updatecreateTime都设置为now()

update时:createTime不变,updateTime设置为now()

<insert id="insert" parameterType="com.mshop.pojo.Cart" >
  insert into mmall_cart (id, user_id, product_id, 
    quantity, checked, create_time, 
    update_time)
  values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, 
    #{quantity,jdbcType=INTEGER}, #{checked,jdbcType=INTEGER}, now(), 
    now())
</insert>

<update id="updateByPrimaryKey" parameterType="com.mshop.pojo.Cart" >
  update mmall_cart
  set user_id = #{userId,jdbcType=INTEGER},
    product_id = #{productId,jdbcType=INTEGER},
    quantity = #{quantity,jdbcType=INTEGER},
    checked = #{checked,jdbcType=INTEGER},
    create_time = #{createTime,jdbcType=TIMESTAMP},
    update_time = now()
  where id = #{id,jdbcType=INTEGER}
</update>

Tips:Mybatis-Generator插件使用的出现的几个小插曲:

a-IDEA右边没有出现Maven Project:

解决方法:IDEA上工具栏-View-Tool Windows-Maven Projects

b-出现Maven Project后,一开始我使用的是IDEA2019版的,不知道是笔者Mybatis插件写法没有配好的原因还是其他导致的,Maven Projects里面没有Plugins

解决方法:笔者将2019版的IDEA换成了2017版的,再按上述的方法再次打开Maven Project,出现了配置的Plugins插件

c-还有IDEA的log一直在报 Maven import导入依赖一直在报错,找了一些资料,网上似乎都一直在说是Maven3.6新版本的bug,建议换成底版本,于是后面笔者重新下载了3.0.*低版本,发现该错误确实没有了

d-万事具备,只欠东风。发现Maven Project中的Plugins依旧没有出现 pom.xml配置的Mybatis-generator插件,网上找了一些方法,似乎不太起作用,后面发现可能是Maven版本的原因,根据网上资料的提示,在pom.xml配置文件的插件管理部分,需要新建一个plugins标签和pluginManager同级,然后把generator插件配置单独配置到新建的Plugins标签中(感觉类似不起效果的Plugins都可以这样写,是否有效未测试)。

<pluginManagement>
    <plugins>
    ...
    </plugins>
    </pluginManagement>
<!--新建和pluginManagement同级的plugins,再把插件的配置单独写入(感觉类似不起效果的Plugins都可以这样写,是否有效微测试)-->
<plugins>
  <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
      <verbose>true</verbose>
      <overwrite>true</overwrite>
    </configuration>
  </plugin>
</plugins>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值