ibatis存取clob字段解决方案

本解决方案适用于以webwork+ibatis+spring基础架构的系统中

    首先定义要持久化的类

 

public class WeeklyWork extends WeeklyWorkModel {

	/**
	 * 上周工作情况
	 */
	public String workLastWeek;

	/**
	 * 下周工作计划
	 */
	public String workNextWeek;

	/**
	 * 需要的支撑
	 */
	public String needSupport;
	
	public String getNeedSupport() {
		return needSupport;
	}
	public void setNeedSupport(String needSupport) {
		this.needSupport = needSupport;
	}
	public String getWorkLastWeek() {
		return workLastWeek;
	}
	public void setWorkLastWeek(String workLastWeek) {
		this.workLastWeek = workLastWeek;
	}
	public String getWorkNextWeek() {
		return workNextWeek;
	}
	public void setWorkNextWeek(String workNextWeek) {
		this.workNextWeek = workNextWeek;
	}
}

 其中workLastWeek,workNextWeek和needSupport在数据库中与之对应的字段B1,B2,B3均为clob类型,

 

然后再ibatis的映射文件中定义要持久化类的返回类型的映射

 

 

<typeAlias alias="WeeklyWork" type="com.broadtext.extend.jdfgs.weeklywork.domain.WeeklyWork"/>
	
	<resultMap class="WeeklyWork" id="WeeklyWorkResult">
	    <result column="B1" property="workLastWeek" jdbcType="CLOB"/>
	    <result column="B2" property="workNextWeek" jdbcType="CLOB"/>
	    <result column="B3" property="needSupport" jdbcType="CLOB"/>
	</resultMap>

 接下来想只要写好存取的SQL就大功告成了

 

<insert id="insertWeeklyWork" parameterClass="com.broadtext.extend.jdfgs.weeklywork.domain.WeeklyWork">
	 	insert into $tableName$(
	 		B1,

			B2,

			B3
	 	)values(	 		
	 		
	 		<isNotNull property="workLastWeek">
				#workLastWeek,javaType=java.lang.String,jdbcType=CLOB#,
			</isNotNull>
			<isNull property="workLastWeek">null,</isNull>
	 		
	 		<isNotNull property="workNextWeek">
				#workNextWeek,javaType=java.lang.String,jdbcType=CLOB#,
			</isNotNull>
			<isNull property="workNextWeek">null,</isNull>

	 		<isNotNull property="needSupport">
				#needSupport,javaType=java.lang.String,jdbcType=CLOB#
			</isNotNull>
			<isNull property="needSupport">null</isNull>
	 	)
	 </insert>

	 <select id="findWeeklyWorkById" parameterClass="java.util.Map" resultMap="WeeklyWorkResult">
	 	select 
			B1  as  B1,
			B2  as  B2,
			B3  as  B3,			
		from $tableName$		
	 </select>

 后台的java代码跟正常的ibatis存取对象一样这里不再复述

 

你可能感兴趣的:(java,spring,sql,ibatis,Webwork)