1、需要加載的包如下
spring.jar
commons-logging-1.0.4.jar
ibatis-2.3.3.720.jar
commons-dbcp.jar
commons-pool.jar
mysql-connector-java-5.0.8-bin.jar
2、創建數據庫

author.sql
1
create table author(
2
id int auto_increment primary key,
3
name varchar(20),
4
address varchar(20),
5
telphone varchar(11))
3、一個簡單的javabean:com.sailor.vo.Author.java

Author.java
1
/** *//**
2
*
3
*/
4
package com.sailor.vo;
5
6
/** *//**
7
* @author sailor
8
*
9
*/
10
public class Author implements java.io.Serializable
{
11
12
/** *//**
13
*
14
*/
15
private static final long serialVersionUID = -2722692654067301576L;
16
private int id;
17
private int age;
18
private String name;
19
private String address;
20
private String telephone;
21
22
public Author()
{
23
}
24
25
26
public Author(int id, int age, String name, String address, String telephone)
{
27
super();
28
this.id = id;
29
this.age = age;
30
this.name = name;
31
this.address = address;
32
this.telephone = telephone;
33
}
34
35
36
/** *//**
37
* @return the id
38
*/
39
public int getId()
{
40
return id;
41
}
42
/** *//**
43
* @param id the id to set
44
*/
45
public void setId(int id)
{
46
this.id = id;
47
}
48
/** *//**
49
* @return the age
50
*/
51
public int getAge()
{
52
return age;
53
}
54
/** *//**
55
* @param age the age to set
56
*/
57
public void setAge(int age)
{
58
this.age = age;
59
}
60
/** *//**
61
* @return the name
62
*/
63
public String getName()
{
64
return name;
65
}
66
/** *//**
67
* @param name the name to set
68
*/
69
public void setName(String name)
{
70
this.name = name;
71
}
72
/** *//**
73
* @return the address
74
*/
75
public String getAddress()
{
76
return address;
77
}
78
/** *//**
79
* @param address the address to set
80
*/
81
public void setAddress(String address)
{
82
this.address = address;
83
}
84
/** *//**
85
* @return the telephone
86
*/
87
public String getTelephone()
{
88
return telephone;
89
}
90
/** *//**
91
* @param telephone the telephone to set
92
*/
93
public void setTelephone(String telephone)
{
94
this.telephone = telephone;
95
}
96
97
98
/**//* (non-Javadoc)
99
* @see java.lang.Object#toString()
100
*/
101
@Override
102
public String toString()
{
103
return "ID: " + this.id + " name: " + this.getName() +
104
" age: " + this.age + " tel: " + this.telephone + " address: " + this.address;
105
}
106
}
107
4、配置sqlMap

