Mybatis Generator 安装(idea+maven)
1.在Intellij IDEA创建maven项目(本过程比较简单,略)
2. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件
<build><!--mybatis逆向工程插件-->
<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>
</build>
3.用了外置的配置文件jdbc.properties,可以将一下属性配置到properties文件之中,模板如下:
driver=org.postgresql.Driver
url=jdbc:postgresql://192.168.31.101:5432/DataBase
usernames=userName
password=password
driverLocation=src/main/webapp/lib/postgresql-42.2.2.jre7.jar
4.在maven项目下的src/main/resources 目录下建立名为 generatorConfig.xml的配置文件,作为mybatis-generator-maven-plugin 插件的执行目标,模板如下:
简单模版:
<?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="jdbc.properties"/> <!--指定特定数据库的jdbc驱动jar包的位置-->
<classPathEntry location="${driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator> <!--jdbc的数据库连接 -->
<jdbcConnection
driverClass="${driver}"
connectionURL="${url}"
userId="${usernames}"
password="${password}">
</jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!--
Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径
-->
<!--<javaModelGenerator targetPackage="com.demo.pojo" targetProject=".\src\main\java">-->
<javaModelGenerator targetPackage="com.bean" targetProject="./src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
<!-- 是否对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="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</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.demo.dao" targetProject=".\src\main\java">-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="./src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!--数据库对应数据表-->
<table tableName="数据库表名字" domainObjectName="SingleLib" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table> <!-- mybatis插件的搭建 -->
</context>
</generatorConfiguration>
复杂配置:
<?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="jdbc.properties"></properties> <!--指定特定数据库的jdbc驱动jar包的位置 -->
<classPathEntry location="${jdbc.driverLocation}"/>
<context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator>
<property name="suppressDate" value="true" />
</commentGenerator> <!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}">
</jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径
-->
<javaModelGenerator targetPackage="com.bean" targetProject="./src/main/java">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/> <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/> <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="true"/> <!-- 给Model添加一个父类 -->
<property name="rootClass" value="com.foo.louis.Hello"/> <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="mapping" 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接口
-->
<javaClientGenerator targetPackage="com.dao" targetProject="./src/main/java" type="MIXEDMAPPER">
<property name="enableSubPackages" value=""/>
<!--
定义Maper.java 源代码中的ByExample() 方法的可视性,可选的值有:
public;
private;
protected;
default
注意:如果 targetRuntime="MyBatis3",此参数被忽略
-->
<property name="exampleMethodVisibility" value=""/>
<!--方法名计数器
Important note: this property is ignored if the target runtime is MyBatis3.
-->
<property name="methodNameCalculator" value=""/> <!-- 为生成的接口添加父接口-->
<property name="rootInterface" value=""/> </javaClientGenerator> <!--数据库对应数据表-->
<table schema="" tableName="表名" domainObjectName="实体类名" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table> <table schema="louis" tableName="user" domainObjectName="User" >
<!-- optional , only for mybatis3 runtime
自动生成的键值(identity,或者序列值)
如果指定此元素,MBG将会生成<selectKey>元素,然后将此元素插入到SQL Map的<insert> 元素之中
sqlStatement 的语句将会返回新的值
如果是一个自增主键的话,你可以使用预定义的语句,或者添加自定义的SQL语句. 预定义的值如下:
Cloudscape This will translate to: VALUES IDENTITY_VAL_LOCAL()
DB2: VALUES IDENTITY_VAL_LOCAL()
DB2_MF: SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
Derby: VALUES IDENTITY_VAL_LOCAL()
HSQLDB: CALL IDENTITY()
Informix: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
MySql: SELECT LAST_INSERT_ID()
SqlServer: SELECT SCOPE_IDENTITY()
SYBASE: SELECT @@IDENTITY
JDBC: This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. This is a database independent method of obtaining the value from identity columns.
identity: 自增主键 If true, then the column is flagged as an identity column and the generated <selectKey> element will be placed after the insert (for an identity column). If false, then the generated <selectKey> will be placed before the insert (typically for a sequence).
-->
<generatedKey column="" sqlStatement="" identity="" type=""/>
<!-- optional.
列的命名规则:
MBG使用 <columnRenamingRule> 元素在计算列名的对应 名称之前,先对列名进行重命名,
作用:一般需要对BUSI_CLIENT_NO 前的BUSI_进行过滤
支持正在表达式
searchString 表示要被换掉的字符串
replaceString 则是要换成的字符串,默认情况下为空字符串,可选
-->
<columnRenamingRule searchString="" replaceString=""/> <!-- optional.告诉 MBG 忽略某一列
column,需要忽略的列
delimitedColumnName:true ,匹配column的值和数据库列的名称 大小写完全匹配,false 忽略大小写匹配
是否限定表的列名,即固定表列在Model中的名称
-->
<ignoreColumn column="PLAN_ID" delimitedColumnName="true" /> <!--optional.覆盖MBG对Model 的生成规则
column: 数据库的列名
javaType: 对应的Java数据类型的完全限定名
在必要的时候可以覆盖由JavaTypeResolver计算得到的java数据类型. For some databases, this is necessary to handle "odd" database types (e.g. MySql's unsigned bigint type should be mapped to java.lang.Object).
jdbcType:该列的JDBC数据类型(INTEGER, DECIMAL, NUMERIC, VARCHAR, etc.),该列可以覆盖由JavaTypeResolver计算得到的Jdbc类型,对某些数据库而言,对于处理特定的JDBC 驱动癖好 很有必要(e.g. DB2's LONGVARCHAR type should be mapped to VARCHAR for iBATIS).
typeHandler:
-->
<columnOverride column="" javaType="" jdbcType="" typeHandler="" delimitedColumnName="" /> </table>
</context>
</generatorConfiguration>
5.在webapp下新建lib包,并把数据库驱动jar包放在里面,项目目录如下:
6.使用
Mybatis Generator 安装(idea+maven)的更多相关文章
- IDEA Maven Mybatis generator 自动生成代码
IDEA Maven Mybatis generator 自动生成代码 一.安装配置maven以及在Idea中配置maven 安装过程步骤可以看上面的博文,里面介绍得很详细. 二.建数据表 DROP ...
- 2016.7.12 eclipse和IDEA中mybatis generator插件的安装与使用
Eclipse中的安装 http://jingyan.baidu.com/article/9faa7231506ed8473c28cbee.html 1.下载插件 2.将插件generator的fea ...
- mybatis generator maven插件自动生成代码
如果你正为无聊Dao代码的编写感到苦恼,如果你正为怕一个单词拼错导致Dao操作失败而感到苦恼,那么就可以考虑一些Mybatis generator这个差价,它会帮我们自动生成代码,类似于Hiberna ...
- MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子
什么是MyBatis Generator MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以自动生成一些简单的CRUD(插入,查询,更新,删除)操作代码,model ...
- mybatis generator使用(基于maven)
1.添加maven依赖 <dependency> <groupId>org.mybatis.generator</groupId> <artifactId&g ...
- Maven多模块项目使用MyBatis Generator
开发环境: JDK:8u102 Maven:3.3.9 MySQL:5.7.10 MySQL Connector:5.1.40 IDE:IntelliJ IDEA 2016 MyBatis:3.4.1 ...
- MyEclipse下安装MyBatis Generator代码反向生成工具
一.离线方式: 在http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/下载 featur ...
- mybatis generator 插件安装及使用
现在Mybatis特别火,但是在开发中却要经常写实体类和配置文件,会不会特别烦人,所以可以利用Mybatis的代码生成插件来生成这部分代码: 1,打开eclipse,点击Help>Softwar ...
- idea + mybatis generator + maven 插件使用
idea + mybatis generator + maven 插件使用 采用的是 generator 的 maven 插件的方式 ~ 1 pom.xml mybatis其它配置一样,下面是配置my ...
随机推荐
- 转 Jmeter业务请求比例
[转载]Jmeter业务请求比例1 ps:文章转自订阅号“测试那点事儿”,链接:https://mp.weixin.qq.com/s/qVD4iNO0QqRIwAIq9_E_Kw 方法二: 可 ...
- 【题解】sweet
题目描述 为了防止糖果被小猫偷吃,John把他的糖果放在了很多的高台上,一个高台可以认为是一段平行于X轴的线段,并且高台的y坐标都是大于0的,每个高台都有左端点和高台的长度,每个高台都有糖果.所有的高 ...
- Neo4J空间数据存储
1.Neo4j Spatial 简介 1.1Neo4j Spatial概念 Neo4j Spatial项目是图数据库Neo4j的一个插件,它通过将空间数据映射到图模型(graph model),它将对 ...
- web服务器和应用服务器以及web应用框架介绍
### web服务器:负责处理http请求,响应静态文件,常见的有Apache,Nginx以及微软的IIS. ### 应用服务器:负责处理逻辑的服务器.比如php.python的代码,是不能直接通过n ...
- sqlServer MERGE 对数据进行修改或插入自动视别 特别用于批量数据的插入或修改
sqlServer MERGE 对数据进行修改或插入自动视别 特别用于批量数据的插入或修改 MERGE customer AS targetTable --目标表 USING ( ...
- C#5.0 异步编程 Async和Await--异步方法的规范和注意事项
要些异步方法要注意一下几点: 异步方法的返回值有三种: 1.没有任何返回值的void 2.返回一个Task任务的Task,可以获得该异步方法的执行状态 3.返回Task<T> 可以获得异步 ...
- MyBatis使用注解方式实现CRUD操作
一.使用注解后就不需要写SysGroupDaoMapper.xml 只需要在Dao的抽象方法前加上相应的注解就可以. package cn.mg39.ssm01.dao; import java.ut ...
- 2018-2-13-win10-uwp-改变鼠标
title author date CreateTime categories win10 uwp 改变鼠标 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...
- JS window对象 Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL。 语法: location.[属性|方法]
Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL. 语法: location.[属性|方法] location对象属性图示: location 对象属性: lo ...
- 判断list中元素是否是相邻
private static List<Integer> findShunZi(List<Integer> tmpCards){ List<Integer> lis ...