2011年6月iBatis 更名为 MyBatis,从 iBatis 到 MyBatis,不只是名称上的变化,MyBatis 提供了更为强大的功能,同时并没有损失其易用性,相反,在很多地方都借助于 JDK 的泛型和注解特性进行了简化。所以你懂的,开始学习就从MyBatis吧.
例子使用MyBatis+Oracle实现一个用户表的查询,如下:
②需导入的包 只有两个: mybatis-3.4.4.jar 和 ojdbc14.jar(JDBC包)
③目录结构 采用最简单的结构,com.mybatis包下有且仅有四个文件 configuration.xml user.xml User.java Test.java
④mybatis配置文件configuration.xml,
[xml]
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 <typeAliases> 5 <typeAlias alias="User" type="com.mybatis.User"></typeAlias> 6 </typeAliases> 7 8 <environments default="development"> 9 <environment id="development"> 10 <transactionManager type="JDBC"></transactionManager> 11 <dataSource type="POOLED"> 12 <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> 13 <property name="url" value="jdbc:oracle:thin:@192.168.0.87:1526:entegor" /> 14 <property name="username" value="ideal" /> 15 <property name="password" value="ideal" /> 16 </dataSource> 17 </environment> 18 </environments> 19 20 <mappers> 21 <mapper resource="user.xml"/> 22 </mappers> 23 24 </configuration>
User表sql文件User.xml,
[xml]
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 4 <mapper namespace="User"> 5 <select id="selectUser" parameterType="int" resultType="User"> 6 SELECT * FROM user WHERE id = #{id} 7 </select> 8 9 <select id="selectUsers" resultType="User"> 10 SELECT * FROM user 11 </select> 12 13 </mapper>
表结构文件User.java
1 package com.mybatis; 2 3 public class User { 4 private int iid; 5 private String ifullname; 6 private String iloginname; 7 private String ipassword; 8 public User(){} 9 public User(int id, String name) { 10 this.iid = id; 11 this.ifullname = name; 12 } 13 public String getIfullname() { 14 return ifullname; 15 } 16 public int getIid() { 17 return iid; 18 } 19 public void setIid(int iid) { 20 this.iid = iid; 21 } 22 public void setIfullname(String ifullname) { 23 this.ifullname = ifullname; 24 } 25 public String getIloginname() { 26 return iloginname; 27 } 28 public void setIloginname(String iloginname) { 29 this.iloginname = iloginname; 30 } 31 public String getIpassword() { 32 return ipassword; 33 } 34 public void setIpassword(String ipassword) { 35 this.ipassword = ipassword; 36 } 37 38 }
⑦测试用例Test.java
1 package com.controller; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 import java.util.List; 6 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 12 import com.mybatis.User; 13 14 public class Test { 15 16 public static void main(String[] args) throws IOException { 17 // TODO Auto-generated method stub 18 String resource = "configuration.xml"; 19 Reader reader = Resources.getResourceAsReader(resource); 20 SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); 21 SqlSession session = ssf.openSession(); 22 try{ 23 User user = session.selectOne("selectUser", 2); 24 System.out.println(user.getIfullname()); 25 System.out.println(user); 26 System.out.println("--------------分隔线---------------"); 27 28 List<User> users = session.selectList("selectUsers"); 29 for(int i=0; i<users.size(); i++) { 30 System.out.println(users.get(i).getIfullname()); 31 } 32 33 } catch (Exception e) { 34 e.printStackTrace(); 35 } finally { 36 session.close(); 37 } 38 } 39 40 }
原文转自:http://blog.csdn.net/u011841085/article/details/18986261
从原文略做修改