iBatis 简介:
iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。
官网为:http://www.mybatis.org/
搭建iBatis 开发环境:
1 、导入相关的jar 包,ibatis-2.3.0.677.jar 、mysql-connector-java-5.1.6-bin.jar
2 、编写配置文件:
Jdbc 连接的属性文件
总配置文件, SqlMapConfig.xml
关于每个实体的映射文件(Map 文件)
Demo :
Student.java:
01 | package com.iflytek.entity; |
06 | * @author xudongwang 2011-12-31 |
08 | * Email:xdwangiflytek@gmail.com |
12 | // 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题 |
22 | public void setId( int
id) { |
26 | public String getName() { |
30 | public void setName(String name) { |
34 | public Date getBirth() { |
38 | public void setBirth(Date birth) { |
42 | public float getScore() { |
46 | public void setScore( float
score) { |
51 | public String toString() { |
52 | return "id=" + id +
"\tname=" + name + "\tmajor=" + birth +
"\tscore=" |
SqlMap.properties :
1 | driver=com.mysql.jdbc.Driver |
2 | url=jdbc:mysql://localhost:3306/ibatis |
Student.xml :
01 | <? xml version = "1.0"
encoding = "UTF-8" ?> |
02 | <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" |
03 | "http://ibatis.apache.org/dtd/sql-map-2.dtd"> |
06 | <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 --> |
07 | < typeAlias alias = "Student"
type = "com.iflytek.entity.Student" /> |
09 | <!-- 这样以后改了sql,就不需要去改java代码了 --> |
10 | <!-- id表示select里的sql语句,resultClass表示返回结果的类型 --> |
11 | < select id = "selectAllStudent"
resultClass = "Student" > |
16 | <!-- parameterClass表示参数的内容 --> |
17 | <!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 --> |
18 | < select id = "selectStudentById"
parameterClass = "int" resultClass = "Student" > |
19 | select * from tbl_student where id=#id# |
22 | <!-- 注意这里的resultClass类型,使用Student类型取决于queryForList还是queryForObject --> |
23 | < select id = "selectStudentByName"
parameterClass = "String" |
24 | resultClass = "Student" > |
25 | select name,birth,score from tbl_student where name like |
29 | < insert id = "addStudent"
parameterClass = "Student" > |
31 | tbl_student(name,birth,score) values |
32 | (#name#,#birth#,#score#); |
33 | < selectKey resultClass = "int"
keyProperty = "id" > |
34 | select @@identity as inserted |
35 | <!-- 这里需要说明一下不同的数据库主键的生成,对各自的数据库有不同的方式: --> |
36 | <!-- mysql:SELECT LAST_INSERT_ID() AS VALUE --> |
37 | <!-- mssql:select @@IDENTITY as value --> |
38 | <!-- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL --> |
39 | <!-- 还有一点需要注意的是不同的数据库生产商生成主键的方式不一样,有些是预先生成 (pre-generate)主键的,如Oracle和PostgreSQL。
|
40 | 有些是事后生成(post-generate)主键的,如MySQL和SQL Server 所以如果是Oracle数据库,则需要将selectKey写在insert之前 --> |
44 | < delete id = "deleteStudentById"
parameterClass = "int" > |
45 | <!-- #id#里的id可以随意取,但是上面的insert则会有影响,因为上面的name会从Student里的属性里去查找 --> |
46 | <!-- 我们也可以这样理解,如果有#占位符,则ibatis会调用parameterClass里的属性去赋值 --> |
47 | delete from tbl_student where id=#id# |
50 | < update id = "updateStudent"
parameterClass = "Student" > |
51 | update tbl_student set |
52 | name=#name#,birth=#birth#,score=#score# where id=#id# |
说明:
如果xml 中没有ibatis 的提示,则window --> Preference--> XML-->XML Catalog---> 点击add
选择uri URI: 请选择本地文件系统上
iBatisDemo1/WebContent/WEB-INF/lib/sql-map-config-2.dtd 文件;
Key Type: 选择Schema Location;
Key: 需要联网的,不建议使用;
SqlMapConfig.xml :
01 | <? xml version = "1.0"
encoding = "UTF-8" ?> |
02 | <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" |
03 | "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> |
06 | <!-- 引用JDBC属性的配置文件 --> |
07 | < properties resource = "com/iflytek/entity/SqlMap.properties"
/> |
09 | < transactionManager type = "JDBC" > |
11 | < dataSource type = "SIMPLE" > |
12 | < property name = "JDBC.Driver"
value = "${driver}" /> |
13 | < property name = "JDBC.ConnectionURL"
value = "${url}" /> |
14 | < property name = "JDBC.Username"
value = "${username}" /> |
15 | < property name = "JDBC.Password"
value = "${password}" /> |
18 | <!-- 这里可以写多个实体的映射文件 --> |
19 | < sqlMap resource = "com/iflytek/entity/Student.xml"
/> |
StudentDao :
01 | package com.iflytek.dao; |
05 | import com.iflytek.entity.Student; |
08 | * @author xudongwang 2011-12-31 |
10 | * Email:xdwangiflytek@gmail.com |
13 | public interface StudentDao { |
22 | public boolean addStudent(Student student); |
31 | public boolean deleteStudentById( int
id); |
40 | public boolean updateStudent(Student student); |
47 | public List<Student> selectAllStudent(); |
56 | public List<Student> selectStudentByName(String name); |
65 | public Student selectStudentById( int
id); |
StudentDaoImpl :
001 | package com.iflytek.daoimpl; |
003 | import java.io.IOException; |
004 | import java.io.Reader; |
005 | import java.sql.SQLException; |
006 | import java.util.List; |
008 | import com.ibatis.common.resources.Resources; |
009 | import com.ibatis.sqlmap.client.SqlMapClient; |
010 | import com.ibatis.sqlmap.client.SqlMapClientBuilder; |
011 | import com.iflytek.dao.StudentDao; |
012 | import com.iflytek.entity.Student; |
015 | * @author xudongwang 2011-12-31 |
017 | * Email:xdwangiflytek@gmail.com |
020 | public class StudentDaoImpl implements
StudentDao { |
022 | private static SqlMapClient sqlMapClient =
null ; |
027 | Reader reader = Resources |
028 | .getResourceAsReader( "com/iflytek/entity/SqlMapConfig.xml" ); |
029 | sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); |
031 | } catch (IOException e) { |
036 | public boolean addStudent(Student student) { |
037 | Object object = null ; |
038 | boolean flag = false ; |
040 | object = sqlMapClient.insert( "addStudent" , student); |
041 | System.out.println( "添加学生信息的返回值:"
+ object); |
042 | } catch (SQLException e) { |
045 | if (object != null ) { |
051 | public boolean deleteStudentById( int
id) { |
052 | boolean flag = false ; |
053 | Object object = null ; |
055 | object = sqlMapClient.delete( "deleteStudentById" , id); |
056 | System.out.println( "删除学生信息的返回值:"
+ object + ",这里返回的是影响的行数" ); |
057 | } catch (SQLException e) { |
060 | if (object != null ) { |
068 | public boolean updateStudent(Student student) { |
069 | boolean flag = false ; |
070 | Object object = false ; |
072 | object = sqlMapClient.update( "updateStudent" , student); |
073 | System.out.println( "更新学生信息的返回值:"
+ object + ",返回影响的行数" ); |
074 | } catch (SQLException e) { |
077 | if (object != null ) { |
083 | public List<Student> selectAllStudent() { |
084 | List<Student> students = null ; |
086 | students = sqlMapClient.queryForList( "selectAllStudent" ); |
087 | } catch (SQLException e) { |
093 | public List<Student> selectStudentByName(String name) { |
094 | List<Student> students = null ; |
096 | students = sqlMapClient.queryForList( "selectStudentByName" ,name); |
097 | } catch (SQLException e) { |
103 | public Student selectStudentById( int
id) { |
104 | Student student = null ; |
106 | student = (Student) sqlMapClient.queryForObject( |
107 | "selectStudentById" , id); |
108 | } catch (SQLException e) { |
TestIbatis.java :
01 | package com.iflytek.test; |
06 | import com.iflytek.daoimpl.StudentDaoImpl; |
07 | import com.iflytek.entity.Student; |
10 | * @author xudongwang 2011-12-31 |
12 | * Email:xdwangiflytek@gmail.com |
15 | public class TestIbatis { |
17 | public static void
main(String[] args) { |
18 | StudentDaoImpl studentDaoImpl = new
StudentDaoImpl(); |
20 | System.out.println( "测试插入" ); |
21 | Student addStudent = new
Student(); |
22 | addStudent.setName( "李四" ); |
23 | addStudent.setBirth(Date.valueOf( "2011-09-02" )); |
24 | addStudent.setScore( 88 ); |
25 | System.out.println(studentDaoImpl.addStudent(addStudent)); |
27 | System.out.println( "测试根据id查询" ); |
28 | System.out.println(studentDaoImpl.selectStudentById( 1 )); |
30 | System.out.println( "测试模糊查询" ); |
31 | List<Student> mohuLists = studentDaoImpl.selectStudentByName( "李" ); |
32 | for (Student student : mohuLists) { |
33 | System.out.println(student); |
36 | System.out.println( "测试查询所有" ); |
37 | List<Student> students = studentDaoImpl.selectAllStudent(); |
38 | for (Student student : students) { |
39 | System.out.println(student); |
42 | System.out.println( "根据id删除学生信息" ); |
43 | System.out.println(studentDaoImpl.deleteStudentById( 1 )); |
45 | System.out.println( "测试更新学生信息" ); |
46 | Student updateStudent = new
Student(); |
47 | updateStudent.setId( 1 ); |
48 | updateStudent.setName( "李四1" ); |
49 | updateStudent.setBirth(Date.valueOf( "2011-08-07" )); |
50 | updateStudent.setScore( 21 ); |
51 | System.out.println(studentDaoImpl.updateStudent(updateStudent)); |
iBatis 的优缺点:
优点:
1、 减少代码量,简单;
2、 性能增强;
3、 Sql 语句与程序代码分离;
4、 增强了移植性;
缺点:
1、 和Hibernate 相比,sql 需要自己写;
2、 参数数量只能有一个,多个参数时不太方便;