MybatisGenerator具体使用小技巧_第1页
MybatisGenerator具体使用小技巧_第2页
MybatisGenerator具体使用小技巧_第3页
MybatisGenerator具体使用小技巧_第4页
MybatisGenerator具体使用小技巧_第5页
已阅读5页,还剩2页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第MybatisGenerator具体使用小技巧目录1、问题描述2、解决方案2.1pom中指定mybatisgenerator插件2.2generatorConfig.xml2.3执行2.4总结

1、问题描述

mybatisgenerator简单说就是生成一些mybatis的公共方法,用了好多年了,一直也没记录过,最近使用稍微有了点变话,简单记录下,方便下次使用,也希望可以帮到其他人。

2、解决方案

最近使用主要有两点变化:

(1)以前使用,指定了本地数据库驱动jar包,本次直接用maven,dependency指定下就好了,配置文件配置好后,团队人员都可以执行;

(2)发现朋友使用的方式挺好的,就是初期开发过程中,不可避免的会增加或修改一些字段,xml中新加或者重新生成方法,都有点麻烦,通过新增一个新的类,继承属性,可以尽可能减少改动;

2.1pom中指定mybatisgenerator插件

plugins

plugin

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-maven-plugin/artifactId

/plugin

plugin

groupIdorg.mybatis.generator/groupId

artifactIdmybatis-generator-maven-plugin/artifactId

version1.4.0/version

configuration

configurationFilesrc/main/resources/generatorConfig.xml/configurationFile

verbosetrue/verbose

overwritetrue/overwrite

/configuration

dependencies

dependency

groupIdmysql/groupId

artifactIdmysql-connector-java/artifactId

version8.0.25/version

/dependency

/dependencies

/plugin

/plugins

说明:

在插件中指定(dependency)数据库的驱动,示例中使用的是:mysql,oracel数据库更换成oracle驱动就好了;

2.2generatorConfig.xml

xmlversion="1.0"encoding="UTF-8"

!DOCTYPEgeneratorConfiguration

PUBLIC"-////DTDMyBatisGeneratorConfiguration1.0//EN"

"/dtd/mybatis-generator-config_1_0.dtd"

generatorConfiguration

contextid="DB2Tables"targetRuntime="MyBatis3"

commentGenerator

!--是否去除自动生成的注释true:是:false:否--

propertyname="suppressAllComments"value="true"/

/commentGenerator

jdbcConnectiondriverconnectionURL="jdbc:mysql://:3306/ruanjianlaowang"

userId="laowang"

password="laowang"

/jdbcConnection

javaTypeResolver

propertyname="forceBigDecimals"value="false"/

/javaTypeResolver

javaModelGeneratortargetPackage="com.laowang.lwcrud.db.entity"targetProject="./src/main/java"

propertyname="enableSubPackages"value="true"/

propertyname="trimStrings"value="true"/

/javaModelGenerator

sqlMapGeneratortargetPackage="com.laowang.lwcrud.db.mapper"targetProject="./src/main/java"

propertyname="enableSubPackages"value="true"/

/sqlMapGenerator

javaClientGeneratortype="XMLMAPPER"targetPackage="com.laowang.lwcrud.db.mapper"targetProject="src/main/java"

propertyname="enableSubPackages"value="true"/

/javaClientGenerator

tabletableName="user"domainObjectName="User"enableInsert="true"

enableDeleteByPrimaryKey="true"

enableSelectByPrimaryKey="true"

enableUpdateByPrimaryKey="true"

enableCountByExample="false"

enableDeleteByExample="true"

enableSelectByExample="true"

enableUpdateByExample="true"

propertyname="useActualColumnNames"value="false"/

!--generatedKeycolumn="id"sqlStatement="MYSQL"identity="true"/--

/table

/context

/generatorConfiguration

说明:

(1)jdbcConnectionjdbcConnection,用于指定数据库连接;

(2)javaModelGenerator/javaModelGenerator指定entity目录;

(3)javaClientGeneratortype="XMLMAPPER"/javaClientGenerator指定Mapper接口目录;

(4)sqlMapGenerator/sqlMapGenerator指定xml文件类目录;

(5)table/table指定具体数据表,其中的几个参数自己可以试试,带ByExample是生成示例,有些朋友喜欢用这个方式,个人还是比较喜欢直接用原始sql,加参数的方式;

(6)这里有个点,table中的generatedKey,假如数据库用自增id的话,可以选择这个,使用uuid的话,注释掉就好了。

2.3执行

2.3.1双击插件

文件生成完成:

2.3.2生成实体类

生成两个实体类;对应数据库中字段;

2.3.3生成mapper类

生成mapper方法,包含常用的保存、更新、删除等;

2.3.4生成xml类

生成xml方法,对应Mapper接口方法;

2.3.5生成扩展文件(mapper)

新增扩展文件,在原有mapper后面增加Ext;

2.3.6生成扩展文件(extmapper.xml)

说明:

扩展类继承,自动生成的属性,这个新增或者修改的属性同时会生效;

mappernamespace="com.laowang.lwcrud.db.mapper.UserExtMapper"

resultMapid="BaseResultMap"type="com.laowang.lwcrud.db.entity.User"

ex

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论