MyBatis自动生成

mybatis-gennerator 自动生成插件可以自动生成项目中所须要的dao、bean、mapper xml文件。java

mybatis-gennerator 的使用主要是generatorConfig.xml 的配置mysql

咱们能够把编写好的generatorConfig.xml配置文件放到eclipse下任意项目的下面sql

下面是配置文件的具体内容数据库

<?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 >
    <classPathEntry    location="D:\Util\maven\maven-dependcies\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar"/>
  <context id="context1" >
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test" userId="test" password="test" />
    <javaModelGenerator targetPackage="com.aspire.comp.esp.common.entity" targetProject="test" />
    <sqlMapGenerator targetPackage="com.aspire.comp.esp.common.dao.mysql" targetProject="test" />
    <javaClientGenerator targetPackage="com.aspire.comp.esp.common.dao" targetProject="test" type="XMLMAPPER" />
    <table schema="test" tableName="a8_company_fre_app_activity" domainObjectName="FreAppActivity"
            enableCountByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" enableUpdateByExample="false"
            selectByExampleQueryId="false">
            	<generatedKey column="id" sqlStatement="mysql"  identity="true"/>
     	
    </table>
    <table schema="test" tableName="a8_company_fre_weixin_activity" domainObjectName="FreWeiXinActivity"
            enableCountByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" enableUpdateByExample="false"
            selectByExampleQueryId="false">
            	<generatedKey column="id" sqlStatement="mysql"  identity="true"/>
     	
    </table>
   
  </context>
</generatorConfiguration>

咱们解释下上面的配置文件 classPathEntry 是本地MySQL链接工具的路径
<context>是一个大目录 表明一个数据库 配置文件能够有多个context 
context 下面的目录
jdbcConnection 数据库地址
javaModelGenerator javaClientGenerator sqlMapGenerator 分别表明的是实体类,Java接口,XML配置文件
targetProject 表明项目名 targetPackage 包路径

而后table 节点表明数据库中表的具体映射 tableName 是代表 domainObjectName 是生成的实体类名
enableCountByExample 为是否生成测试类 项目中通常都是 false 
generatedKey 是主键自动生成(适用于自增)

在generatorConfig.xml 配置文件上右键执行 便能自动生成所需的实体类、接口、配置文件了mybatis

相关文章
相关标签/搜索