IBATIS簡介
ibatis是 Apache的開源項目,一個ORM 解決方案,ibatis最大的特點就是小巧,上手很快。
使用 ibatis提供的ORM機制,對業務邏輯實現人員而言,面對的是純粹的Java對象,這一層與通過Hibernate 實現ORM而言是基本一致的。
iBatis是一個基于SQL映射支持Java和·NET的持久層框架,相對Hibernate和ApacheOJB等“一站式”ORM解決方案而言,iBatis 是一種“半自動化”的ORM實現。
一、JAR包依賴
ibatis-2.3.4.726.jar
mysql-connector-java-5.0.8-bin.jar
二、SqlMap.properties
1
2
3
4
|
driver = com.mysql.jdbc.Driver url = jdbc:mysql: / / 127.0 . 0.1 : 3306 / test username = root password = root |
三、SqlMapConfig.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> < sqlMapConfig > <!-- 引用JDBC屬性的配置文件 --> < properties resource = "com/ligang/SqlMap.properties" /> <!-- 使用JDBC的事務管理 --> < transactionManager type = "JDBC" > <!-- 數據源 --> < dataSource type = "SIMPLE" > < property name = "JDBC.Driver" value = "${driver}" /> < property name = "JDBC.ConnectionURL" value = "${url}" /> < property name = "JDBC.Username" value = "${username}" /> < property name = "JDBC.Password" value = "${password}" /> </ dataSource > </ transactionManager > <!-- 這里可以寫多個實體的映射文件 --> < sqlMap resource = "com/ligang/Student.xml" /> </ sqlMapConfig > |
四、Student.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
55
56
57
58
59
60
61
62
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> < sqlMap > <!-- 通過typeAlias使得我們在下面使用Student實體類的時候不需要寫包名 --> < typeAlias alias = "Student" type = "com.ligang.Student" /> <!-- id表示select里的sql語句,resultClass表示返回結果的類型 --> < select id = "findAll" resultClass = "Student" > select * from student </ select > <!-- parameterClass表示參數的內容 --> < select id = "findByID" parameterClass = "String" resultClass = "Student" > select * from student where id = #id# </ select > < insert id = "insertStudent" parameterClass = "Student" > insert into Student(id,name,age,address) values(#id#,#name#,#age#,#address#) <!-- 返回自動增長值 --> < selectKey resultClass = "String" keyProperty = "id" > select @@identity as inserted </ selectKey > </ insert > < delete id = "deleteStudentByID" parameterClass = "String" > delete from student where id = #id# </ delete > < delete id = "deleteStudent" parameterClass = "Student" > delete from Student where id = #id# </ delete > < update id = "updateStudent" parameterClass = "Student" > update student set name=#name#,age=#age#,address=#address# where id = #id# </ update > <!-- 模糊查詢,使用$代替#。此種方法就是去掉了類型檢查,使用字符串連接,不過可能會有sql注入風險--> < select id = "selectByLike" parameterClass = "String" resultClass = "Student" > select * from student where name like '%$name$%' </ select > <!-- 多條件組合查詢 --> <!-- 方法一(對象構造查詢參數) --> <!-- 項目中在寫ibatis中的sql語句時,where user_id in (#user_id_list# ),運行時總是不行,這里不該用#,而應該用$,區別如下: 1.#是把傳入的數據當作字符串,如#user_id_list#傳入的是1,2,則sql語句生成是這樣,in ('1,2') ,當然不可以 2.$傳入的數據直接生成在sql里,如#user_id_list#傳入的是1,2,則sql語句生成是這樣,in(1,2) 這就對了. 3.#方式能夠很大程度防止sql注入. 4.$方式無法方式sql注入. 5.$方式一般用于傳入數據庫對象.例如傳入表名. 6.一般能用#的就別用$. 直觀的說 #str# 出來的效果是 'str' $str$ 出來的效果是 str 另外 ##只能用在特定的幾個地方 $$可以用在任何地方 比如 order by $str$ 你甚至可以直接寫 $str$ 把 order by 這個字串放在str里傳進來 --> < select id = "findByCon1" parameterClass = "Student" resultClass = "Student" > select * from student where name like '%$name$%' and age >= #age# </ select > <!-- 方法二(map封裝查詢參數) --> < parameterMap class = "java.util.HashMap" id = "paramMap" > < parameter property = "name" /> < parameter property = "age" /> </ parameterMap > < select id = "findByCon2" parameterMap = "paramMap" resultClass = "Student" > select * from student where name like ? and age >= ? </ select > </ sqlMap > |
五、JAVA代碼
實體類:略
Dao:略
DaoImpl:
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
|
package com.ligang; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class StudentDaoImpl implements StudentDao { public static SqlMapClient sqlMapClient = null ; static { try { Reader reader = Resources.getResourceAsReader( "com/ligang/SqlMapConfig.xml" ); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); } catch (IOException e) { e.printStackTrace(); } } public List<Student> findAll() { List<Student> list = null ; try { list = sqlMapClient.queryForList( "findAll" ); } catch (SQLException e) { e.printStackTrace(); } return list; } public Student findByID(String id){ Student student = null ; try { student = (Student) sqlMapClient.queryForObject( "findByID" , id); } catch (SQLException e) { e.printStackTrace(); } return student; } public void addStudent(Student student){ try { sqlMapClient.insert( "insertStudent" ,student); } catch (SQLException e) { e.printStackTrace(); } } public void deleteStudentByID(String id){ try { sqlMapClient.delete( "deleteStudentByID" ,id); } catch (SQLException e) { e.printStackTrace(); } } public void deleteStudent(Student student){ try { sqlMapClient.delete( "deleteStudent" ,student); } catch (SQLException e) { e.printStackTrace(); } } public void updateStudent(Student student){ try { sqlMapClient.update( "updateStudent" , student); } catch (SQLException e) { e.printStackTrace(); } } public List<Student> findByCon(String name){ List<Student> stuList = new ArrayList<Student>(); try { stuList = sqlMapClient.queryForList( "selectByLike" ,name); } catch (SQLException e) { e.printStackTrace(); } return stuList; } public List<Student> findByCon(Student student){ List<Student> stuList = new ArrayList<Student>(); try { stuList = sqlMapClient.queryForList( "findByCon1" ,student); } catch (SQLException e) { e.printStackTrace(); } return stuList; } public List<Student> findByCon(Map map){ List<Student> stuList = new ArrayList<Student>(); try { stuList = sqlMapClient.queryForList( "findByCon2" ,map); } catch (SQLException e) { e.printStackTrace(); } return stuList; } } |
總結
通過學習我們會發現,Hibernate體系中的內容真的很多,而ibatis更容易上手,小巧靈活。本文有關ibatis搭建Java項目的介紹就到這里,希望對大家有所幫助。
原文鏈接:http://blog.csdn.net/ligang2585116/article/details/43410697