前言:
mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件、对应的dao接口文件以及对应的entity实体(bean)
一、首先,我们需要引入所需要的jar包
1、mybatis-generator所需的jar包
mybatis-generator-core-1.3.2.jar (mybatis-generator-core的版本可以自行选择)
2、数据库连接jar包
比如Oracle数据库用ojdbc6.jar或者MySQL的mysql-connector-Java.jar,版本依据数据库自行选择
二、Mybatis-Generator配置文件详解
1、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>
- <!--这里写配置-->
- </generatorConfiguration>
2、配置数据库连接jar包所在位置
以ojdbc6.jar为例
- <!-- 指定数据连接驱动jar地址 -->
- <!-- <classPathEntry location="${classPath}" /> -->
- <classPathEntry location="D:\Repository\Publish\ojdbc6.jar" />
3、数据库连接参数和自动生成参数配置
详细配置的含义看对应注释
- <context id="SqlTables" targetRuntime="MyBatis3">
- <!-- 注释 -->
- <commentGenerator>
- <property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
- <property name="suppressDate" value="true" /><!-- 是否生成注释代时间戳 -->
- </commentGenerator>
- <!-- jdbc连接 -->
- <!-- <jdbcConnection driverClass="${jdbc_driver}" -->
- <!-- connectionURL="${jdbc_url}" userId="${jdbc_user}" -->
- <!-- password="${jdbc_password}" /> -->
- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
- connectionURL="jdbc:oracle:thin:@//localhost:1521/orcl" userId="admin"
- password="123456" />
- <!-- 类型转换 -->
- <javaTypeResolver>
- <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
- <!-- 生成实体类地址 targetProject是生成文件的存放位置,targetPackage是生成文件的所在packet-->
- <javaModelGenerator targetPackage="cc.eguid.blog.entity"
- targetProject="D:Repository\eguid-blog-entity\src\main\java" >
- <property name="enableSubPackages" value="false"/>
- <property name="rootClass" value="com.itssky.aqjg.entity.base.BaseInfo"/>
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <!-- 生成mapxml文件 -->
- <sqlMapGenerator targetPackage="cc.eguid.blog.dao.mapper"
- targetProject="D:\Repository\eguid-blog-dao\src\main\java" >
- <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
- <property name="enableSubPackages" value="false"/>
- </sqlMapGenerator>
- <!-- 生成mapxml对应client,也就是接口dao -->
- <javaClientGenerator targetPackage="cc.eguid.blog.dao"
- targetProject="D:\Repository\aqjg\eguid-blog-dao\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>
- <!-- 配置表 -->
- <!--tableName对应表名,domainObjectName是实体类名 xxxxxByExample这几个是是否生成选择性增删改查mapper-->
- <table tableName="B_DBGL_PINGSYBAXX" domainObjectName="Pingsybaxx"
- enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" selectByExampleQueryId="false"></table>
- </context>
三、通过命令行运行
1、命令(generatorConfig.xml为配置文件)
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
2、建议在mybatis-generator-core-1.3.2.jar所在文件夹下新建一个xxx.bat文件,里面放入上面的命令,就可以方便自动生成
四、完整配置例子
- <?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="generatorConfig.properties" /> -->
- <!-- 指定数据连接驱动jar地址 -->
- <!-- <classPathEntry location="${classPath}" /> -->
- <classPathEntry location="D:\blessedRepository\Publish\ojdbc6.jar" />
- <context id="SqlTables" targetRuntime="MyBatis3">
- <!-- 注释 -->
- <commentGenerator>
- <property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
- <property name="suppressDate" value="true" /><!-- 是否生成注释代时间戳 -->
- </commentGenerator>
- <!-- jdbc连接 -->
- <!-- <jdbcConnection driverClass="${jdbc_driver}" -->
- <!-- connectionURL="${jdbc_url}" userId="${jdbc_user}" -->
- <!-- password="${jdbc_password}" /> -->
- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
- connectionURL="jdbc:oracle:thin:@//localhost:1521/orcl" userId="admin"
- password="123456" />
- <!-- 类型转换 -->
- <javaTypeResolver>
- <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
- <!-- 生成实体类地址 targetProject是生成文件的存放位置,targetPackage是生成文件的所在packet-->
- <javaModelGenerator targetPackage="cc.eguid.blog.entity"
- targetProject="D:Repository\eguid-blog-entity\src\main\java" >
- <property name="enableSubPackages" value="false"/>
- <property name="rootClass" value="com.itssky.aqjg.entity.base.BaseInfo"/>
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <!-- 生成mapxml文件 -->
- <sqlMapGenerator targetPackage="cc.eguid.blog.dao.mapper"
- targetProject="D:\Repository\eguid-blog-dao\src\main\java" >
- <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
- <property name="enableSubPackages" value="false"/>
- </sqlMapGenerator>
- <!-- 生成mapxml对应client,也就是接口dao -->
- <javaClientGenerator targetPackage="cc.eguid.blog.dao"
- targetProject="D:\Repository\aqjg\eguid-blog-dao\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>
- <!-- 配置表 -->
- <!--tableName对应表名,domainObjectName是实体类名 xxxxxByExample这几个是是否生成选择性增删改查mapper-->
- <table tableName="B_DBGL_PINGSYBAXX" domainObjectName="Pingsybaxx"
- enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" selectByExampleQueryId="false"></table>
- </context>
- </generatorConfiguration>