使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件
2018-01-24 13:46
1206 查看
前言:
mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件、对应的dao接口文件以及对应的entity实体(bean)问题解决:1、如果遇到“不能有前言”的错误,请将配置文件重新保存为UTF-8(不包含BOM)文字编码的文件
一、首先,我们需要引入所需要的jar包
1、mybatis-generator所需的jar包
mybatis-generator-core-1.3.5.jar (mybatis-generator-core的版本可以自行选择)mybatis-generator下载地址:https://github.com/mybatis/generator/releases
2、数据库连接jar包
根据自己的需要选择数据库驱动oracle的驱动:ojdbc6.jar
mysql驱动:mysql-connector-java.jar
postgre驱动:postgresql-9.3-1102.jdbc4.jar
使用postgre进行举例
二、Mybatis-Generator配置文件详解
1、xml配置文件头
[html] viewplain copy
print?
<?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>
<!--这里写配置-->
</generatorConfiguration>
2、指定数据库驱动包位置
以ojdbc6.jar为例,支持zip和jar两种文件格式[html] view
plain copy
print?
<!-- 指定数据连接驱动jar地址 -->
<!-- <classPathEntry location="${classPath}" /> -->
<classPathEntry location="D:\Repository\Publish\ojdbc6.jar" />
3、数据库连接参数和自动生成参数配置
一个完整的配置实例:[html] view
plain copy
print?
<?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>
<!-- 指定数据连接驱动jar地址 -->
<classPathEntry location="E:\mybatis-generator\postgresql-9.3-1102.jdbc4.jar" />
<context id="SqlTables" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /><!-- 是否生成注释代时间戳 -->
</commentGenerator>
<jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://127.0.0.1:5432/JS_VNMP5.1" userId="postgres" password="123456" />
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="cc.eguid.entity"
targetProject="E:\mybatis-generator\src\main\java" >
<property name="enableSubPackages" value="false"/>
<!--设置父类 <property name="rootClass" value="cc.eguid.entity.baseEntity"/> -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="cc.eguid.dao"
targetProject="E:\mybatis-generator\src\main\java" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<span style="white-space:pre"> </span><property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="cc.eguid.dao"
targetProject="E:\mybatis-generator\src\main\java" type="XMLMAPPER" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 用户权限表 -->
<table tableName="eguid_userrole" domainObjectName="Userrole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
三、通过命令行运行
1、执行命令(generatorConfig.xml为配置文件)java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
2、建议在mybatis-generator-core-1.3.5.jar所在文件夹下新建一个xxx.bat文件,里面放入上面的命令,就可以方便自动生成
完成上述操作后,执行xxx.bat文件即可在对应文件夹中生成三个文件,xxxmapper.xml,xxxmapper.java,xxx.java
注意:请务必保证要生成的路径已经存在,否则会执行失败
3、一个完整的xxx.bat执行文件
[javascript] view
plain copy
print?
@echo off
java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
pause
四、使用总结
1、引入mybatis-generator的jar包和数据库驱动jar包2、配置mybatis-generator的配置文件
2.1、指定数据库驱动jar包的位置和数据库连接配置
2.2、配置实体类生成器(javaModelGenerator),可以通过配置rootClass属性为实体类指定继承的父类
2.3、配置SQL映射文件生成器(sqlMapGenerator)
2.4、配置dao层接口生成器(javaClientGenerator)
3、使用命令行执行mybatis-generator即可生成
相关文章推荐
- 简单三步快速学会使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件(postgre使用实例)
- 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)
- 使用Mybatis生成工具自动生成实体类和对应的mapper映射文件以及接口文件
- Mybatis--使用mybatis generator插件映射数据库,自动生成pojo对象,dao接口,mapper.xml文件的方法
- 使用mybatis-generator自动生成实体类,接口实现类和Mapper映射配置文件
- 通过mybatis工具generatorConfig.xml自动生成实体,DAO,映射文件
- 整合maven+mybatis+generator生成java自定义model实体类,dao接口和mapper映射文件
- 利用generator自动生成model(实体)、dao(接口)、mapper(映射)
- 通过mybatis工具generatorConfig.xml自动生成实体,DAO,映射文件
- 2分钟使用mybatisGenerator自动生成dao和mapper文件——使用cmd命令行
- 使用mybatis的generator自动生成po及mapper映射文件
- 如何用MyBatis-Generator自动创建代码(映射生成实体类、DAO接口和Mapping映射文件)
- mybatis逆向工程自动生成实体类、接口以及映射Mapper.xml配置文件
- 使用Mybatis-Generator自动生成Dao、model和mapper.xml相关文件
- 使用Mybatis-Generator自动生成Dao、model和mapper.xml相关文件
- 使用Mybatis-Generator逆向生成po,映射文件和mapper接口
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- Mybatis使用generator自动生成映射配置文件信息
- 使用mybatis自带工具,自动生成表对应domain、mapper.xml以及dao