前段时间看了oracle内容,干脆把ibatis结合oracle一块搞了。
准备ibatis环境
oracle 10g express ibatis beta 5
新建工程 在WEBINF/lib下 导入以下包:
../oraclexe/app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar
ibatis-3-core-3.0.0.208.jar
总配置文件SqlMapConfig.xml
-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="puf"/>
<property name="password" value="pufang890505"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/pf/ibatis/domain/Student.xml"/>
</mappers>
</configuration>
关于每个实体的映射文件(map)Student.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="cn.pf.ibatis.domain.StudentMapper"> <select id="selectStudent" parameterType="int" resultType="cn.pf.ibatis.domain.Student"> select * from Student where id = #{id} </select> </mapper>
创建Student POJO
package cn.pf.ibatis.domain; import java.util.Date; /** * 学生PO * @author pf * @version 2010-3-16下午03:00:00 */ public class Student { /** * 学生编号 */ private int id; /** * 学生姓名 */ private String name; /** * 学生专业 */ private String major; /** * 学生生日 */ private Date birth; /** * 学生分数 */ private double score; /** * ... 构造函数 */ public Student() { super(); } /** * ... 构造函数 * @param id * @param name * @param major * @param birth * @param score */ public Student(int id, String name, String major, Date birth, double score) { super(); this.id = id; this.name = name; this.major = major; this.birth = birth; this.score = score; } /** * id getter 方法 * @return the id */ public int getId() { return id; } /** * id setter 方法 * @param id the id to set */ public void setId(int id) { this.id = id; } /** * name getter 方法 * @return the name */ public String getName() { return name; } /** * name setter 方法 * @param name the name to set */ public void setName(String name) { this.name = name; } /** * major getter 方法 * @return the major */ public String getMajor() { return major; } /** * major setter 方法 * @param major the major to set */ public void setMajor(String major) { this.major = major; } /** * birth getter 方法 * @return the birth */ public Date getBirth() { return birth; } /** * birth setter 方法 * @param birth the birth to set */ public void setBirth(Date birth) { this.birth = birth; } /** * score getter 方法 * @return the score */ public double getScore() { return score; } /** * score setter 方法 * @param score the score to set */ public void setScore(double score) { this.score = score; } /** * 转换对象为字符串 * @return 对象转换后的字符串 * @see java.lang.Object#toString() */ @Override public String toString() { return "Student [birth=" + birth + ", id=" + id + ", major=" + major + ", name=" + name + ", score=" + score + "]"; } }
test.java
String resource = "SqlMapConfig.xml"; Reader reader = null; try { //使用ibatis提供的Resources类读取资源文件 reader = Resources.getResourceAsReader(resource); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //根据资源文件内容建立session工厂 SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader); //session工厂打开一个session SqlSession session = sqlMapper.openSession(true); Student student; try{ student = (Student)session.selectOne("cn.pf.ibatis.domain.StudentMapper.selectStudent", 1); }finally{ session.close(); } System.out.println(student.toString());
ibatis 3 果然改动很大,调试花了不少时间,很多细小的错误耽误了很久。那个ibatis 3 user guide缺少完整的实例真是不爽,自己一点点试探过来,继续研究各种配置,下一步完成一个基本的crud操作。