最近做项目,mapping 有点多而且容易写错,于是试着用了Mybatis-Generator 插件自动生成 dao, domain  mapping 文件。感觉还挺好用。把相关配置分享,一边以后做项目的时候直接拿来用。  我用的是eclipse Mybatis-Generator 插件。

      环境:eclipse  Mars.2

      插件:org.mybatis.generator.eclipse.site-1.3.5.201609070108   

      数据库jar:mysql-connector-java-5.1.26-bin.jar

        数据库表:

CREATE TABLE `teacher` (
  `id` bigint(20) NOT NULL DEFAULT '0' COMMENT '主键id',
  `name` varchar(40) NOT NULL DEFAULT '' COMMENT '名称',
  `age` smallint(6) NOT NULL DEFAULT '0' COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='教师表';

   关于插件eclipse 自行百度。当安装成功后(重启eclipse ,在新建项目里面有一个mybatis 文件夹说明插件安装成功,如下图),

    首先新建一个Java项目(普通项目 or  web项目)

   点击新建  ---> 选择其他----> 选择mybatis  --->  选择下一步 ---> 选择本地项目       会自动生成一个xml  文件,设置就是在xml 里面完成的

Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件

Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件

 最终目录结构如下图  ,需要说明的是  包必须要自己创建,数据库的jar 包需要自己添加

Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件

    源文件: mybatis_generatorConfig.properties

# 数据库驱动jar 路径
 drive.class.path=F:/Users/zh/workspace/Mybatis_Generator/webapp/WEB-INF/lib/mysql-connector-java-5.1.26-bin.jar
 
 # 数据库连接参数
 jdbc.driver=com.mysql.jdbc.Driver
 jdbc.url=jdbc:mysql://localhost:3306/articl
 jdbc.username=root
 jdbc.password=123
 
 # 包路径配置
 model.package=cn.mg.damain
 dao.package=cn.mg.dao
 xml.mapper.package=cn.mg.dao
 #项目名称
 target.project=Mybatis_Generator

generatorConfig.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="mybatis_generatorConfig.properties" />
     <!--数据库驱动包路径 -->
     <classPathEntry location="${drive.class.path}" />
 
     <context id="MySQLTables" targetRuntime="MyBatis3">
         <!--关闭注释 -->
         <commentGenerator>
             <property name="suppressDate" value="true" />
         </commentGenerator>
 
         <!--数据库连接信息 -->
         <jdbcConnection driverClass="${jdbc.driver}"
             connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">
         </jdbcConnection>
          <!--targetPackage 表示包名    targetProject:表示项目名称 -->
         <!--生成的model 包路径 -->
         <javaModelGenerator targetPackage="${model.package}"
             targetProject="${target.project}">
             <property name="enableSubPackages" value="ture" />
             <property name="trimStrings" value="true" />
         </javaModelGenerator>
 
         <!--生成xml mapper文件 路径 -->
         <sqlMapGenerator targetPackage="${xml.mapper.package}"
             targetProject="${target.project}">
             <property name="enableSubPackages" value="ture" />
         </sqlMapGenerator>
 
         <!-- 生成的Dao接口 的包路径 -->
         <javaClientGenerator type="XMLMAPPER"
             targetPackage="${dao.package}" targetProject="${target.project}">
             <property name="enableSubPackages" value="ture" />
         </javaClientGenerator>
 
         <!--对应数据库表名 tableName    domainObjectName 表示生成的domain 名-->
         <table tableName="teacher" domainObjectName="teacher" enableCountByExample="false"
             enableUpdateByExample="false" enableDeleteByExample="false"
             enableSelectByExample="false" selectByExampleQueryId="false">
         </table>
     </context>
 </generatorConfiguration>

 最后右键generatorConfig.xml  选择Generate

Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件