ibatis学习笔记(四)>>>>>>>ibatis使用实例
1.去ibatis官网下载ibatis的相关包。 http://ibatis.apache.org/.最后把ibatis.jar包导入到项目的中(假定我们这次实例的数据库是mysql。)。
2.项目相关配置文件database-config.properties(数据库配置属性文件)、SqlConfig.xml(ibatis配置文件)、 TEACHER_SqlMap.xml(java实体和数据库sql配置文件)
database-config.properties文件内容
1. java.mysql.server=127.0.0.1
2. java.mysql.port=3306
3. java.mysql.url=jdbc/:mysql/://${java.mysql.server}/:${java.mysql.port}/${java.mysql.database}
4. java.mysql.database=school
5. java.mysql.username=root
6. java.mysql.password=root
7. java.mysql.driver=com.mysql.jdbc.Driver
java.mysql.server=127.0.0.1java.mysql.port=3306java.mysql.url=jdbc/:mysql/://${java.mysql.server}/:${java.mysql.port}/${java.mysql.database}java.mysql.database=schooljava.mysql.username=rootjava.mysql.password=rootjava.mysql.driver=com.mysql.jdbc.Driver
TEACHER_SqlMap.xml文件内容
1. <?xml version="1.0" encoding="UTF-8" ?>
2. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
3. <sqlMap namespace="TEACHER">
4. <typeAlias type="com.yjpeng.ibatis.model.Teacher" alias="teacher" />
5. <resultMap class="teacher" id="teacherResult">
6. <result column="id" property="id" jdbcType="INT" />
7. <result column="name" property="name" jdbcType="VARCHAR" />
8. <result column="address" property="address" jdbcType="VARCHAR" />
9. </resultMap>
10. <select id="teacher_getTeacherList" resultMap="teacherResult">
11. select * from teacher
12. </select>
13. </sqlMap>
<?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 namespace="TEACHER"> <typeAlias type="com.yjpeng.ibatis.model.Teacher" alias="teacher" /> <resultMap class="teacher" id="teacherResult"> <result column="id" property="id" jdbcType="INT" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="address" property="address" jdbcType="VARCHAR" /> </resultMap> <select id="teacher_getTeacherList" resultMap="teacherResult"> select * from teacher </select></sqlMap>
SqlConfig.xml文件内容
1. <?xml version="1.0" encoding="UTF-8" ?>
2. <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd" >
3. <sqlMapConfig>
4. <properties resource="database-config.properties" />
5. <settings cacheModelsEnabled="true" />
6. <transactionManager type="JDBC">
7. <dataSource type="SIMPLE">
8. <!-- JDBC驱动 -->
9. <property name="JDBC.Driver" value="${java.mysql.driver}" />
10. <!-- JDBC连接URL -->
11. <property name="JDBC.ConnectionURL" value="${java.mysql.url}" />
12. <!-- JDBC访问数据库的用户名 -->
13. <property name="JDBC.Username" value="${java.mysql.username}" />
14. <!-- JDBC访问数据库的密码 -->
15. <property name="JDBC.Password" value="${java.mysql.password}" />
16. </dataSource>
17. </transactionManager>
18. <sqlMap resource="com/yjpeng/ibatis/sqlmap/TEACHER_SqlMap.xml" />
19. </sqlMapConfig>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd" ><sqlMapConfig> <properties resource="database-config.properties" /> <settings cacheModelsEnabled="true" /> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <!-- JDBC驱动 --> <property name="JDBC.Driver" value="${java.mysql.driver}" /> <!-- JDBC连接URL --> <property name="JDBC.ConnectionURL" value="${java.mysql.url}" /> <!-- JDBC访问数据库的用户名 --> <property name="JDBC.Username" value="${java.mysql.username}" /> <!-- JDBC访问数据库的密码 --> <property name="JDBC.Password" value="${java.mysql.password}" /> </dataSource> </transactionManager> <sqlMap resource="com/yjpeng/ibatis/sqlmap/TEACHER_SqlMap.xml" /></sqlMapConfig>
3.TeacherDAO.java(接口文件)、TeacherDAOImpl接口实现文件。
TeacherDAO.java文件内容
1. package com.yjpeng.ibatis.dao;
2.
3. import java.util.List;
4.
5. import com.yjpeng.ibatis.model.Teacher;
6.
7. public interface TeacherDAO {
8. List<Teacher> getTeacherList();
9. }
package com.yjpeng.ibatis.dao;import java.util.List;import com.yjpeng.ibatis.model.Teacher;public interface TeacherDAO { List<Teacher> getTeacherList();}
TeacherDAOImpl.java文件内容
1. package com.yjpeng.ibatis.dao;
2.
3.
4. import java.io.Reader;
5. import java.sql.SQLException;
6. import java.util.List;
7.
8. import com.ibatis.common.resources.Resources;
9. import com.ibatis.sqlmap.client.SqlMapClient;
10. import com.ibatis.sqlmap.client.SqlMapClientBuilder;
11. import com.yjpeng.ibatis.model.Teacher;
12.
13. public class TeacherDAOImpl implements TeacherDAO {
14. private SqlMapClient sqlmap = null;
15.
16. public TeacherDAOImpl(SqlMapClient sqlmap){
17. this.sqlmap = sqlmap;
18. }
19. public List<Teacher> getTeacherList() {
20. List<Teacher> list = null;
21. try {
22. list = sqlmap.queryForList("teacher_getTeacherList", null);
23. return list;
24. } catch (SQLException e) {
25. e.printStackTrace();
26. }
27. return null;
28. }
29.
30. public static void main(String[] args) {
31. Reader reader = null;
32. try{
33. reader = Resources.getResourceAsReader("etc/SqlConfig.xml");
34. SqlMapClient sqlmap = SqlMapClientBuilder.buildSqlMapClient(reader);
35. TeacherDAO teacherDAO = new TeacherDAOImpl(sqlmap);
36. List<Teacher> list = teacherDAO.getTeacherList();
37. System.out.println(list.size());
38. }catch (Exception e) {
39. e.printStackTrace();
40. }
41. }
42. }
package com.yjpeng.ibatis.dao;import java.io.Reader;import java.sql.SQLException;import java.util.List;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;import com.yjpeng.ibatis.model.Teacher;public class TeacherDAOImpl implements TeacherDAO { private SqlMapClient sqlmap = null; public TeacherDAOImpl(SqlMapClient sqlmap){ this.sqlmap = sqlmap; } public List<Teacher> getTeacherList() { List<Teacher> list = null; try { list = sqlmap.queryForList("teacher_getTeacherList", null); return list; } catch (SQLException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { Reader reader = null; try{ reader = Resources.getResourceAsReader("etc/SqlConfig.xml"); SqlMapClient sqlmap = SqlMapClientBuilder.buildSqlMapClient(reader); TeacherDAO teacherDAO = new TeacherDAOImpl(sqlmap); List<Teacher> list = teacherDAO.getTeacherList(); System.out.println(list.size()); }catch (Exception e) { e.printStackTrace(); } }}
好了,一个简单的ibatis实例,已经讲解完了。由于是例子所以写的比较简单请见谅。