MyBatis 的簡單應用

介紹 java

1.例子中包含了 mybatis 的經常使用sql的寫法  
2.動態sql 的應用  
3.存儲過程的使用

目錄 
 

MyBatis-config.xml 中 set 的說明 []: 表示 可能的不太正確 sql

Xml代碼 apache

<!-- 配置設置 -->  
    <settings>  
        <!-- 配置全局性 cache 的 ( 開 / 關) default:true -->  
        <setting name="cacheEnabled" value="true"/>  
          
        <!-- 是否使用 懶加載 關聯對象  同 hibernate中的延遲加載 同樣  default:true -->  
        <setting name="lazyLoadingEnabled" value="true"/>  
          
        <!-- [當對象使用延遲加載時 屬性的加載取決於能被引用到的那些延遲屬性,不然,按需加載(須要的是時候纔去加載)] -->  
        <setting name="aggressiveLazyLoading" value="true"/>  
          
        <!-- 是否容許單條sql 返回多個數據集  (取決於驅動的兼容性) default:true -->  
        <setting name="multipleResultSetsEnabled" value="true"/>  
          
        <!-- 是否能夠使用列的別名 (取決於驅動的兼容性) default:true-->  
        <setting name="useColumnLabel" value="true"/>  
          
        <!--容許JDBC 生成主鍵。須要驅動器支持。若是設爲了true,這個設置將強制使用被生成的主鍵,有一些驅動器不兼容不過仍然能夠執行。  default:false-->  
        <setting name="useGeneratedKeys" value="false"/>  
          
        <!--指定 MyBatis 如何自動映射 數據基表的列 NONE:不隱射 PARTIAL:部分  FULL:所有-->  
        <setting name="autoMappingBehavior" value="PARTIAL"/>  
          
        <!-- 這是默認的執行類型   
            SIMPLE :簡單    
            REUSE:執行器可能重複使用prepared statements 語句   
            BATCH:執行器能夠重複執行語句和批量更新  
        -->  
        <setting name="defaultExecutorType" value="SIMPLE"/>  
          
        <!-- 設置驅動等待數據響應的超時數  默認沒有設置-->  
        <setting name="defaultStatementTimeout" value="25000"/>  
          
        <!-- [是否啓用 行內嵌套語句  defaut:false] -->  
        <setting name="safeRowBoundsEnabled" value="false"/>  
          
        <!-- [是否 啓用  數據中 A_column 自動映射 到 java類中駝峯命名的屬性 default:fasle] -->  
        <setting name="mapUnderscoreToCamelCase" value="false"/>  
          
        <!-- 設置本地緩存範圍 session:就會有數據的共享  statement:語句範圍 (這樣就不會有數據的共享 ) defalut:session -->  
        <setting name="localCacheScope" value="SESSION"/>  
          
        <!-- 設置但JDBC類型爲空時,某些驅動程序 要指定值,default:other -->  
        <setting name="jdbcTypeForNull" value="OTHER"/>  
          
        <!-- 設置觸發延遲加載的方法  -->  
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>  
          
    </settings>

表,序列 ,存儲過程 的建立 

存儲過程 數組

create or replace procedure pro_getAllStudent  
(  
v_sid number,  
v_sname varchar2,  
userList_cursor out sys_refcursor  
)  
as  
begin  
  update student set sname=v_sname where sid=v_sid;  
  open userList_cursor for select* from student;  
end;

測試 緩存

SQL> declare  
  2  v_student_row student%rowtype;  
  3  v_sid student.sid%type:=11;  
  4  v_sname student.sname%type:='張浩';  
  5  v_student_rows sys_refcursor;  
  6  begin  
  7  pro_getAllStudent(v_sid,v_sname,v_student_rows);  
  8  loop  
  9  fetch v_student_rows into v_student_row;  
 10  exit when v_student_rows%notfound;  
 11  Dbms_Output.put_line('第'||v_student_rows%rowcount||'行,學生id'||v_student_row.sid||'--姓名:'||v_student_row.sname);  
 12  end loop;  
 13  close v_student_rows;  
 14  end;  
 15  /

序列 session

-- Create sequence   
create sequence STUDENT_SEQ  
minvalue 1  
maxvalue 999999999999999999999999999  
start with 32  
increment by 1  
cache 20;