author.xml
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE sqlMap
3
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
4
"http://www.ibatis.com/dtd/sql-map-2.dtd">
5
6
7
<sqlMap namespace="Author">
8
9
<!-- 定義類的別名 -->
10
<typeAlias alias="Author" type="com.sailor.vo.Author" />
11
12
<cacheModel id="authorCache" type="LRU">
13
<flushInterval hours="24"/>
14
<flushOnExecute statement="insertAuthor"/>
15
<flushOnExecute statement="updateAuthor"/>
16
<flushOnExecute statement="deleteAuthor"/>
17
<property name="size" value="1000" />
18
</cacheModel>
19
20
<resultMap id="authorResult" class="Author">
21
<result property="id" column="auth_id" jdbcType="int" javaType="java.lang.Integer"/>
22
<result property="name" column="auth_name" jdbcType="VARCHAR" javaType="java.lang.String"/>
23
<result property="age" column="auth_age" jdbcType="int" javaType="java.lang.Integer"/>
24
<result property="telephone" column="auth_tel" jdbcType="VARCHAR" javaType="java.lang.String"/>
25
<result property="address" column="auth_address" jdbcType="VARCHAR" javaType="java.lang.String"/>
26
</resultMap>
27
28
<parameterMap id="insert-para" class="Author">
29
<parameter property="id"/>
30
<parameter property="name"/>
31
<parameter property="age"/>
32
<parameter property="telephone"/>
33
<parameter property="address"/>
34
</parameterMap>
35
36
<!-- 按ID返回該作者詳細信息 -->
37
<select id="getAuthor" parameterClass="int" resultClass="Author">
38
SELECT auth_id as id,
39
auth_name as name,
40
auth_age as age,
41
auth_tel as telephone,
42
auth_address as address
43
FROM author WHERE auth_id = #id#
44
</select>
45
46
<!-- 列出所有的作者 -->
47
48
<statement id="getAllAuthor" resultMap="authorResult" cacheModel="authorCache">
49
<!-- <select id="getAllAuthor" resultClass="java.util.HashMap"> -->
50
SELECT * FROM author
51
</statement>
52
53
54
<!-- 新增作者 -->
55
<insert id="insertAuthor" parameterClass="Author">
56
INSERT INTO author (auth_id, auth_name,auth_age,auth_tel,auth_address) VALUES (#id#, #name#, #age#, #telephone#, #address#)
57
<selectKey resultClass="int" type="post" keyProperty="id" >
58
select LAST_INSERT_ID() as value
59
</selectKey>
60
</insert>
61
62
<!--
63
<statement id="insertAuthor" parameterMap="insert-para">
64
INSERT INTO author (auth_id, auth_name,auth_age,auth_tel,auth_address) VALUES (?, ?, ?, ?, ?)
65
<selectKey resultClass="int" type="post" keyProperty="id" >
66
select LAST_INSERT_ID() as value
67
</selectKey>
68
</statement>
69
-->
70
71
72
<!-- 更新作者信息 -->
73
<update id="updateAuthor" parameterClass="Author">
74
UPDATE author set auth_name=#name# WHERE auth_id = #id#
75
</update>
76
77
<!-- 按ID刪除作者信息 -->
78
<delete id="deleteAuthor" parameterClass="int">
79
delete from author WHERE auth_id = #id#
80
</delete>
81
82
</sqlMap>
83
84

sqlMapConfig.xml
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE sqlMapConfig
3
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
4
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
5
6
<!-- Always ensure to use the correct XML header as above! -->
7
8
<sqlMapConfig>
9
10
<!-- These settings control SqlMapClient configuration details, primarily to do with transaction
11
management. They are all optional (more detail later in this document). -->
12
13
<settings
14
cacheModelsEnabled="true"
15
enhancementEnabled="true"
16
lazyLoadingEnabled="true"
17
maxRequests="32"
18
maxSessions="10"
19
maxTransactions="5"
20
useStatementNamespaces="false"
21
/>
22
23
24
<!-- Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths
25
are relative to the classpath. For now, we only have one… -->
26
<sqlMap resource="com/sailor/dao/author.xml" />
27
28
</sqlMapConfig>
29

IAuthor.java
1
/** *//**
2
*
3
*/
4
package com.sailor.dao;
5
6
import java.util.List;
7
8
import com.sailor.vo.Author;
9
10
/** *//**
11
* @author sailor
12
* Sep 5, 2008 4:23:39 PM
13
*/
14
public interface IAuthorDAO
{
15
16
/** *//**
17
* 增加作者
18
* @param author
19
*/
20
public void saveAuthor(Author author);
21
22
/** *//**
23
* 修改作者
24
* @param author
25
*/
26
public void updateAuthor(Author author);
27
28
/** *//**
29
* 刪除作者
30
* @param author
31
*/
32
public void deleteAuthor(Author author);
33
34
/** *//**
35
* 獲得List
36
* @return
37
*/
38
public List<Author> getAllAuthor();
39
40
/** *//**
41
* 按ID查看
42
* @param id
43
* @return
44
*/
45
public Author getAuthorById(int id);
46
}
47

AuthorDAO.java
1
/** *//**
2
*
3
*/
4
package com.sailor.dao;
5
6
import java.util.List;
7
8
import org.springframework.orm.ibatis.SqlMapClientTemplate;
9
10
import com.sailor.vo.Author;
11
12
/** *//**
13
* @author sailor
14
* Sep 5, 2008 4:24:57 PM
15
*/
16
public class AuthorDAO implements IAuthorDAO
{
17
18
private SqlMapClientTemplate sqlMapClientTemplate;
19
20
public SqlMapClientTemplate getSqlMapClientTemplate()
{
21
return sqlMapClientTemplate;
22
}
23
24
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate)
{
25
this.sqlMapClientTemplate = sqlMapClientTemplate;
26
}
27
28
/** *//**
29
* 刪除一個作者
30
*/
31
public void deleteAuthor(Author author)
{
32
33
sqlMapClientTemplate.delete("deleteAuthor", author);
34
}
35
36
37
/** *//**
38
* 獲取List列表
39
*/
40
@SuppressWarnings("unchecked")
41
public List<com.sailor.vo.Author> getAllAuthor()
{
42
43
return sqlMapClientTemplate.queryForList("getAllAuthor");
44
}
45
46
47
/** *//**
48
* 按ID查詢詳細信息
49
*/
50
public com.sailor.vo.Author getAuthorById(int id)
{
51
52
return (Author)sqlMapClientTemplate.queryForObject("getAuthor", new Integer(id));
53
}
54
55
/** *//**
56
* 保存作者詳細信息
57
*/
58
public void saveAuthor(Author author)
{
59
60
sqlMapClientTemplate.insert("insertAuthor", author);
61
}
62
63
/** *//**
64
* 更新作者信息
65
*/
66
public void updateAuthor(Author author)
{
67
68
sqlMapClientTemplate.update("updateAuthor", author);
69
}
70
}
71

