使用mybatis自动生成代码

具体操作如下:
首先需要在pom文件中导入相关依赖(如果使用springboot的方式,在yml文件中配置相关数据如下:)
1.使用传统的pom的方式构建generator生成器

 <plugin>
                <groupId>org.mybatis.generator</groupId>`在这里插入代码片`
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
            </plugin>

然后添加generatorConfig.xml配置文件,在其中添加注释如下:

<generatorConfiguration>
    <!-- 其中classPathEntry 是引用的jdbc的类路径 -->
    <classPathEntry
    //这里的路径。尽量不要写一个死的东西,使用配置文件的方式,或者使用jar包引入的方式
            location="F:\maven\repo\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.CachePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <!-- javaTypeResolver式类型转换的信息 -->
        <!-- <commentGenerator> <property name="suppressAllComments" value="true"
            /> </commentGenerator> -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://47.93.190.46:3306/supervision" userId="jinsui"
                        password="jinsui123">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- Bean路径设置 -->
        <javaModelGenerator targetPackage="com.jwkj.api.web.module.dao.mapper"
                            targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 映射文件XML路径设置 -->
        <sqlMapGenerator targetPackage="mybatis.mappers.datum"
                         targetProject="src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 对应的Mapper路径设置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.jwkj.api.web.module.dao.mapper"
                             targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 数据库表 与对应的Bean设置 -->
        <!-- -->
        <!-- 以上已经生成 -->
        <table tableName="module_tree" domainObjectName="Module"></table>
        <!-- <table tableName="menu_sort_type" domainObjectName="HotelSortType"></table>
            <table tableName="menu_type" domainObjectName="HotelType"></table> <table
            tableName="user_saved_menu" domainObjectName="UserSavedHotel"></table> -->
        <!-- <table tableName="menu_rooms" domainObjectName="HotelRooms"></table> -->
    </context>
</generatorConfiguration>

最后使用Maven的generator的插件即可生成代码:

2.使用springboot的方式整合 mybatis的gernerator
使用yml配置文件的方式

mybatis:
  mapper-locations: classpath*:mapper/**/*.xml
  config-location: classpath:mybatis/config.xml

mybatis-plus:
  mapper-locations: classpath*:mapper/**/*.xml
  config-location: classpath:mybatis/config.xml
  type-aliases-package: com.jwkj.tt.api.quality.pojo.entity
  global-config:
    db-config:
      id-type: auto
      field-strategy: not_empty
    db-column-underline: true
  type-enums-package: com.baomidou.mybatisplus.core.enums

然后其他步骤和上述一样,调用Maven的generator插件
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值