學生表 mybatis

create table STUDENT  
(  
  SID    NUMBER(8) primary key not null,  
  SNAME  VARCHAR2(20) not null,  
  MAJOR  VARCHAR2(100),  
  BIRTH  DATE,  
  SCORE  NUMBER(6,2),  
  CID    NUMBER(8),  
  STATUS CHAR(3)  
)

班級表 oracle

-- Create table  
create table CLASSES  
(  
  CID        NUMBER(8) primary key  not null,  
  CNAME      VARCHAR2(20) not null,  
  TEACHER    VARCHAR2(25),  
  CREATEDATE DATE  
)

mybatis-config.xml app

Xml代碼ide

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  "http://mybatis.org/dtd/mybatis-3-config.dtd">  
<configuration>  
  
    <!-- 配置的元素順序 properties?, settings?, typeAliases?, typeHandlers?,   
    objectFactory?, objectWrapperFactory?, proxyFactory?, plugins?,   
    environments?, databaseIdProvider?, mappers -->  
       
    <!-- 使用屬性文件 並且能夠在這裏這是 覆蓋文件中的值 -->  
    <properties resource="mybatis-config.properties">  
        <!--   
        <property name="username" value="admin"/>  
        <property name="password" value="123456"/>  
         -->  
    </properties>  
     
      
    <!-- 別名的配置 -->  
    <typeAliases>  
        <typeAlias type="com.mybatis.student.Student" alias="Student"/>  
        <typeAlias type="com.mybatis.classes.Classes" alias="Classes"/>  
        <!--   
            也能夠使用 包範圍來配置  
            <package name="com.mybatis"/>  
         -->  
    </typeAliases>  
      
    <environments default="development">  
        <environment id="development">  
            <transactionManager type="JDBC"/>  
            <dataSource type="POOLED">  
                <property name="driver" value="${driver}"/>  
                <property name="url" value="${url}"/>  
                <property name="username" value="${username}"/>  
                <property name="password" value="${password}"/>  
            </dataSource>  
        </environment>  
    </environments>  
      
    <mappers>  
        <mapper resource="com/mybatis/student/StudentMapper.xml"/>  
        <mapper resource="com/mybatis/classes/ClassesMapper.xml"/>  
    </mappers>  
      
</configuration>

mybatis-config.properties 

Xml代碼

driver=oracle.jdbc.driver.OracleDriver  
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl  
username=luob  
password=luob

Student.java 

Java代碼

package com.mybatis.student;  
  
import java.io.Serializable;  
import java.util.Date;  
  
import com.mybatis.classes.Classes;  
  
@SuppressWarnings("serial")  
public class Student implements Serializable {  
private int sid;  
private String sname;  
private String major;  
private Date birth;  
private float score;  
private int cid;  
private int status;  
//get set

StudentMapper.xml 

Xml代碼

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
 <mapper namespace="com.mybatis.student">  
    <!-- <!ELEMENT mapper (  
        cache-ref | cache | resultMap* | parameterMap* | sql*   
        | insert* | update* | delete* | select* )+> -->  
      
    <!-- 設置緩存 若是用戶須要登陸 須要設置這種類型 type=org.mybatis.caches.oscache.LoggingOSCache-->  
    <cache eviction="FIFO" readOnly="true" size="256" flushInterval="60000"/>  
       
    <!-- 定義能夠重用的sql 代碼片斷  -->  
    <sql id="studentColumns">sid,sname,score</sql>  
  
    <!-- 自定義結果集 -->     
    <resultMap type="Student" id="studentResultMap">  
        <id property="sid" column="SID"/>  
        <result property="sname" column="SNAME"/>  
        <result property="score" column="SCORE"/>  
    </resultMap>  
      
    <resultMap type="Student" id="studentAllResultMap">  
        <id property="sid" column="SID"/>  
        <result property="sname" column="SNAME"/>  
        <result property="major" column="MAJOR"/>  
        <result property="birth" column="BIRTH"/>  
        <result property="score" column="SCORE"/>  
        <result property="cid" column="CID"/>  
        <result property="status" column="STATUS"/>  
    </resultMap>  
      
