评:
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:
1. package
2.
3. import
4.
5. /**
6. * @author xudongwang 2011-12-31
7. *
8. * Email:xdwangiflytek@gmail.com
9. *
10. */
11. public class
12. // 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题
13. private int
14. private
15. private
16. private float
17.
18. public int
19. return
20. }
21.
22. public void setId(int
23. this.id = id;
24. }
25.
26. public
27. return
28. }
29.
30. public void
31. this.name = name;
32. }
33.
34. public
35. return
36. }
37.
38. public void
39. this.birth = birth;
40. }
41.
42. public float
43. return
44. }
45.
46. public void setScore(float
47. this.score = score;
48. }
49.
50. @Override
51. public
52. return "id=" + id + "\tname=" + name + "\tmajor=" + birth + "\tscore="
53. "\n";
54. }
55.
56. }
SqlMap.properties :
1. driver=com.mysql.jdbc.Driver
2. url=jdbc:mysql://localhost:3306/ibatis
3. username=root
4. password=123
Student.xml :
1. <?xml version="1.0" encoding="UTF-8" ?>
2. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
3. >
4.
5. <sqlMap>
6. <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 -->
7. <typeAlias alias="Student" type="com.iflytek.entity.Student" />
8.
9. <!-- 这样以后改了sql,就不需要去改java代码了 -->
10. <!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->
11. <select id="selectAllStudent" resultClass="Student">
12. select * from
13. tbl_student
14. </select>
15.
16. <!-- parameterClass表示参数的内容 -->
17. <!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 -->
18. <select id="selectStudentById" parameterClass="int" resultClass="Student">
19. id=#id#
20. </select>
21.
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
26. '%$name$%'
27. </select>
28.
29. <insert id="addStudent" parameterClass="Student">
30. insert into
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. >
41. </selectKey>
42. </insert>
43.
44. <delete id="deleteStudentById" parameterClass="int">
45. <!-- #id#里的id可以随意取,但是上面的insert则会有影响,因为上面的name会从Student里的属性里去查找 -->
46. <!-- 我们也可以这样理解,如果有#占位符,则ibatis会调用parameterClass里的属性去赋值 -->
47. id=#id#
48. </delete>
49.
50. <update id="updateStudent" parameterClass="Student">
51. update tbl_student set
52. name=#name#,birth=#birth#,score=#score# where id=#id#
53. </update>
54.
55. </sqlMap>
说明:
如果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 :
1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
3. >
4.
5. <sqlMapConfig>
6. <!-- 引用JDBC属性的配置文件 -->
7. <properties resource="com/iflytek/entity/SqlMap.properties" />
8. <!-- 使用JDBC的事务管理 -->
9. <transactionManager type="JDBC">
10. <!-- 数据源 -->
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}" />
16. </dataSource>
17. </transactionManager>
18. <!-- 这里可以写多个实体的映射文件 -->
19. <sqlMap resource="com/iflytek/entity/Student.xml" />
20. </sqlMapConfig>
StudentDao :
1. package
2.
3. import
4.
5. import
6.
7. /**
8. * @author xudongwang 2011-12-31
9. *
10. * Email:xdwangiflytek@gmail.com
11. *
12. */
13. public interface
14.
15. /**
16. * 添加学生信息
17. *
18. * @param student
19. * 学生实体
20. * @return 返回是否添加成功
21. */
22. public boolean
23.
24. /**
25. * 根据学生id删除学生信息
26. *
27. * @param id
28. * 学生id
29. * @return 删除是否成功
30. */
31. public boolean deleteStudentById(int
32.
33. /**
34. * 更新学生信息
35. *
36. * @param student
37. * 学生实体
38. * @return 更新是否成功
39. */
40. public boolean
41.
42. /**
43. * 查询全部学生信息
44. *
45. * @return 返回学生列表
46. */
47. public
48.
49. /**
50. * 根据学生姓名模糊查询学生信息
51. *
52. * @param name
53. * 学生姓名
54. * @return 学生信息列表
55. */
56. public
57.
58. /**
59. * 根据学生id查询学生信息
60. *
61. * @param id
62. * 学生id
63. * @return 学生对象
64. */
65. public Student selectStudentById(int
66.
67. }
StudentDaoImpl :
1. package
2.
3. import
4. import
5. import
6. import
7.
8. import
9. import
10. import
11. import
12. import
13.
14. /**
15. * @author xudongwang 2011-12-31
16. *
17. * Email:xdwangiflytek@gmail.com
18. *
19. */
20. public class StudentDaoImpl implements
21.
22. private static SqlMapClient sqlMapClient = null;
23.
24. // 读取配置文件
25. static
26. try
27. Reader reader = Resources
28. "com/iflytek/entity/SqlMapConfig.xml");
29. sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
30. reader.close();
31. catch
32. e.printStackTrace();
33. }
34. }
35.
36. public boolean
37. null;
38. boolean flag = false;
39. try
40. "addStudent", student);
41. "添加学生信息的返回值:"
42. catch
43. e.printStackTrace();
44. }
45. if (object != null) {
46. true;
47. }
48. return
49. }
50.
51. public boolean deleteStudentById(int
52. boolean flag = false;
53. null;
54. try
55. "deleteStudentById", id);
56. "删除学生信息的返回值:" + object + ",这里返回的是影响的行数");
57. catch
58. e.printStackTrace();
59. }
60. if (object != null) {
61. true;
62.
63. }
64. return
65.
66. }
67.
68. public boolean
69. boolean flag = false;
70. false;
71. try
72. "updateStudent", student);
73. "更新学生信息的返回值:" + object + ",返回影响的行数");
74. catch
75. e.printStackTrace();
76. }
77. if (object != null) {
78. true;
79. }
80. return
81. }
82.
83. public
84. null;
85. try
86. "selectAllStudent");
87. catch
88. e.printStackTrace();
89. }
90. return
91. }
92.
93. public
94. null;
95. try
96. "selectStudentByName",name);
97. catch
98. e.printStackTrace();
99. }
100. return
101. }
102.
103. public Student selectStudentById(int
104. null;
105. try
106. student = (Student) sqlMapClient.queryForObject(
107. "selectStudentById", id);
108. catch
109. e.printStackTrace();
110. }
111. return
112. }
113. }
TestIbatis.java :
1. package
2.
3. import
4. import
5.
6. import
7. import
8.
9. /**
10. * @author xudongwang 2011-12-31
11. *
12. * Email:xdwangiflytek@gmail.com
13. *
14. */
15. public class
16.
17. public static void
18. new
19.
20. "测试插入");
21. new
22. "李四");
23. "2011-09-02"));
24. 88);
25. System.out.println(studentDaoImpl.addStudent(addStudent));
26.
27. "测试根据id查询");
28. 1));
29.
30. "测试模糊查询");
31. "李");
32. for
33. System.out.println(student);
34. }
35.
36. "测试查询所有");
37. List<Student> students = studentDaoImpl.selectAllStudent();
38. for
39. System.out.println(student);
40. }
41.
42. "根据id删除学生信息");
43. 1));
44.
45. "测试更新学生信息");
46. new
47. 1);
48. "李四1");
49. "2011-08-07"));
50. 21);
51. System.out.println(studentDaoImpl.updateStudent(updateStudent));
52.
53. }
54. }
iBatis 的优缺点:
优点:
1、 减少代码量,简单;
2、 性能增强;
3、 Sql 语句与程序代码分离;
4、 增强了移植性;
缺点:
1、 和Hibernate 相比,sql 需要自己写;
2、 参数数量只能有一个,多个参数时不太方便;
- iBatisDemo.rar (998.6 KB)
- 下载次数: 526