Mybatis Generator 使用(Maven插件)
来源:互联网 发布:喜马拉雅网络加载失败 编辑:程序博客网 时间:2024/05/18 18:14
最近搭一个项目后台框架,项目采用SSM框架,项目比较紧急,所以选用Mybatis Generator来自动生成XML、Dao和Model来减少重复的工作。
Mybatis Generator有以下两种使用方法:
- 直接下载相关jar包 下载地址:https://github.com/mybatis/generator/releases
- 使用maven中的generator插件
为了保证项目方便管理,所以采用了Maven插件的方式使用Mybatis Generator来自动生成代码。
导入Mybatis Generator插件
在pom.xml增加以下插件:
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal> generate </goal> </goals> </execution> </executions> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
配置Mybatis Generator插件
生成的xml存放在src\main\resources路径下 :
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" ><generatorConfiguration> <!-- java连接数据库jar所在路径 --> <classPathEntry location="E:\OpenCode\weixin\src\main\resources\mysql-connector-java-5.1.30.jar" /> <context id="context1" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://120.25.89.67:3306/db_iyiqiba" userId="ZSQ1" password="123" /> <!-- mybatis里专门用来处理NUMERIC和DECIMAL类型的策略 --> <javaTypeResolver> <property name="forceBigDecimals" value="true" /> </javaTypeResolver> <!-- 生成model类的包 和路径 --> <javaModelGenerator targetPackage="com.iyiqiba.dao.model" targetProject="src\main\java" /> <!-- 生成Mapper.xml 所在路径下面配置路径为src\main\resources\ibatis-generator --> <sqlMapGenerator targetPackage="ibatis-generator" targetProject="src\main\resources" /> <!-- 生成Dao接口所在包和文件路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.iyiqiba.dao" targetProject="src\main\java"> </javaClientGenerator> <!-- tableName:表名 domainObjectName:对应数据库表的model类名称--> <table tableName="tb_student_base_info" domainObjectName="StudentBaseInfo"> <!-- 如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate --> <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_student_detaill_info" domainObjectName="StudentDetaillInfo" > <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_business_base_info" domainObjectName="BusinessBaseInfo" > <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_business_detaill_info" domainObjectName="BusinessDetaillInfo" > <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_third_user_info" domainObjectName="ThirdUserInfo"> <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_activity_comment" domainObjectName="ActivityComment"> <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_activity_info" domainObjectName="ActivityInfo"> <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_activity_join" domainObjectName="ActivityJoin"> <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_activity_mark" domainObjectName="ActivityMark" > <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_address_info" domainObjectName="AddressInfo"> <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_activity_placard" domainObjectName="ActivityPlacard" > <property name="useActualColumnNames" value="false"/> </table> <table tableName="tb_kind_info" domainObjectName="KindInfo" > <property name="useActualColumnNames" value="false"/> </table> </context></generatorConfiguration>
这里并没有屏蔽Example,很多人不想生成Example可能感觉比较杂乱。
可以通过在<table/>
中配置enableUpdateByExample
等信息来去掉Example。
<table tableName="tb_business_base_info" domainObjectName="BusinessBaseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <property name="useActualColumnNames" value="false"/> </table>
mybatis generator目标就是减少Dao层的工作量,针对这一点建议保留Example,如果不保留可能还需要写更多的动态SQL才能完成工作,这样代码自动生成工具的价值就体现不出来了。
因为自动生成代码的时候会覆盖文件,所以建议把手写的动态SQL和自动生成的分成2个Mapper.xml文件,这样数据库添加字段或有其他修改的时候重新生成Mapper.xml不会影响我们手写的动态SQL。
执行maven命令
右键项目–RunAS-Maven build-输入mybatis-generator:generate指令 点击Run 即可成功相关代码。
阅读全文
1 0
- Mybatis Generator 使用(Maven插件)
- maven-mybatis-generator插件使用
- Maven插件mybatis-generator的使用
- idea + mybatis generator + maven 插件使用
- mybatis-generator-maven 插件
- Maven插件之MyBatis Generator
- maven(18)-mybatis generator插件
- IDEA mybatis-generator-maven-plugin 插件的使用
- 在eclipse maven项目中使用mybatis-generator插件
- 使用mybatis-generator插件后的maven打包问题
- Mybatis-Generator使用(maven项目)
- mybatis generator插件的使用 。
- mybatis generator插件的使用
- (八)Intellij mybatis 插件mybatis-generator 使用
- maven插件mybatis-generator自动生成
- maven插件mybatis-generator生成代码配置
- 将mybatis-generator封装成Maven插件
- maven插件mybatis-generator生成代码配置
- OpenCV代码提取:remap函数的实现
- EditText
- ADG swicthover操作(主备切换)
- k8s-configmap 创建
- JAVA源码解析(11)-java.beans.XMLEncoder、java.beans.XMLDecoder(一)
- Mybatis Generator 使用(Maven插件)
- centos7在线lvm扩容
- 系列:iOS开发-从扩展UIButton到自定义控件
- electron安装设置代理的坑
- 使用DWR框架实现服务器推送Demo
- MAC下,使用brew安装软件时总提示更新问题
- shell脚本基础知识
- 电容的选择
- 三、Apache的安装