最近做项目,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

    

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

 

generatorConfig.xml 配置:

   1 <?xml version="1.0" encoding="UTF-8"?>

 2 <!DOCTYPE generatorConfiguration
 3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 
 6 <generatorConfiguration>
 7     <!-- 配置文件路径 -->
 8     <properties resource="mybatis_generatorConfig.properties" />
 9     <!--数据库驱动包路径 -->
10     <classPathEntry location="${drive.class.path}" />
11 
12     <context >
13         <!--关闭注释 -->
14         <commentGenerator>
15             <property name="suppressDate" value="true" />

<property name="suppressAllComments" value="true"/>
16         </commentGenerator>
17 
18         <!--数据库连接信息 -->
19         <jdbcConnection driverClass="${jdbc.driver}"
20             connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">
21         </jdbcConnection>
22          <!--targetPackage 表示包名    targetProject:表示项目名称 -->
23         <!--生成的model 包路径 -->
24         <javaModelGenerator targetPackage="${model.package}"
25             targetProject="${target.project}">
26             <property name="enableSubPackages" value="ture" />
27             <property name="trimStrings" value="true" />
28         </javaModelGenerator>
29 
30         <!--生成xml mapper文件 路径 -->
31         <sqlMapGenerator targetPackage="${xml.mapper.package}"
32             targetProject="${target.project}">
33             <property name="enableSubPackages" value="ture" />
34         </sqlMapGenerator>
35 
36         <!-- 生成的Dao接口 的包路径 -->
37         <javaClientGenerator type="XMLMAPPER"
38             targetPackage="${dao.package}" targetProject="${target.project}">
39             <property name="enableSubPackages" value="ture" />
40         </javaClientGenerator>
41 
42         <!--对应数据库表名 tableName    domainObjectName 表示生成的domain 名-->
43         <table tableName="teacher" domainObjectName="teacher" enableCountByExample="false"
44             enableUpdateByExample="false" enableDeleteByExample="false"
45             enableSelectByExample="false" selectByExampleQueryId="false">
46         </table>
47     </context>
48 </generatorConfiguration>

 最后右键generatorConfig.xml  选择Generate

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

 

相关文章:

  • 2022-02-15
  • 2021-07-04
猜你喜欢
  • 2021-06-14
相关资源
相似解决方案