最近项目中用到IBATIS ,我没有用过,现在开始慢慢学习。IBATIS是一个持久层框架,但是比HIBERNATE要轻许多,用ibatis基本上sql语句要自己写。今天下午做了一个简单的demo。
流程:在DAO中读取SqlMapConfig.xml文件(连接数据库,寻找实体类配置文件路径)。当DAO中调用sql语句时候:sqlMapClient.queryForList("getAllStudent"); 会去实体类的配置文件里面寻找sql语句。
com.lubby.bean.SqlMapConfig.xml
<?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/lubby/bean/SqlMap.properties" /> --> <!-- 使用JDBC的事务管理 --> <transactionManager type="JDBC"> <!-- 数据源 --> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="org.postgresql.Driver" /> <property name="JDBC.ConnectionURL" value="jdbc:postgresql://127.0.0.1:5432/lubby" /> <property name="JDBC.Username" value="admin" /> <property name="JDBC.Password" value="admin" /> </dataSource> </transactionManager> <!-- 这里可以写多个实体的映射文件 --> <sqlMap resource="com/lubby/bean/Student.xml" /> </sqlMapConfig>
com.lubby.bean.Student.java
public class Student { private String sid; private String name; private int age; public Student() { super(); } public Student(String sid, String name, int age) { super(); this.sid = sid; this.name = name; this.age = age; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [sid=" + sid + ", name=" + name + ", age=" + age + "]"; } }
com.lubby.bean.Student.xml
<?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.lubby.bean.Student" /> <!-- 这样以后改了sql,就不需要去改java代码了 --> <!-- id表示select里的sql语句,resultClass表示返回结果的类型 --> <select id="getAllStudent" resultClass="Student"> select * from student </select> <!-- parameterClass表示参数的内容 --> <!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 --> <select id="getStudentById" parameterClass="String" resultClass="Student"> select * from student where sid=#id# </select> </sqlMap>
com.lubby.dao.StudentDAO.java
public interface StudentDAO { /** * 获取所有学生 * @return 所有学生信息列表 */ public List<Student> getAllStudent(); /** * 根据学生ID查询信息 * @return 所查学生信息 */ public Student getStudentById(String id); }com.lubby.daoimpl.StudentDAOImpl.java
package com.lubby.daoimpl; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.lubby.bean.Student; import com.lubby.dao.StudentDAO; public class StudentDAOImpl implements StudentDAO { private static SqlMapClient sqlMapClient = null; // 读取配置文件 static { try { Reader reader = Resources.getResourceAsReader("com/lubby/bean/SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public List<Student> getAllStudent() { List<Student> list = null; try { list = sqlMapClient.queryForList("getAllStudent"); } catch (SQLException e) { e.printStackTrace(); } return list; } @Override public Student getStudentById(String id) { Student student = null; try { student = (Student) sqlMapClient.queryForObject("getStudentById", id); } catch (SQLException e) { e.printStackTrace(); } return student; } }
com.lubby.test.IbatisTest.java
public class IbatisTest { public static void main(String[] args) { StudentDAO stuDaoImpl = new StudentDAOImpl(); List<Student> list = new ArrayList<>(); list = stuDaoImpl.getAllStudent(); for (Student stu : list) { System.out.println(stu); } Student student = stuDaoImpl.getStudentById("003"); System.out.println("ID为004的学生信息:" + student); } }
Student [sid=001, name=Tom, age=22] Student [sid=002, name=Jack, age=23] Student [sid=003, name=Tom, age=20] Student [sid=004, name=Alice, age=18] ID为004的学生信息:Student [sid=003, name=Tom, age=20]
原文地址:http://blog.csdn.net/liu00614/article/details/28650369