    <!-- 只用構造函數 建立對象 對於那些 比較少的列 -->  
    <resultMap type="Student" id="studentAndClassesResultMap">  
        <constructor>  
            <idArg column="SID" javaType="int"/>  
            <arg column="SNAME" javaType="String"/>  
            <arg column="SCORE" javaType="float"/>  
        </constructor>  
        <association property="classes" javaType="Classes" resultMap="com.mybatis.classes.classesResultMap"/>  
    </resultMap>  
   
   
    <select id="selectStudentAndClassBySname" parameterType="String" resultMap="studentAndClassesResultMap">  
        select s.sid,s.sname,s.score,c.cid,c.cname,c.teacher,c.createdate from student s left join classes c on s.cid=c.cid where s.sname=#{sname}  
    </select>  
   
    <insert id="addStudentBySequence" parameterType="Student" >  
    <selectKey keyProperty="sid" resultType="int" order="BEFORE">  
        select STUDENT_SEQ.nextVal from dual  
    </selectKey>  
        insert into student(sid,sname,major,birth,score)  
        values (#{sid},#{sname},#{major},#{birth},#{score})  
    </insert>  
      
    <insert id="addStudent" parameterType="Student">  
        insert into student(sid,sname,major,birth,score)  
        values (#{sid},#{sname},#{major},#{birth},#{score})  
    </insert>  
      
    <delete id="delStudentById" parameterType="int">  
        delete student where sid=#{sid}  
    </delete>  
      
    <select id="queryAllStudent" resultType="Student" useCache="true" flushCache="false" timeout="10000">  
        select * from student  
    </select>  
      
    <!-- 參數能夠指定一個特定的數據類型  還能夠使用自定類型處理: typeHandler=MyTypeHandler -->  
    <select id="queryStudentByName" resultType="Student" parameterType="String">  
        select * from student where sname like #{property,javaType=String,jdbcType=VARCHAR}  
    </select>  
      
    <!-- 參數能夠指定一個特定的數據類型 對於數字類型 ,numericScale=2  用於設置小數類型  -->  
    <select id="queryStudentById" resultType="Student" parameterType="int">  
        select * from student where sid=#{property,javaType=int,jdbcType=NUMERIC}  
    </select>  
      
      
    <update id="updStudentById" parameterType="Student">  
        update student   
        <trim prefix="SET" suffixOverrides=",">  
            <if test="sname !=null">sname=#{sname},</if>  
            <if test="major !=null">majoir=#{major},</if>  
            <if test="birth !=null">birth=#{birth},</if>  
            <if test="score !=null">score=#{score}</if>  
        </trim>  
        where sid=#{sid}  
    </update>  
      
    <!-- 在這裏 利用了 可重用的sql代碼片斷 -->  
    <select id="selectMapResult" resultMap="studentResultMap" parameterType="String">  
        select <include refid="studentColumns"/> from STUDENT where sname like #{sname}  
    </select>   
      
    <!-- Dynamic  Sql 使用  if -->  
    <select id="selectStudentByDynamicSql" parameterType="Student" resultType="Student">  
        select * from student   
        <where>  
            <if test="sname !=null">  
                sname like #{sname}  
            </if>  
            <if test="sid !=null">  
                AND sid=#{sid}  
            </if>  
        </where>  
    </select>  
      
    <!-- 採用 OGNL 表達式  來配置動態sql 使用trim 去掉 where 中多餘的  and 或者 or  where  choose  when otherwise-->  
    <select id="selectStudentByDynamicSqlChoose" parameterType="Student" resultType="Student">  
        select * from student   
        <trim prefix="WHERE" prefixOverrides="AND | OR ">  
            <choose>  
                <when test=" sname !=null and sname.length() >0 ">   
                    sname like #{sname}  
                </when>  
                <when test="sid !=null and sid>0">  
                    AND sid = #{sid}  
                </when>  
                <otherwise>  
                    AND status='1'  
                </otherwise>  
            </choose>  
        </trim>  
    </select>  
      
    <!-- 使用foreach 遍歷list  只能小寫-->  
    <select id="selectStudentByIds" resultType="Student">  
        select * from student   
        where sid in  
        <foreach collection="list" item="itm" index="index" open="(" separator="," close=")">  
            #{itm}  
        </foreach>  
    </select>  
      
    <!-- 使用foreach 遍歷arry 只能小寫 -->  
    <select id="selectStudentByIdArray" resultType="Student">  
        select * from student   
        where sid in  
        <foreach collection="array" item="itm" index="index" open="(" separator="," close=")">  
            #{itm}  
        </foreach>  
    </select>  
      
    <parameterMap type="map" id="procedureParam">  
        <parameter property="sid" javaType="int" jdbcType="NUMERIC" mode="IN" />  
        <parameter property="sname" javaType="String" jdbcType="VARCHAR" mode="IN" />  
        <parameter property="studentList" javaType="ResultSet" jdbcType="CURSOR" mode="OUT" resultMap="studentAllResultMap"/>   
    </parameterMap>  
    <!--傳入map集合參數 ,調用  待用遊標存儲過程(先執行 修改後而後查詢全部)  -->  
    <select id="getAllStudentAfterupdate" statementType="CALLABLE" useCache="true" parameterMap="procedureParam">  
        {call LUOB.pro_getallstudent(?,?,?)}  
    </select>  
      
     
 </mapper>

Classes.java 

Java代碼

package com.mybatis.classes;  
  
import java.sql.Date;  
import java.util.List;  
  
import com.mybatis.student.Student;  
  
public class Classes {  
    private int cid;  
    private String cname;  
    private String teacher;  
    private Date createDate;  
  
    private List<Student> students;  
    //get set

ClassesMapper.xml 

Xml代碼 

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
 <mapper namespace="com.mybatis.classes">  
      
    <!-- 設置 緩存共享 -->  
    <cache-ref namespace="com.mybatis.student"/>  
      
    <resultMap type="Classes" id="classesResultMap">  
        <id column="CID" property="cid"/>  
        <result column="CNAME" property="cname"/>  
        <result column="TEACHER" property="teacher"/>  
        <result column="CREATEDATE" property="createDate"/>  
    </resultMap>  
      
    <!-- columnPrefix:別名前綴 -->  
    <resultMap type="Classes" id="classesAndStudentListResultMap">  
        <id column="CID" property="cid"/>  
        <result column="CNAME" property="cname"/>  
        <result column="TEACHER" property="teacher"/>  
        <result column="CREATEDATE" property="createDate"/>  
        <collection property="students" ofType="Student" resultMap="com.mybatis.student.studentResultMap" columnPrefix="stu_"/>  
    </resultMap>  
      
    <!-- 下面採用了 別名 stu_ 來區分列名 -->  
    <select id="selectClassAndStudentListById" resultMap="classesAndStudentListResultMap" parameterType="int">  
        select   
            c.cid,  
            c.cname,  
            c.teacher,  
            c.createdate,  
            s.sid stu_sid,  
            s.sname stu_sname,  
            s.score stu_score  
        from student s right join classes c on s.cid=c.cid where c.cid=#{cid}  
    </select>  
      
 </mapper>

TestStudentAndClasses.java 

Java代碼

package com.mybatis.student;  
  
import java.io.IOException;  
import java.io.Reader;  
import java.sql.CallableStatement;  
import java.sql.Connection;  
import java.sql.Date;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import org.apache.ibatis.io.Resources;  
import org.apache.ibatis.session.SqlSession;  
import org.apache.ibatis.session.SqlSessionFactory;  
import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
import org.junit.Before;  
import org.junit.Test;  
  
import com.mybatis.classes.Classes;  
  
  
public class TestStudentAndClasses {  
  
    private SqlSessionFactory sqlSessionFactory;  
    @Before  
    public void init() throws IOException{  
        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");   
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);   
    }  
      
    /** 
     * 測試新增  手動給 sid 
     */  
    @Test  
    public void testAddStudent(){  
        SqlSession session=sqlSessionFactory.openSession();  
        Student student =new Student();  
        student.setSid(35);  
        student.setSname("Guider");  
        student.setScore(100);  
        student.setMajor("Games");  
        student.setBirth(Date.valueOf("2008-08-08"));  
        session.insert("com.mybatis.student.addStudent", student);  
        session.commit();  
        session.close();  
    }  
    /** 
     * 測試新增 採用序列 給sid 
     */  
    @Test  
    public void testAddStudentBySequence(){  
        SqlSession session=sqlSessionFactory.openSession();  
        Student student =new Student();  
        student.setSname("Provdwer");  
        student.setScore(100);  
        student.setMajor("Games");  
        student.setBirth(Date.valueOf("2008-08-08"));  
        session.insert("com.mybatis.student.addStudentBySequence", student);  
        session.commit();  
        session.close();  
    }  
      
    /** 
     * 測試刪除 
     */  
    @Test  
    public void testDelStudentById(){  
        SqlSession session=sqlSessionFactory.openSession();  
        session.delete("com.mybatis.student.delStudentById", 12);  
        session.commit();  
        session.close();  
    }  
      
    /** 
     * 測試根據 sid更新 
     */  
    @Test  
    public void testUpdStudentById(){  
        SqlSession session=sqlSessionFactory.openSession();  
        Student student =new Student();  
        student.setSid(0);  
        student.setSname("Sandy");  
        student.setScore(100);  
        student.setMajor("sandy");  
        student.setBirth(Date.valueOf("2008-08-08"));  
        session.update("com.mybatis.student.addStudentBySequence", student);  
        session.commit();  
        session.close();  
          
    }  
    /** 
     * 測試查詢全部 
     */  
    @Test  
    public void testQueryAllStudent(){  
        List<Student> stuList=new ArrayList<Student>();  
        SqlSession session=sqlSessionFactory.openSession();   
        stuList=session.selectList("com.mybatis.student.queryAllStudent");  
        session.commit();  
        session.close();  
        for (Student student : stuList) {  
            System.out.println(student);  
        }  
    }  
      
    /** 
     * 測試根據 name 模糊查詢 
     */  
    @Test  
    public void testQueryStudentByName(){  
        List<Student> stuList=new ArrayList<Student>();  
        SqlSession session=sqlSessionFactory.openSession();  
        stuList=session.selectList("com.mybatis.student.queryStudentByName","%l%");  
        session.commit();  
        session.close();  
        for (Student student : stuList) {  
            System.out.println(student);  
        }  
    }  
      
    /** 
     * 測個根據sid查找一個對象 
     */  
    @Test  
    public void testQueryStudentById(){  
        SqlSession session=sqlSessionFactory.openSession();  
        Student student=(Student)session.selectOne("com.mybatis.student.queryStudentById",1);  
        session.close();  
        System.out.println(student);  
    }  
      
    /** 
     * 測試 使用resultMap 自定返回值集合 
     */  
    @Test  
    public void testStudentResultMap(){  
        List<Student> stuList=new ArrayList<Student>();  
        SqlSession session=sqlSessionFactory.openSession();  
        stuList=session.selectList("com.mybatis.student.selectMapResult","%l%");  
        session.close();  
        for (Student student : stuList) {  
            System.out.println(student);  
        }  
    }  
      
    /** 
     * 測試 左鏈接查  一對一 的 關係 
     */  
    @Test  
    public void testSelectStudentAndClassBySname(){  
        List<Student> stuList=new ArrayList<Student>();  
        SqlSession session=sqlSessionFactory.openSession();  
        stuList=session.selectList("com.mybatis.student.selectStudentAndClassBySname","luob");  
        session.close();  
        for (Student student : stuList) {  
            System.out.println(student+"//--"+student.getClasses());  
        }  
    }  
      
    /** 
     * 測試 多對一的 關係的 右鏈接的查詢 
     */  
    @Test  
    public void testSelectClassAndStudentListById(){  
        SqlSession session=sqlSessionFactory.openSession();  
        Classes classes=(Classes)session.selectOne("com.mybatis.classes.selectClassAndStudentListById",1);  
        session.close();  
        System.out.println(classes);  
        for (Student student : classes.getStudents()) {  
            System.out.println(student+"//--"+student.getClasses());  
        }  
    }  
      
    /** 
     * 測試 動態sql 的 應用 where if  ognl 
     */  
    @Test  
    public void testSelectStudentByDynamicSql(){  
        Student pstudent=new Student();  
        pstudent.setSid(1);  
        List<Student> stuList=new ArrayList<Student>();  
        SqlSession session=sqlSessionFactory.openSession();  
        stuList=session.selectList("com.mybatis.student.selectStudentByDynamicSql",pstudent);  
        session.close();  
        for (Student student : stuList) {  
            System.out.println(student+"//--"+student.getClasses());  
        }  
    }  
      
    /** 
     * 測試 動態sql 的choose  where when otherwise 
     */  
    @Test  
    public void testSelectStudentByDynamicSqlChoose(){  
        Student pstudent=new Student();  
        pstudent.setSid(1);  
        //pstudent.setSname("luob");  
        List<Student> stuList=new ArrayList<Student>();  
        SqlSession session=sqlSessionFactory.openSession();  
        stuList=session.selectList("com.mybatis.student.selectStudentByDynamicSqlChoose",pstudent);  
        session.close();  
        for (Student student : stuList) {  
            System.out.println(student+"//--"+student.getClasses());  
        }  
    }  
      
    /** 
     * 測試 動態sql 中foreach 的使用 傳入 集合list 參數  
     */  
    @Test  
    public void testSelectStudentByIds(){  
        ArrayList<Integer> ids=new ArrayList<Integer>();  
        ids.add(1);  
        ids.add(6);  
        ids.add(21);  
        ids.add(23);  
        List<Student> stuList=new ArrayList<Student>();  
  
        SqlSession session=sqlSessionFactory.openSession();  
        stuList=session.selectList("com.mybatis.student.selectStudentByIds",ids);  
        session.close();  
        for (Student student : stuList) {  
            System.out.println(student+"//--"+student.getClasses());  
        }  
          
    }  
    /** 
     * 測試 動態sql 中foreach 的使用 傳入 數組array 參數  
     */  
    @Test  
    public void testSelectStudentByIdArray(){  
          
        List<Student> stuList=new ArrayList<Student>();  
        Integer[] idArry=new Integer[]{1,6,21,23};  
        SqlSession session=sqlSessionFactory.openSession();  
        stuList=session.selectList("com.mybatis.student.selectStudentByIdArray",idArry);  
        session.close();  
        for (Student student : stuList) {  
            System.out.println(student+"//--"+student.getClasses());  
        }  
          
    }  
      
    /** 
     * 測試調用 存儲過程   裏面有遊標哦   返回多個結果  
     */  
    @Test  
    public void testGetAllStudentAfterupdate(){  
        List<Student> stuList=new ArrayList<Student>();  
        Map map = new HashMap();  
        map.put("sid", 10);  
        map.put("sname", "張翰");  
        map.put("studentList",stuList);  
        SqlSession session=sqlSessionFactory.openSession();  
          
        session.selectOne("com.mybatis.student.getAllStudentAfterupdate",map);  
        stuList=(ArrayList<Student>)map.get("studentList");  
        for (Student student : stuList) {  
            System.out.println(student+"//--"+student.getClasses());  
        }  
        session.close();  
    }  
      
    /** 
     * 使用jdbc 測試 遊標的建立是否成功  
     */  
    @Test  
    public void testJdbcProcedure(){  
          
        Connection con=null;  
          
        try {  
            Class.forName("oracle.jdbc.driver.OracleDriver");  
            con=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","luob","luob");  
            CallableStatement cs=con.prepareCall("{call LUOB.pro_getallstudent(?,?,?)}");  
            cs.setInt(1, 10);  
            cs.setString(2,"張翰");  
            //!!! 注意這裏 type 在Types中 沒有這個類型  
            cs.registerOutParameter(3,oracle.jdbc.OracleTypes.CURSOR);  
            cs.execute();  
            ResultSet rs=(ResultSet)cs.getObject(3);  
            while(rs.next()){  
                Student student=new Student();  
                student.setSid(rs.getInt(1));  
                student.setSname(rs.getString(2));  
                student.setMajor(rs.getString(3));  
                student.setBirth(rs.getDate(4));  
                student.setScore(rs.getFloat(5));  
                student.setCid(rs.getInt(6));  
                student.setStatus(rs.getInt(7));  
                System.out.println(student);  
            }  
              
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}
相關文章
相關標籤/搜索