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"
?>
<
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
=
"******"
/>
<
javaModelGenerator
targetPackage
=
"com.ibator.model"
targetProject
=
"IbatorDemo"
/>
<
sqlMapGenerator
targetPackage
=
"com.ibator.mapper"
targetProject
=
"IbatorDemo"
/>
<
daoGenerator
targetPackage
=
"com.ibator.dao"
targetProject
=
"IbatorDemo"
implementationPackage
=
"com.ibator.dao.impl"
type
=
"IBATIS"
/>
<
table
schema
=
""
tableName
=
"role"
>
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"
?>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<
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
>
<
javaModelGenerator
targetPackage
=
"com.ibator.web.model"
targetProject
=
"src/main/java"
>
<
property
name
=
"enableSubPackages"
value
=
"ture"
/>
<
property
name
=
"trimStrings"
value
=
"true"
/>
javaModelGenerator
>
<
sqlMapGenerator
targetPackage
=
"com.ibator.web.dao"
targetProject
=
"src/main/java"
>
<
property
name
=
"enableSubPackages"
value
=
"ture"
/>
sqlMapGenerator
>
<
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