eclipse插件(二)——Mybatis-Generator

一、前言

  Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,而由于手动书写很容易出错,我们可以在eclipse中安装Mybatis-Generator插件来帮我们自动生成文件。

二、使用方法

1.在线安装

在Help--Eclipser Marketplace中查找:Mybatis Generator 1.3.5,点击Installed

2.在eclipse中使用

New--other--输入关键字

 

 

在Location中选择应用的项目

 

项目目录下就多出来generatorConfig.xml文件

打开generatorConfig.xml,具体内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 3 <generatorConfiguration>
 4     <!-- 可以在此处引入配置文件 -->
 5     <!-- <properties resource="jdbc.properties"/> -->
 6 
 7     <!-- 数据库驱动 -->
 8     <classPathEntry
 9         location="D:\MavenRepository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar" />
10     <context id="context1">
11         <commentGenerator>
12             <!-- 抑制警告 -->
13             <!-- <property name="suppressTypeWarnings" value="true" /> -->
14             <!-- 是否生成注释代时间戳 -->
15             <property name="suppressDate" value="true" />
16             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
17             <property name="suppressAllComments" value="true" />
18         </commentGenerator>
19 
20         <!-- 数据库连接信息,也可以通过properties文件配置 -->
21         <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/user"
22             driverClass="com.mysql.jdbc.Driver" password="root" userId="root" />
23 
24         <!-- 生成模型的包名和位置 targetPackage:生成entity类的包名 targetProject:包所在的位置,最好使用相对路径 -->
25         <javaModelGenerator targetPackage="com.sun.entity"
26             targetProject="SSHDemo/src/main/java">
27             <!-- enableSubPackages:是否让schema作为包的后缀 -->
28             <property name="enableSubPackages" value="false" />
29             <!-- 从数据库返回的值被清理前后的空格 -->
30             <property name="trimStrings" value="true" />
31         </javaModelGenerator>
32 
33         <!-- 生成映射文件的包名和位置 -->
34         <sqlMapGenerator targetPackage="com.sun.mapper"
35             targetProject="SSHDemo/src/main/java">
36             <property name="enableSubPackages" value="false" />
37         </sqlMapGenerator>
38 
39         <!-- 生成DAO的包名和位置 -->
40         <javaClientGenerator targetPackage="com.sun.dao"
41             targetProject="SSHDemo/src/main/java" type="XMLMAPPER">
42             <property name="enableSubPackages" value="true" />
43         </javaClientGenerator>
44 
45         <table schema="general" tableName="user" domainObjectName="User"
46             enableCountByExample="false" enableUpdateByExample="false"
47             enableDeleteByExample="false" enableSelectByExample="false"
48             selectByExampleQueryId="false">
49             <!--entity字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名 -->
50             <property name="useActualColumnNames" value="false" />
51             <!-- 忽略列,不生成bean 字段 -->
52             <!-- <ignoreColumn column="FRED" /> -->
53             <!-- 指定列的java数据类型 -->
54             <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
55         </table>
56     </context>
57 </generatorConfiguration>

右击generatorConfig.xml,选择Generate MyBaties/iBATIS Artifacts

然后就可以在项目下发现生成好了包和相应的文件

 

posted @ 2017-10-24 18:13  孤岛千峰  阅读(75)  评论(0)    收藏  举报