﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Charming Java-文章分类-Ibatis</title><link>http://www.blogjava.net/vivianLiu/category/39289.html</link><description>Everyone will enjoy it!</description><language>zh-cn</language><lastBuildDate>Wed, 29 Apr 2009 23:04:22 GMT</lastBuildDate><pubDate>Wed, 29 Apr 2009 23:04:22 GMT</pubDate><ttl>60</ttl><item><title>Ibatis多对多</title><link>http://www.blogjava.net/vivianLiu/articles/268212.html</link><dc:creator>vivian</dc:creator><author>vivian</author><pubDate>Wed, 29 Apr 2009 11:28:00 GMT</pubDate><guid>http://www.blogjava.net/vivianLiu/articles/268212.html</guid><wfw:comment>http://www.blogjava.net/vivianLiu/comments/268212.html</wfw:comment><comments>http://www.blogjava.net/vivianLiu/articles/268212.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vivianLiu/comments/commentRss/268212.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vivianLiu/services/trackbacks/268212.html</trackback:ping><description><![CDATA[现在到了多对多的了！<br />
其实和一对多差不多的！<br />
我这个例子是以学生和老师为例子的，一个学生可以有很多老师，一个老师可以有很多学生！<br />
<br />
我的MySql脚本为：<br />
<br />
<p>DROP TABLE IF EXISTS `student`;<br />
create table student(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id&nbsp; VARCHAR(100) PRIMARY KEY,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_name VARCHAR(100)<br />
);<br />
DROP TABLE IF EXISTS `teacher`;<br />
create table teacher(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teacher_id VARCHAR(100) PRIMARY KEY,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teacher_name VARCHAR(100)<br />
);<br />
DROP TABLE IF EXISTS `teacher_student`;<br />
create table teacher_student(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ts_id VARCHAR(100) PRIMARY KEY,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teacher_id VARCHAR(100),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id VARCHAR(100),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FOREIGN KEY teacher_id references teacher(teacher_id),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FOREIGN KEY student_id references student(student_id)<br />
);</p>
<p>alter table teacher_student&nbsp; add constraint&nbsp; ts1&nbsp;&nbsp; unique&nbsp; nonclutered (teacher_id,student_id);&nbsp; </p>
<p>insert into student values ('001','liufang');<br />
insert into student values ('002','jianghaiying');<br />
insert into student values ('003','duanhuixia');</p>
<p>insert into teacher values ('001','gaowei');<br />
insert into teacher values ('002','lihua');<br />
insert into teacher values ('003','xugang');</p>
<p>insert into teacher_student values('1','001','001');<br />
insert into teacher_student values('2','002','001');<br />
insert into teacher_student values('3','003','001');<br />
insert into teacher_student values('4','001','002');<br />
insert into teacher_student values('4','002','002');<br />
insert into teacher_student values('5','003','003');</p>
<p>我的Ibatis配置文件为：<br />
<br />
</p>
<p>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</p>
<p>&lt;!DOCTYPE sqlMap&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; "http://ibatis.apache.org/dtd/sql-map-2.dtd"&gt;</p>
<p>&lt;sqlMap namespace="Student_Teacher"&gt;</p>
<p>&nbsp; &lt;!-- Use type aliases to avoid typing the full classname every time. --&gt;<br />
&nbsp; &lt;typeAlias alias="Student_Teacher.Student" type="com.foundersoftware.wishjob.demo.ibatis.manytomany.model.Student" /&gt;<br />
&nbsp; &lt;typeAlias alias="Student_Teacher.Teacher" type="com.foundersoftware.wishjob.demo.ibatis.manytomany.model.Teacher"/&gt;<br />
&nbsp; &lt;!-- Result maps describe the mapping between the columns returned<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from a query, and the class properties.&nbsp; A result map isn't<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; necessary if the columns (or aliases) match to the properties <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exactly. --&gt;<br />
&nbsp; &lt;resultMap id="studentResult_basic" class="Student_Teacher.Student"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="studentId" column="student_id" jdbcType="varchar"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="studentName" column="student_name" jdbcType="varchar"/&gt;<br />
&nbsp; &lt;/resultMap&gt;<br />
&nbsp; <br />
&nbsp; &lt;resultMap id="studentResult" class="Student_Teacher.Student" extends="studentResult_basic" &gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="teacherList" column="student_id" select="Student_Teacher.selectBystudentId" /&gt;<br />
&nbsp; &lt;/resultMap&gt;<br />
&nbsp; <br />
&nbsp; &lt;resultMap id="teacherResult_basic" class="Student_Teacher.Teacher"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="teacherId" column="teacher_id" jdbcType="varchar"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="teacherName" column="teacher_name" jdbcType="varchar"/&gt;<br />
&nbsp;&nbsp; &lt;/resultMap&gt;<br />
&nbsp;&nbsp; <br />
&nbsp;&nbsp; &lt;resultMap id="teacherResult" class="Student_Teacher.Teacher" extends="teacherResult_basic"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="studentList" column="teacher_id" select="Student_Teacher.selectByteacherId" /&gt;<br />
&nbsp; &lt;/resultMap&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- select all the properies of a student including the teacherList by<br />
&nbsp;&nbsp;&nbsp;&nbsp; using Id--&gt;<br />
&nbsp; &lt;select id="SelectStudentById" parameterClass="String" resultMap="studentResult"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select * from student where student_id = #studentId#<br />
&nbsp; &lt;/select&gt; </p>
<p>&nbsp; &lt;!-- select all the properies of students including the teacherList --&gt;<br />
&nbsp; &lt;select id="SelectAllStudents" resultMap="studentResult"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select * from student <br />
&nbsp; &lt;/select&gt; <br />
&nbsp; <br />
&nbsp; &lt;select id="selectByteacherId" parameterClass="String" resultMap="studentResult"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select s.* from student s where s.student_id in<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (select student_id from teacher_student where teacher_id=#value#)<br />
&nbsp; &lt;/select&gt; <br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp; &lt;select id="SelectTeacherById" parameterClass="String" resultMap="teacherResult"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select * from teacher where teacher_id = #teacherId#<br />
&nbsp; &lt;/select&gt; <br />
&nbsp; <br />
&nbsp; <br />
&nbsp; &lt;select id="selectBystudentId" parameterClass="String" resultMap="teacherResult"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select t.* from Teacher t where t.teacher_id in<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (select teacher_id from teacher_student where student_id=#value#)<br />
&nbsp; &lt;/select&gt; <br />
&nbsp; <br />
&nbsp; &lt;!-- Insert example, using the Student parameter class --&gt;<br />
&nbsp; &lt;insert id="insertStudent" parameterClass="Student_Teacher.Student" &gt;<br />
&nbsp;&nbsp;&nbsp; insert into student (<br />
&nbsp;&nbsp;&nbsp; student_id,<br />
&nbsp;&nbsp;&nbsp; student_name)<br />
&nbsp;&nbsp;&nbsp; values(#studentId#,#studentName#)<br />
&nbsp; &lt;/insert&gt;&nbsp; <br />
&nbsp; <br />
&nbsp; &lt;!-- Update example, using the Student parameter class --&gt;<br />
&nbsp; &lt;update id="updateStudent" parameterClass="Student_Teacher.Student"&gt;<br />
&nbsp;&nbsp;&nbsp; update student set<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_name = #studentName#<br />
&nbsp;&nbsp;&nbsp; where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id = #studentId#<br />
&nbsp; &lt;/update&gt;<br />
&nbsp; <br />
&nbsp;&nbsp; &lt;!-- Delete example, using an integer as the parameter class --&gt;<br />
&nbsp; &lt;delete id="deleteStudentById" parameterClass="string"&gt;<br />
&nbsp;&nbsp;&nbsp; delete from student where student_id = #student_id#<br />
&nbsp; &lt;/delete&gt;<br />
&lt;/sqlMap&gt;<br />
</p>
<p><br />
我有两个Model类，分别为：</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.manytomany.model;</p>
<p>import java.util.ArrayList;<br />
import java.util.List;</p>
<p>public class Student {<br />
&nbsp;<br />
&nbsp;private String studentId;<br />
&nbsp;<br />
&nbsp;private String studentName;<br />
&nbsp;<br />
&nbsp;private List&lt;Teacher&gt; teacherList = new ArrayList&lt;Teacher&gt;(); <br />
&nbsp;<br />
&nbsp;public String getStudentId() {<br />
&nbsp;&nbsp;return studentId;<br />
&nbsp;}</p>
<p>&nbsp;public void setStudentId(String studentId) {<br />
&nbsp;&nbsp;this.studentId = studentId;<br />
&nbsp;}</p>
<p>&nbsp;public String getStudentName() {<br />
&nbsp;&nbsp;return studentName;<br />
&nbsp;}</p>
<p>&nbsp;public void setStudentName(String studentName) {<br />
&nbsp;&nbsp;this.studentName = studentName;<br />
&nbsp;}</p>
<p>&nbsp; public String toString() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "Student{" +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "studentId=" + studentId +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ", studentName='" + studentName + '\'' +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ", TeacherList='" + teacherList.size() + '\'' +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '}';<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; public String out() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuffer sb = new StringBuffer();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.append("Student{" +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "studentId=" + studentId +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ", studentName='" + studentName + '\'' +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ", TeacherList='" + teacherList.size() + '\'');<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (Teacher teacher : teacherList) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.append("\n\t").append(teacher.toString());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sb.toString(); <br />
}<br />
}<br />
</p>
<p><br />
和</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.manytomany.model;</p>
<p>import java.util.ArrayList;<br />
import java.util.List;</p>
<p>public class Teacher {<br />
&nbsp;<br />
&nbsp;private String teacherId;<br />
&nbsp;<br />
&nbsp;private String teacherName;<br />
&nbsp;<br />
&nbsp;private List&lt;Student&gt; studentList= new ArrayList&lt;Student&gt;(); <br />
&nbsp;<br />
&nbsp;public String getTeacherId() {<br />
&nbsp;&nbsp;return teacherId;<br />
&nbsp;}</p>
<p>&nbsp;public void setTeacherId(String teacherId) {<br />
&nbsp;&nbsp;this.teacherId = teacherId;<br />
&nbsp;}</p>
<p>&nbsp;public String getTeacherName() {<br />
&nbsp;&nbsp;return teacherName;<br />
&nbsp;}</p>
<p>&nbsp;public void setTeacherName(String teacherName) {<br />
&nbsp;&nbsp;this.teacherName = teacherName;<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp; public String toString() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "Teacher{" +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "teacherId=" + teacherId +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ", teacherName='" + teacherName + '\'' +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ", studentList=" + studentList.size() +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '}';<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; public String out(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuffer sb= new StringBuffer();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(studentList.size()&gt;0){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.append("Teacher{" +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "teacherId=" + teacherId +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ", teacherName='" + teacherName + '\'' +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ", studentList=" + studentList.size());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(Student s: studentList){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.append("\n\t").append(s.toString());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.append("\n}");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sb.toString();<br />
&nbsp;&nbsp;&nbsp;&nbsp; } <br />
&nbsp;<br />
}<br />
</p>
<p><br />
我的DAO层为：</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.manytomany.dao;</p>
<p>import java.sql.SQLException;<br />
import java.util.ArrayList;<br />
import java.util.List;</p>
<p>import com.foundersoftware.wishjob.demo.ibatis.manytomany.model.*;<br />
import com.foundersoftware.wishjob.util.SqlMapFactory;<br />
import com.ibatis.sqlmap.client.SqlMapClient;</p>
<p>public class Student_TeacherDAOIbatisImpl&nbsp; {<br />
</p>
<p>//我的SqlMapFactory工具类已经在Ibatis单表的文章中给出了~！<br />
&nbsp;private static SqlMapClient sqlMapper = SqlMapFactory.getInstance(); </p>
<p>&nbsp;public void add(Student student) {<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;sqlMapper.insert("Student_Teacher.insertStudent", student);<br />
&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;<br />
&nbsp;}</p>
<p>&nbsp;public void update(Student student) {<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;sqlMapper.update("Student_Teacher.updateStudent", student);<br />
&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}&nbsp;<br />
&nbsp;}</p>
<p><br />
&nbsp;@SuppressWarnings("unchecked")<br />
&nbsp;public List&lt;Student&gt; query() {<br />
&nbsp;&nbsp;List &lt;Student&gt;list = new ArrayList&lt;Student&gt;();<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;list = sqlMapper.queryForList("Student_Teacher.SelectAllStudents");<br />
&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;return list;<br />
&nbsp;}</p>
<p><br />
&nbsp;public void delete(String student_id) {<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;sqlMapper.delete("Student_Teacher.deleteStudentById", student_id);<br />
&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;}</p>
<p>&nbsp;public Student getStudentById(String student_id) {<br />
&nbsp;&nbsp;Student student = new Student();<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;student = (Student) sqlMapper.queryForObject("Student_Teacher.SelectStudentById", student_id);<br />
&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;return student;<br />
&nbsp;}<br />
}<br />
</p>
<p><br />
&nbsp;我的Test为：</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.manytomany.test;<br />
import java.util.ArrayList;<br />
import java.util.Iterator;<br />
import java.util.List;</p>
<p>import com.foundersoftware.wishjob.demo.ibatis.manytomany.dao.Student_TeacherDAOIbatisImpl;<br />
import com.foundersoftware.wishjob.demo.ibatis.manytomany.model.*;<br />
public class ManytomanyTest {<br />
&nbsp;<br />
&nbsp;public static void main(String[] args){<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;Student_TeacherDAOIbatisImpl test = new Student_TeacherDAOIbatisImpl();<br />
&nbsp;&nbsp;List&lt;Student&gt; student = new ArrayList&lt;Student&gt;();<br />
&nbsp;&nbsp;try{<br />
&nbsp;&nbsp;&nbsp;//select all students from the table "student"!<br />
&nbsp;&nbsp;&nbsp;student = test.query();<br />
&nbsp;&nbsp;&nbsp;Iterator&lt;Student&gt; it=student.iterator();<br />
&nbsp;&nbsp;&nbsp;System.out.println("select all students from table student:");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(it.hasNext()){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Student s1=(Student)it.next();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println(s1.out());;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //select all the properties of a student including the teacherList by<br />
&nbsp;&nbsp;&nbsp;//using the StudentId("001")<br />
&nbsp;&nbsp;&nbsp;Student studentTest = test.getStudentById("001");<br />
&nbsp;&nbsp;&nbsp;System.out.println(studentTest.out());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //insert student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s2= new Student();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s2.setStudentId("004");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s2.setStudentName("liqie");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.add(s2);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("insert sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //update student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s3=test.getStudentById("001");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s3.setStudentName("duanhuixia");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.update(s3);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("update sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //delete student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s4=test.getStudentById("003");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.delete(s4.getStudentId());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("delete sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;}catch(Exception e){<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;}</p>
<p>}<br />
<br />
我也是刚刚开始学习Ibatis，大家互相进步啊！<br />
</p>
<img src ="http://www.blogjava.net/vivianLiu/aggbug/268212.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vivianLiu/" target="_blank">vivian</a> 2009-04-29 19:28 <a href="http://www.blogjava.net/vivianLiu/articles/268212.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ibatis单表</title><link>http://www.blogjava.net/vivianLiu/articles/268209.html</link><dc:creator>vivian</dc:creator><author>vivian</author><pubDate>Wed, 29 Apr 2009 11:22:00 GMT</pubDate><guid>http://www.blogjava.net/vivianLiu/articles/268209.html</guid><wfw:comment>http://www.blogjava.net/vivianLiu/comments/268209.html</wfw:comment><comments>http://www.blogjava.net/vivianLiu/articles/268209.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vivianLiu/comments/commentRss/268209.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vivianLiu/services/trackbacks/268209.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 下面为我以Student表为例子写出的单表映射，这个实现了增删改查，我觉得比较简洁明了，最主要的是可以直接用啊！<br />
<br />
MySql脚本为：<br />
<br />
<p>DROP TABLE IF EXISTS `student`;<br />
create table student(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id&nbsp; VARCHAR(100) PRIMARY KEY,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_name VARCHAR(100)<br />
);</p>
<p>insert into student values ('001','liufang');<br />
insert into student values ('002','jianghaiying');<br />
insert into student values ('003','duanhuixia');</p>
<p>&nbsp;Ibatis配置文件为：<br />
<br />
</p>
<p>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</p>
<p>&lt;!DOCTYPE sqlMap&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; "http://ibatis.apache.org/dtd/sql-map-2.dtd"&gt;</p>
<p>&lt;sqlMap namespace="Student"&gt;</p>
<p>&nbsp; &lt;!-- Use type aliases to avoid typing the full classname every time. --&gt;<br />
&nbsp; &lt;typeAlias alias="Student.Student" type="com.foundersoftware.wishjob.demo.ibatis.crud.model.Student"/&gt;</p>
<p>&nbsp; &lt;!-- Result maps describe the mapping between the columns returned<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from a query, and the class properties.&nbsp; A result map isn't<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; necessary if the columns (or aliases) match to the properties <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exactly. --&gt;<br />
&nbsp; &lt;resultMap id="studentResult" class="Student.Student"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="studentId" column="student_id" jdbcType="varchar"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="studentName" column="student_name" jdbcType="varchar"/&gt;<br />
&nbsp; &lt;/resultMap&gt;</p>
<p>&nbsp; &lt;!-- the cacheModel of ibatis!<br />
&nbsp; if you don't excute "insertStudent" "updateStudent"<br />
&nbsp; and "deleteStudentById",the result will be saved in cache for 24 hours--&gt;<br />
&nbsp; &lt;cacheModel id="student-cache" type="LRU"&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;flushInterval hours="24"/&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;flushOnExecute statement="Student.insertStudent"/&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;flushOnExecute statement="Student.updateStudent"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;flushOnExecute statement="Student.deleteStudentById"/&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="size" value="1000"/&gt;<br />
&nbsp; &lt;/cacheModel&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- Select with no parameters using the result map for Student class. --&gt;<br />
&nbsp; &lt;select id="selectAllStudentes" resultMap="studentResult" cacheModel="student-cache" &gt;<br />
&nbsp;&nbsp;&nbsp; select * from student<br />
&nbsp; &lt;/select&gt;</p>
<p>&nbsp;&lt;!-- A simpler select example without the result class.&nbsp;&nbsp;&nbsp; --&gt;<br />
&nbsp; &lt;select id="selectStudentById" parameterClass="string"&nbsp; resultMap="studentResult" cacheModel="student-cache"&gt;<br />
&nbsp;&nbsp;&nbsp; select<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_name<br />
&nbsp;&nbsp;&nbsp; from student<br />
&nbsp;&nbsp;&nbsp; where student_id = #student_id#<br />
&nbsp; &lt;/select&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- Insert example, using the Student parameter class --&gt;<br />
&nbsp;&nbsp; &lt;insert id="insertStudent" parameterClass="Student.Student" &gt;<br />
&nbsp;&nbsp;&nbsp; insert into student (<br />
&nbsp;&nbsp;&nbsp; student_id,<br />
&nbsp;&nbsp;&nbsp; student_name)<br />
&nbsp;&nbsp;&nbsp; values(#studentId#,#studentName#)<br />
&nbsp; &lt;/insert&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- Update example, using the Student parameter class --&gt;<br />
&nbsp; &lt;update id="updateStudent" parameterClass="Student.Student"&gt;<br />
&nbsp;&nbsp;&nbsp; update student set<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_name = #studentName#<br />
&nbsp;&nbsp;&nbsp; where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id = #studentId#<br />
&nbsp; &lt;/update&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- Delete example, using an integer as the parameter class --&gt;<br />
&nbsp; &lt;delete id="deleteStudentById" parameterClass="string"&gt;<br />
&nbsp;&nbsp;&nbsp; delete from student where student_id = #student_id#<br />
&nbsp; &lt;/delete&gt;<br />
&lt;/sqlMap&gt;</p>
<br />
我的Model类为：<br />
<br />
<p>package com.foundersoftware.wishjob.demo.ibatis.crud.model;</p>
<p>public class Student {</p>
<p>private String studentId;<br />
&nbsp;<br />
&nbsp;private String studentName;<br />
&nbsp;<br />
&nbsp;public String getStudentId() {<br />
&nbsp;&nbsp;return studentId;<br />
&nbsp;}</p>
<p>&nbsp;public void setStudentId(String studentId) {<br />
&nbsp;&nbsp;this.studentId = studentId;<br />
&nbsp;}</p>
<p>&nbsp;public String getStudentName() {<br />
&nbsp;&nbsp;return studentName;<br />
&nbsp;}</p>
<p>&nbsp;public void setStudentName(String studentName) {<br />
&nbsp;&nbsp;this.studentName = studentName;<br />
&nbsp;}</p>
<p>}</p>
<p>我的DAO类为：</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.crud.dao;</p>
<p>import java.sql.SQLException;<br />
import java.util.ArrayList;<br />
import java.util.List;</p>
<p>import com.foundersoftware.wishjob.demo.ibatis.crud.model.Student;<br />
import com.foundersoftware.wishjob.util.SqlMapFactory;<br />
import com.ibatis.sqlmap.client.SqlMapClient;</p>
<p>public class StudentIbatisDAO {<br />
&nbsp;<br />
&nbsp;/**<br />
&nbsp;&nbsp;&nbsp; * SqlMapClient instances are thread safe, so you only need one.<br />
&nbsp;&nbsp;&nbsp; * In this case, we'll use a static singleton.&nbsp; So sue me.&nbsp; ;-)<br />
&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp;&nbsp; private static SqlMapClient sqlMapper = SqlMapFactory.getInstance(); </p>
<p>&nbsp;&nbsp;public void add(Student student) {<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;sqlMapper.insert("Student.insertStudent", student);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public void update(Student student) {<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;sqlMapper.update("Student.updateStudent", student);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}&nbsp;<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;@SuppressWarnings("unchecked")<br />
&nbsp;&nbsp;public List&lt;Student&gt; query() {<br />
&nbsp;&nbsp;&nbsp;List &lt;Student&gt;list = new ArrayList&lt;Student&gt;();<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;list = sqlMapper.queryForList("Student.selectAllStudentes");<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;return list;<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public void delete(String student_id) {<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;sqlMapper.delete("Student.deleteStudentById", student_id);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public Student getStudentById(String student_id) {<br />
&nbsp;&nbsp;&nbsp;Student student = new Student();<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;student = (Student) sqlMapper.queryForObject("Student.selectStudentById", student_id);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;return student;<br />
&nbsp;&nbsp;}</p>
<p>}<br />
</p>
<p><br />
从上面可以看到我用了一个SqlMapFactory的工具类，在这里和大家一起分享！</p>
<p>package com.foundersoftware.wishjob.util;</p>
<p>import java.io.IOException;<br />
import java.io.Reader;<br />
import com.ibatis.common.resources.Resources;<br />
import com.ibatis.sqlmap.client.SqlMapClient;<br />
import com.ibatis.sqlmap.client.SqlMapClientBuilder;</p>
<p>public class SqlMapFactory {<br />
&nbsp;<br />
&nbsp;private static final SqlMapClient sqlMap;<br />
&nbsp;static {<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;String resource = "com/foundersoftware/wishjob/config/sqlmap-config.xml";<br />
&nbsp;&nbsp;&nbsp;Reader reader = Resources.getResourceAsReader (resource);<br />
&nbsp;&nbsp;&nbsp;sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);<br />
&nbsp;&nbsp;} catch (Exception e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;throw new RuntimeException ("Error initializing MyAppSqlConfig class. Cause: " + e);<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
&nbsp;&nbsp;<br />
&nbsp;public static SqlMapClient getSqlMapInstance () {<br />
&nbsp;&nbsp;return sqlMap;<br />
&nbsp;&nbsp;}<br />
&nbsp;public static SqlMapClient getInstance () {<br />
&nbsp;&nbsp;return sqlMap;<br />
&nbsp;&nbsp;}<br />
&nbsp;public static SqlMapClient getInstance1() {<br />
&nbsp;&nbsp;String resource = "com/foundersoftware/wishjob/config/sqlmap-config.xml";<br />
&nbsp;&nbsp;Reader reader;<br />
&nbsp;&nbsp;SqlMapClient sqlMap = null;<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;reader = Resources.getResourceAsReader(resource);<br />
&nbsp;&nbsp;&nbsp;sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);<br />
&nbsp;&nbsp;} catch (IOException e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;return sqlMap;<br />
&nbsp;}<br />
}<br />
</p>
<p><br />
我的Test类为：</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.crud.test;<br />
import java.util.ArrayList;<br />
import java.util.Iterator;<br />
import java.util.List;</p>
<p>import com.foundersoftware.wishjob.demo.ibatis.crud.dao.StudentIbatisDAO;<br />
import com.foundersoftware.wishjob.demo.ibatis.crud.model.Student;<br />
public class CrudTest {<br />
&nbsp;<br />
&nbsp;public static void main(String[] args){<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;StudentIbatisDAO test = new StudentIbatisDAO();<br />
&nbsp;&nbsp;List&lt;Student&gt; student = new ArrayList&lt;Student&gt;();<br />
&nbsp;&nbsp;try{<br />
&nbsp;&nbsp;&nbsp;//select all students from the table "student"!<br />
&nbsp;&nbsp;&nbsp;student = test.query();<br />
&nbsp;&nbsp;&nbsp;Iterator&lt;Student&gt; it=student.iterator();<br />
&nbsp;&nbsp;&nbsp;System.out.println("select all students from table student:");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(it.hasNext()){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Student s1=(Student)it.next();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println(s1.getStudentId()+"-&gt;"+s1.getStudentName());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //select studentName from table "student" by studentId<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("select studentName from table student by studentId(001):");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s2=test.getStudentById("001");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(s2.getStudentName());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //insert student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s3= new Student();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s3.setStudentId("004");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s3.setStudentName("jianghaiying");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.add(s3);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("insert sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //update student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s4=test.getStudentById("002");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s4.setStudentName("duanhuixia");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.update(s4);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("update sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //delete student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s5=test.getStudentById("003");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.delete(s5.getStudentId());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("delete sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;}catch(Exception e){<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;}</p>
<p>}<br />
</p>
<p><br />
大家如果有什么疑问可以一起讨论啊！我也是新手啊！</p>
<img src ="http://www.blogjava.net/vivianLiu/aggbug/268209.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vivianLiu/" target="_blank">vivian</a> 2009-04-29 19:22 <a href="http://www.blogjava.net/vivianLiu/articles/268209.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ibatis一对多</title><link>http://www.blogjava.net/vivianLiu/articles/268205.html</link><dc:creator>vivian</dc:creator><author>vivian</author><pubDate>Wed, 29 Apr 2009 11:15:00 GMT</pubDate><guid>http://www.blogjava.net/vivianLiu/articles/268205.html</guid><wfw:comment>http://www.blogjava.net/vivianLiu/comments/268205.html</wfw:comment><comments>http://www.blogjava.net/vivianLiu/articles/268205.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vivianLiu/comments/commentRss/268205.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vivianLiu/services/trackbacks/268205.html</trackback:ping><description><![CDATA[在MySql数据库中的脚本是：<br />
<br />
<p>drop table if exists 'student`;<br />
create table student(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id&nbsp; VARCHAR(100) PRIMARY KEY,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_name VARCHAR(100),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; specialty_id VARCHAR(100),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FOREIGN KEY specialty_id references specialty(specialty_id)<br />
);<br />
drop table if exists&nbsp; 'specialty';<br />
create table specialty(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; specialty_id VARCHAR(100) PRIMARY KEY,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; specialty_name VARCHAR(100)<br />
);</p>
<p>insert into student values ('001','liufang','001');<br />
insert into student values ('002','jianghaiying','001');<br />
insert into student values ('003','duanhuixia','001');</p>
<p>insert into specialty values ('001','English');<br />
insert into specialty values ('002','French');<br />
insert into specialty values ('003','Japanese');<br />
<br />
Ibatis中的配置文件为：<br />
OnetoMany.xml：</p>
<p>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</p>
<p>&lt;!DOCTYPE sqlMap&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; "http://ibatis.apache.org/dtd/sql-map-2.dtd"&gt;</p>
<p>&lt;sqlMap namespace="Student_Specialty"&gt;</p>
<p>&nbsp; &lt;!-- Use type aliases to avoid typing the full classname every time. --&gt;<br />
&nbsp; &lt;typeAlias alias="Student_Specialty.Student" type="com.foundersoftware.wishjob.demo.ibatis.onetomany.model.Student"/&gt;<br />
&nbsp; &lt;typeAlias alias="Specialty" type="com.foundersoftware.wishjob.demo.ibatis.onetomany.model.Specialty"/&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- Result maps describe the mapping between the columns returned<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from a query, and the class properties.&nbsp; A result map isn't<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; necessary if the columns (or aliases) match to the properties <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exactly. --&gt;<br />
&nbsp; &lt;resultMap id="studentResult" class="Student_Specialty.Student"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="studentId" column="student_id" jdbcType="varchar"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="studentName" column="student_name" jdbcType="varchar"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="specialty" column="specialty_id" select="Student_Specialty.selectSpecialtyById"/&gt;<br />
&nbsp; &lt;/resultMap&gt;<br />
&nbsp; <br />
&nbsp; &lt;resultMap id="specialtyResult" class="Specialty"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="specialtyId" column="specialty_id" jdbcType="varchar"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result property="specialtyName" column="specialty_name" jdbcType="varchar"/&gt;<br />
&nbsp; &lt;/resultMap&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- the cacheModel of ibatis!if you don't excute "insertStudent" "updateStudent"<br />
&nbsp; and "deleteStudentById",the result will be saved in cache for 24 hours--&gt;<br />
&nbsp; &lt;cacheModel id="student-specialty-cache" type="LRU"&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;flushInterval hours="24"/&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;flushOnExecute statement="Student_Specialty.insertStudent"/&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;flushOnExecute statement="Student_Specialty.updateStudent"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;flushOnExecute statement="Student_Specialty.deleteStudentById"/&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="size" value="1000"/&gt;<br />
&nbsp; &lt;/cacheModel&gt;<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp; &lt;!-- Select with no parameters using the result map for Student class. --&gt;<br />
&nbsp; &lt;select id="selectAllStudents" resultMap="studentResult" cacheModel="student-specialty-cache" &gt;<br />
&nbsp;&nbsp;&nbsp; select * from student<br />
&nbsp; &lt;/select&gt;</p>
<p>&nbsp;&lt;!-- A simpler select example without the result class.&nbsp;&nbsp;&nbsp; --&gt;<br />
&nbsp; &lt;select id="selectSpecialtyById" parameterClass="string" resultMap="specialtyResult" cacheModel="student-specialty-cache"&gt;<br />
&nbsp;&nbsp;&nbsp; select<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; specialty_id,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; specialty_name<br />
&nbsp;&nbsp;&nbsp; from specialty<br />
&nbsp;&nbsp;&nbsp; where specialty_id = #specialty_id#<br />
&nbsp; &lt;/select&gt;<br />
&nbsp; <br />
&nbsp;&lt;!-- A simpler select example without the result class.&nbsp;&nbsp;&nbsp; --&gt;<br />
&nbsp; &lt;select id="selectStudentById" parameterClass="string" resultMap="studentResult" cacheModel="student-specialty-cache"&gt;<br />
&nbsp;&nbsp;&nbsp; select<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_name,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; specialty_id<br />
&nbsp;&nbsp;&nbsp; from student<br />
&nbsp;&nbsp;&nbsp; where student_id = #student_id#<br />
&nbsp; &lt;/select&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- Insert example, using the Student parameter class --&gt;<br />
&nbsp; &lt;insert id="insertStudent" parameterClass="Student_Specialty.Student" &gt;<br />
&nbsp;&nbsp;&nbsp; insert into student (<br />
&nbsp;&nbsp;&nbsp; student_id,<br />
&nbsp;&nbsp;&nbsp; student_name,<br />
&nbsp;&nbsp;&nbsp; specialty_id)<br />
&nbsp;&nbsp;&nbsp; values(#studentId#,#studentName#,#specialty.specialtyId#)<br />
&nbsp; &lt;/insert&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- Update example, using the Student parameter class --&gt;<br />
&nbsp; &lt;update id="updateStudent" parameterClass="Student_Specialty.Student"&gt;<br />
&nbsp;&nbsp;&nbsp; update student set<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_name = #studentName#,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; specialty_id = #specialty.specialtyId#<br />
&nbsp;&nbsp;&nbsp; where<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; student_id = #studentId#<br />
&nbsp; &lt;/update&gt;<br />
&nbsp; <br />
&nbsp; &lt;!-- Delete example, using an integer as the parameter class --&gt;<br />
&nbsp; &lt;delete id="deleteStudentById" parameterClass="string"&gt;<br />
&nbsp;&nbsp;&nbsp; delete from student where student_id = #student_id#<br />
&nbsp; &lt;/delete&gt;<br />
&lt;/sqlMap&gt;<br />
<br />
用到的两个Model类：<br />
</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.onetomany.model;</p>
<p>public class Student {</p>
<p>&nbsp;&nbsp;&nbsp; private String studentId;<br />
&nbsp;<br />
&nbsp;private String studentName;<br />
&nbsp;<br />
&nbsp;private Specialty specialty;<br />
&nbsp;<br />
&nbsp;public String getStudentId() {<br />
&nbsp;&nbsp;return studentId;<br />
&nbsp;}</p>
<p>&nbsp;public void setStudentId(String studentId) {<br />
&nbsp;&nbsp;this.studentId = studentId;<br />
&nbsp;}</p>
<p>&nbsp;public String getStudentName() {<br />
&nbsp;&nbsp;return studentName;<br />
&nbsp;}</p>
<p>&nbsp;public void setStudentName(String studentName) {<br />
&nbsp;&nbsp;this.studentName = studentName;<br />
&nbsp;}</p>
<p>&nbsp;public Specialty getSpecialty() {<br />
&nbsp;&nbsp;return specialty;<br />
&nbsp;}</p>
<p>&nbsp;public void setSpecialty(Specialty specialty) {<br />
&nbsp;&nbsp;this.specialty = specialty;<br />
&nbsp;}<br />
}<br />
</p>
<br />
和<br />
<br />
<p>package com.foundersoftware.wishjob.demo.ibatis.onetomany.model;</p>
<p>public class Specialty {</p>
<p>&nbsp;private String specialtyId;<br />
&nbsp;<br />
&nbsp;private String specialtyName;</p>
<p>&nbsp;public String getSpecialtyId() {<br />
&nbsp;&nbsp;return specialtyId;<br />
&nbsp;}</p>
<p>&nbsp;public void setSpecialtyId(String specialtyId) {<br />
&nbsp;&nbsp;this.specialtyId = specialtyId;<br />
&nbsp;}</p>
<p>&nbsp;public String getSpecialtyName() {<br />
&nbsp;&nbsp;return specialtyName;<br />
&nbsp;}</p>
<p>&nbsp;public void setSpecialtyName(String specialtyName) {<br />
&nbsp;&nbsp;this.specialtyName = specialtyName;<br />
&nbsp;}<br />
}<br />
<br />
一个DAO类为：</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.onetomany.dao;</p>
<p>import java.sql.SQLException;<br />
import java.util.ArrayList;<br />
import java.util.List;</p>
<p>import com.foundersoftware.wishjob.demo.ibatis.onetomany.model.Student;<br />
import com.foundersoftware.wishjob.demo.ibatis.onetomany.model.Specialty;<br />
import com.foundersoftware.wishjob.util.SqlMapFactory;<br />
import com.ibatis.sqlmap.client.SqlMapClient;</p>
<p>public class Student_SpecialtyIbatisDAO {<br />
&nbsp;<br />
&nbsp;/**<br />
&nbsp;&nbsp;&nbsp; * SqlMapClient instances are thread safe, so you only need one.<br />
&nbsp;&nbsp;&nbsp; * In this case, we'll use a static singleton.&nbsp; So sue me.&nbsp; ;-)<br />
&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;//这个用到了一个工具类SqlMapFactory，这个在我的Ibatis单表操作中以写出了！<br />
&nbsp;&nbsp;&nbsp;&nbsp; private static SqlMapClient sqlMapper = SqlMapFactory.getInstance(); </p>
<p>&nbsp;&nbsp;public void add(Student student) {<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;sqlMapper.insert("Student_Specialty.insertStudent", student);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public void update(Student student) {<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;sqlMapper.update("Student_Specialty.updateStudent", student);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}&nbsp;<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;@SuppressWarnings("unchecked")<br />
&nbsp;&nbsp;public List&lt;Student&gt; query() {<br />
&nbsp;&nbsp;&nbsp;List &lt;Student&gt;list = new ArrayList&lt;Student&gt;();<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;list = sqlMapper.queryForList("Student_Specialty.selectAllStudents");<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;return list;<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public void delete(String student_id) {<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;sqlMapper.delete("Student_Specialty.deleteStudentById", student_id);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public Student getStudentById(String student_id) {<br />
&nbsp;&nbsp;&nbsp;Student student = new Student();<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;student = (Student) sqlMapper.queryForObject("Student_Specialty.selectStudentById", student_id);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;return student;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;public Specialty getSpecialtyById(String specialty_id) {<br />
&nbsp;&nbsp;&nbsp;Specialty specialty = new Specialty();<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;specialty = (Specialty) sqlMapper.queryForObject("Student_Specialty.selectSpecialtyById", specialty_id);<br />
&nbsp;&nbsp;&nbsp;} catch (SQLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;return specialty;<br />
&nbsp;&nbsp;}<br />
}<br />
<br />
一个测试类：<br />
<br />
</p>
<p>package com.foundersoftware.wishjob.demo.ibatis.onetomany.test;<br />
import java.util.ArrayList;<br />
import java.util.Iterator;<br />
import java.util.List;</p>
<p>import com.foundersoftware.wishjob.demo.ibatis.onetomany.dao.Student_SpecialtyIbatisDAO;<br />
import com.foundersoftware.wishjob.demo.ibatis.onetomany.model.Specialty;<br />
import com.foundersoftware.wishjob.demo.ibatis.onetomany.model.Student;</p>
<p>public class OnetomanyTest {<br />
&nbsp;<br />
&nbsp;public static void main(String[] args){<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;Student_SpecialtyIbatisDAO test = new Student_SpecialtyIbatisDAO();<br />
&nbsp;&nbsp;List&lt;Student&gt; student = new ArrayList&lt;Student&gt;();<br />
&nbsp;&nbsp;try{<br />
&nbsp;&nbsp;&nbsp;//select all students from the table "student"!<br />
&nbsp;&nbsp;&nbsp;student = test.query();<br />
&nbsp;&nbsp;&nbsp;Iterator&lt;Student&gt; it=student.iterator();<br />
&nbsp;&nbsp;&nbsp;System.out.println("select all students from table student:");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(it.hasNext()){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Student s1=(Student)it.next();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println(s1.getStudentId()+"-&gt;"+s1.getStudentName()+"-&gt;"+s1.getSpecialty().getSpecialtyName());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //select studentName from table "student" by studentId<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("select studentName from table student by studentId(001):");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s2=test.getStudentById("001");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(s2.getStudentName()+"-&gt;"+s2.getSpecialty().getSpecialtyName());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //insert student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s3= new Student();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s3.setStudentId("004");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s3.setStudentName("jianghaiying");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Specialty specialty = test.getSpecialtyById("002");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s3.setSpecialty(specialty);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.add(s3);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("insert sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //update student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s4=test.getStudentById("003");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s4.setStudentName("duanhuixia");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.update(s4);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("update sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //delete student<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student s5=test.getStudentById("001");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.delete(s5.getStudentId());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("delete sucess");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;}catch(Exception e){<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;}</p>
<p>}<br />
</p>
<p>可以直接使用啊！</p>
<img src ="http://www.blogjava.net/vivianLiu/aggbug/268205.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vivianLiu/" target="_blank">vivian</a> 2009-04-29 19:15 <a href="http://www.blogjava.net/vivianLiu/articles/268205.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>