您的位置:首页 > 编程语言 > Java开发

Spring2.5+Struts2+Ibatis整合之三

2009-05-17 15:11 357 查看
转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广
该篇主要编写beanbeanIbaits配置文件。
在src下创建包com.zyg.ssi.bean,在该包下创建bean类Student,其代码如下:

package com.zyg.ssi.bean;

public class Student {
	private Integer stuId;
	private String stuName;
	
	public Student() {}
	
	public Student(String stuName) {
		this.stuName = stuName;
	}
	
	public Integer getStuId() {
		return stuId;
	}
	public void setStuId(Integer stuId) {
		this.stuId = stuId;
	}
	
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	@Override
	public String toString() {
		String result = "学号:"+this.stuId+",姓名:"+this.stuName;
		return result;
	}
}


在包com.zyg.ssi.bean下创建bean的Ibaits配置文件Student.xml,其代码如下:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" 
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap>
	<typeAlias alias="Student" type="com.zyg.ssi.bean.Student"/>
	
	<resultMap id="Student" class="Student">
    <result property="stuId" column="stuid"/>
    <result property="stuName" column="stuname"/>

  </resultMap>
  
	<select id="selectAllStudent" resultClass="Student">
		select * from student
	</select>
	
	<select id="selectStudentById" parameterClass="int" resultClass="Student">
		select * from student where stuid=#sid#
	</select>
	
	<delete id="delStudentById" parameterClass="int">
		delete from student where stuid=#id#
	</delete>
	
	<insert id="insertStudent" parameterClass="Student">
	    insert into Student (stuid,stuname)
	    	values (#stuId#,#stuName#)
    </insert>
    
    <!-- oracle 序列使用 -->
    <!-- 
    <insert id="insertStudentBySequence" parameterClass="Student">
    	<selectkey resultClass="int" KeyProperty="sid">
    		select PK_StudentSequence.nextVal from dual
    	</selectkey>
	    insert into Student (stuid,stuname)
	    	values (#sid#,#sname#)
    </insert>
     -->
     
    <update id="updateStudent" parameterClass="Student">
	    update Student set stuName=#stuName# where stuId=#stuId#
    </update>
    
    <select id="selectStudentByName" parameterClass="String" resultClass="Student">
		select stuId,stuName from student where stuName like '%$stuName$%'
	</select>
</sqlMap>


以上Student的配置文件很容易理解,对以下两种情况下的使用稍加说明:

代码:

<delete id="delStudentById" parameterClass="int">
	delete from student where stuid=#id#
</delete>


#id#只是占位符代表传入的参数,所以两个#之间的名称并无限制。但是对于代码:

<insert id="insertStudent" parameterClass="Student">
	    insert into Student (stuid,stuname)
	    	values (#stuId#,#stuName#)
</insert>


中的#stuId##stuName#并不是占位符,两个#之间的名称对应parameterClass指定的类的getter方法,所以在此处其名称必须与Student类中getter方法的名称对应。

至此,该篇完成了beanStudent以及其Ibaits配置文件Student.xml的编写。下一篇开始编写DAO层代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: