Mybatis generator自动生成mybatis配置和类信息
自动生成代码方式两种:
1、命令形式生成代码,详细讲解每一个配置参数。
2、Eclipse利用插件形式生成代码。
安装插件方式:
eclipse插件安装地址:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
附件有link安装包,link安装方式参考http://maimode.iteye.com/admin/blogs/1164524
MyBatis Generator详细介绍参见:http://code.google.com/p/mybatis/wiki/Generator
安装插件的过程就不说了,安装完后,eclipse中File-》new-》other中会发现多了mybatis选项说明插件安装成功
如何使用插件
在任意项目中利用上图中的向导创建generatorConfig.xml文件(名称可修改)然后修改文件内容,主要是设置连接数据的相关参数:
generator自动生成mybatis的xml配置、model、map等信息:
1、下载mybatis-generator-core-1.3.2.jar包。
网址:http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGenerator,下载mybatis-generator-core-1.3.2-bundle.zip,将mybatis-generator-core-1.3.2.jar放到项目中
2、编写generatorConfig的xml文件,名下:generatorConfig.xml
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="init.properties"/><!-- 指定数据连接驱动jar地址 --><classPathEntry location="E:\workspace\mysql-connector-java-5.1.24-bin.jar"/><!-- 一个数据库一个context --><context id="infoGuardian" targetRuntime="MyBatis3"><!-- 注释 --><commentGenerator > <property name="suppressAllComments" value="false"/><!-- 是否取消注释 --><property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳--></commentGenerator> <!-- jdbc连接 --><jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" /> <!-- 类型转换 --><javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com.common.user.model" targetProject="basic-user-web\src\main\java" > <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true"/> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="com.common.user.Mapper" targetProject="basic-user-web\src\main\java" > <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com.user.model" targetProject="basic-user-web\src\main\resources" type="XMLMAPPER" > <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 配置表信息 --> <!--<table schema="cap_common" tableName="mosf_common_user" domainObjectName="User"> enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> --> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample是否生成 example类 --> <!-- 忽略列,不生成bean 字段 --> <!-- <ignoreColumn column="FRED" /> --> <!-- 指定列的java数据类型 --> <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> <!-- </table> --> <table schema="" tableName="mosf_common_user" domainObjectName="UserModel" delimitIdentifiers="true" delimitAllColumns="true" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /></context> </generatorConfiguration>
table其他属性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类,
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true,
这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时,
就不会生成对应的Example类了.
如果table里边不配置property,默认字段都生成为类属性。
<ignoreColumn column="FRED" />//忽略字段
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。
点击generatorConfig.xml右键上的选项,如果配置正确,便自动创建相关文件了。
文件主要有三类:
client包,mapper 接口文件
model包,实体bean文件
mapper包,mapper xml文件
转载于:https://www.cnblogs.com/564085446java/p/3831651.html
Mybatis generator自动生成mybatis配置和类信息相关推荐
- generator自动生成mybatis配置和类信息
generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://cod ...
- SpringBoot如何自动生成实体类和Dao层以及映射文件(mybatis generator 自动生成代码)
一.首先添加自动生成代码插件 <!-- mybatis generator 自动生成代码插件 生成时解除注释 --><plugin><groupId>org.myb ...
- generator自动生成mybatis的xml配置
generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://cod ...
- mybatis generator 自动生成代码(带注释的实体类)
使用前提: 当你开发的java 项目或新模块的数据库有N张表操作时,这时要自己写实体类.dao.SqlMapper.xml等文件,如果有多个表,就是造成时间浪费降低开发效率,所以建议使用mybatis ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
我们这一一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池的好处我 ...
- IDEA使用Mybatis Generator自动生成部分代码
IDEA使用Mybatis Generator自动生成部分代码 说明:使用Mybatis Generator代码生成配置,简单生成部分后端代码,包括(实体类,Mapper.Mapper.xml文件) ...
- 使用MyBatis Generator自动生成持久层CRUD代码的两种方法
最近在使用MyBatis,得知可以利用MyBatis Generator自动生成实体类.DAO接口和Mapping映射文件.当数据库中的表多的时候,让你不用再手写Mapping映射文件,和实体类,就可 ...
- mybatis generator自动生成sqlmap代码的不完善之处以及解决方法
mybatis generator自动生成sqlmap代码的不完善之处以及解决方法 参考文章: (1)mybatis generator自动生成sqlmap代码的不完善之处以及解决方法 (2)http ...
- idea 集成mybatis,利用MyBatis Generator自动生成实体类、mapper文件
最近一个老项目集成mybatis,利用 generator自动生成实体类.mapper的时候折腾了一小时,记录一下,避免以后再折腾 很简单的三步 https://gitee.com/shunangua ...
最新文章
- sskeychain使用(轻量级框架)
- golang获取md5
- 115.什么是SHELL
- Linux的apache的allowoverwrite参数的解释
- 剪贴板所有api函数
- C#调用bat 不显示DOS窗口,禁止DOS窗口一闪而过
- 三星Galaxy S22 Ultra真机首曝:颜值与实力并存堪称完美
- SQL注入攻击测试入实战(1)—成功渗透台湾某净化设备公司官网
- 8.Linux的LVM使用详解
- koa项目用mongoose与mongodb交互,始终报错FormModel is not defined
- esp8266教程:编译sdk常用命令
- win10系统点电脑无线图标没反应的,点设置里的显示可用网络没反应
- php apm,apm是什么?
- java 检测点击事件控件_iCheck控件ifClicked和ifChanged事件的讨论
- CVE-2021-44228 Log4j 远程代码执行漏洞——原理
- 双击打开Excel2016文件后无法直接显示文件内容的解决办法
- 【突变检验合集】含Pettitt突变检验等
- 基因家族的鉴定-基于Windows系统上的HMMER
- 【C#】CurrentCulture和CurrentUICulture的区别及winform多语言版本设置
- Python 使用 opencv 库将BMP格式图片转RAW