applicationContext.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans
3
xmlns="http://www.springframework.org/schema/beans"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
6
7
<!-- 配置數據源 -->
8
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
9
<property name="driverClassName">
10
<value>com.mysql.jdbc.Driver</value>
11
</property>
12
<property name="username">
13
<value>root</value>
14
</property>
15
<property name="password">
16
<value>sa</value>
17
</property>
18
<property name="url">
19
<value>jdbc:mysql://localhost:3306/test</value>
20
</property>
21
</bean>
22
23
24
<!-- 配置Dao層 -->
25
26
<!--根據dataSource和configLocation創建一個SqlMapClient-->
27
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
28
<property name="configLocation">
29
<!-- 本Demo是個application,如果是個web Project,路徑應為/WEB-INF/lib/classes/SqlMapConfig.xml -->
30
<value>SqlMapConfig.xml</value>
31
</property>
32
<property name="dataSource">
33
<ref bean="dataSource" />
34
</property>
35
</bean>
36
37
<!--根據sqlMapClien創建一個SqlMapClient模版類-->
38
<bean id="sqlMapClientTemplate"
39
class="org.springframework.orm.ibatis.SqlMapClientTemplate">
40
<property name="sqlMapClient">
41
<ref bean="sqlMapClient" />
42
</property>
43
</bean>
44
45
<!--將上面的模版類織入到我們的DAO對象中-->
46
<bean id="authorDAO" class="com.sailor.dao.AuthorDAO">
47
<property name="sqlMapClientTemplate">
48
<ref bean="sqlMapClientTemplate" />
49
</property>
50
</bean>
51
52
53
</beans>
54
8、客戶端測試

TestClient.java
1
import java.util.List;
2
3
import org.springframework.context.ApplicationContext;
4
import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6
7
import com.sailor.dao.AuthorDAO;
8
import com.sailor.vo.Author;
9
10
/** *//**
11
*
12
*/
13
14
/** *//**
15
* @author sailor
16
* Sep 5, 2008 4:46:30 PM
17
*/
18
public class TestClient
{
19
20
/** *//**
21
* @param args
22
*/
23
public static void main(String[] args)
{
24
25
System.out.println("讀取配置文件");
26
ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
27
AuthorDAO authorDao = (AuthorDAO)factory.getBean("authorDAO");
28
System.out.println("get List");
29
List<Author> list = authorDao.getAllAuthor();
30
System.out.println("print List");
31
for(Author author : list)
{
32
System.out.println(author);
33
}
34
}
35
}
36
spring.jar
commons-logging-1.0.4.jar
ibatis-2.3.3.720.jar
commons-dbcp.jar
commons-pool.jar
mysql-connector-java-5.0.8-bin.jar
2、創建數據庫


1

2

3

4

5

3、一個簡單的javabean:com.sailor.vo.Author.java


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

48


49

50

51



52

53

54


55

56

57



58

59

60


61

62

63



64

65

66


67

68

69



70

71

72


73

74

75



76

77

78


79

80

81



82

83

84


85

86

87



88

89

90


91

92

93



94

95

96

97

98


99

100

101

102



103

104

105

106

107

4、配置sqlMap


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

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

5、配置SqlMapConfig.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

6、編寫dao 文件


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



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


48

49

50



51

52

53

54

55


56

57

58



59

60

61

62

63


64

65

66



67

68

69

70

71

7、配置applicationContext.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

48

49

50

51

52

53

54

8、客戶端測試


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
