mybatis代碼生成的2種方式

mybatis使用方便,但是各種配置文件、bean及dao的重複書寫很是煩人。幸好mybatis已經有了自動生成工具:常用的方式有命令行、maven、eclipse插件。下面,將介紹eclipse及maven的方式生成mybatis代碼。

一、eclipse中使用插件生成代碼

1.首先需要在eclipse中安裝ibator插件

        打開網址(http://ibatis.apache.org/tools/ibator/)


根據說明,在eclipse中安裝ibator插件,然後重啓eclipse。

2.驗證ibator插件是否安裝成功

        在eclipse中依次點擊:File–New–other,在搜索框中輸入ib,如果出現如下所示,則說明安裝成功。


3.新建java項目,在src下右鍵New–Other


點擊Next,在File Name中輸入ibatorConfig.xml,點擊完成,就會在src下生存所需的配置文件。

4.ibatorConfig.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN" "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd" >
<ibatorConfiguration>
<classPathEntry location="E:\***\sources\mysql\mysql-connector-java-5.1.15-bin.jar" />
<ibatorContext id="context1">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/spring4mybatis"
userId="root" password="******" />
 
<!-- javaBean生成器 targetPackage:包名,targetProject:項目名 -->
<javaModelGenerator targetPackage="com.ibator.model"
targetProject="IbatorDemo" />
 
<!-- 映射文件生成器 -->
<sqlMapGenerator targetPackage="com.ibator.mapper"
targetProject="IbatorDemo" />
 
<!-- dao生成器,type 指定生成dao類的模板,可選擇IBATIS、SPRING、GENERIC-CI、GENERIC-SI implementationPackage
dao實現類的包名 -->
<daoGenerator targetPackage="com.ibator.dao"
targetProject="IbatorDemo" implementationPackage="com.ibator.dao.impl"
type="IBATIS" />
 
<!-- 表名屬性映射 tableName爲表名,可使用SQL通配符%和_,domainObjectName爲對應類名,如不寫則默認和表名相同 -->
<table schema="" tableName="role">
 
<!-- columnOverride 指定屬性名稱,不指定則以默認規則處理:字母先全變小寫,然後去掉下劃線,下劃線後首字母大寫 <columnOverride
column="???" property="???" /> -->
 
<!-- 是否使用列名做屬性名,默認值爲false,處理規則:字母先全變小寫,然後去掉下劃線,下劃線後首字母大寫 <property name="useActualColumnNames"
value="true" /> -->
 
<!-- 指定生成主鍵的語句
<generatedKey column="id" sqlStatement="SELECT LAST_INSERT_ID()+1" />-->
 
<!-- 指定生成列名的 替換規則
<columnRenamingRule searchString="flag"
replaceString="FLAG" />-->
 
<!-- 指定生成的列名
<columnOverride column="id" property="uid" />-->
 
<!-- 忽略掉無需生成的列
<ignoreColumn column="???" />-->
</table>
</ibatorContext>
</ibatorConfiguration>

5.配置完成後,在該文件上右擊,選擇Generate IBATIS Artifacts,然後在IbatorDemo項目下就會看到生成的代碼。


:當使用的daoGenerator類型爲SPRING時,生成的dao所繼承的類爲SqlMapClientDaoSupport,但是在spring 3.2之後該方法已經廢棄,所以如果所用spring版本在3.2之後,應該使用org.mybatis.spring.support.SqlSessionDaoSupport

二、maven生成mybatis代碼

1.新建maven項目

2.修改pom.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>IbatorDemo</groupId>
<artifactId>com.ibator</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>IbatorDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
 
<build>
<finalName>IbatorDemo</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
</plugins>
</build>
</project>

3.在src/main/resources下建立配置文件generatorConfig.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?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">
 
<!-- 配置Run As Maven build : Goals 參數 : mybatis-generator:generate -Dmybatis.generator.overwrite=true -->
<!-- 配置 tableName,使用 Run As Maven build 生成 dao model 層 -->
<generatorConfiguration>
 
<!--數據庫驅動包路徑 -->
<classPathEntry location="E:\****\sources\mysql\mysql-connector-java-5.1.15-bin.jar"/>
 
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--關閉註釋 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
 
<!--數據庫連接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/spring4mybatis" userId="root"
password="******">
</jdbcConnection>
 
<!--生成的model 包路徑 -->
<javaModelGenerator targetPackage="com.ibator.web.model" targetProject="src/main/java">
<property name="enableSubPackages" value="ture"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
 
<!--生成xml mapper文件 路徑 -->
<sqlMapGenerator targetPackage="com.ibator.web.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="ture"/>
</sqlMapGenerator>
 
<!-- 生成的Dao接口 的包路徑 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ibator.web.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="ture"/>
</javaClientGenerator>
 
<!--對應數據庫表名 -->
<table tableName="user">
 
</table>
</context>
</generatorConfiguration>

4.項目上右鍵–run as–maven build


點擊run,完成。


http://www.1024china.com/?p=94

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章