使用SqlMapClient后sql的写法(待完善)

使用SqlMapClient后写mapper的方法不同于普通mybatis的写法。在工作中遇到的一些问题先一一列出,虽然目前不知是为什么一定要这样写!

在spring中使用SqlMapClientTemplate操作数据库方法:http://blog.csdn.net/wangxy799/article/details/50553274

如WAYBILL.mapper文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
 "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="WAYBILL"> 
<span style="white-space:pre">	</span><!-- 内容 -->
	
</sqlMap>
**注意此处的命名空间,不再是mybatis那样写的是DAO接口


定义别名(这在mybatis映射文件中是不能写的):

<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
 "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="WAYBILL"> 
	
	<!-- 给实体类定义别名 -->
	<typeAlias alias="waybillModel" type="org.express.model.WaybillModel" />
	<typeAlias alias="Detail" type="org.express.model.Detail" />
	<typeAlias alias="ExpressInfo" type="org.express.model.ExpressInfo" />
	
</sqlMap></span>


插入数据:

<insert id="save" parameterClass="waybillModel">
	INSERT INTO WAYBILL
	<dynamic prepend="(" close=")">
		<isNotNull property="waybillNo" prepend=",">waybillNo</isNotNull>
		<isNotNull property="transferNo" prepend=",">transferNo</isNotNull>
		<isNotNull property="country" prepend=",">country</isNotNull>
		<isNotNull property="channelCode" prepend=",">channelCode</isNotNull>
		<isNotNull property="lastState" prepend=",">lastState</isNotNull>
		<isNotNull property="checkInDate" prepend=",">checkInDate</isNotNull>
		<isNotNull property="checkOutDate" prepend=",">checkOutDate</isNotNull>
		<isNotNull property="remark" prepend=",">remark</isNotNull>
	</dynamic> 
	<dynamic prepend="values (" close=")">
		<isNotNull property="waybillNo" prepend=",">#waybillNo#</isNotNull>
		<isNotNull property="transferNo" prepend=",">#transferNo#</isNotNull>
		<isNotNull property="country" prepend=",">#country#</isNotNull>
		<isNotNull property="channelCode" prepend=",">#channelCode#</isNotNull>
		<isNotNull property="lastState" prepend=",">#lastState#</isNotNull>
		<isNotNull property="checkInDate" prepend=",">#checkInDate#</isNotNull>
		<isNotNull property="checkOutDate" prepend=",">#checkOutDate#</isNotNull>
		<isNotNull property="remark" prepend=",">#remark#</isNotNull>
		</dynamic>
</insert> 
**在这里写<if test=" ***!=null and ***!=''">貌似不行。


更新数据:

<update id="saveDetail1" parameterClass="Detail">
	UPDATE WAYBILL 
		<dynamic prepend="SET" close=" ">
			<isNotNull property="compTyp" prepend=",">compTyp=#compTyp#</isNotNull>
			<isNotNull property="state" prepend=",">state=#state#</isNotNull>
		</dynamic>
	WHERE transferNo=#transferNo#;
</update>
**之前将set直接写出来的,是不对的,如下:

<!-- 错误的写法 -->
<update id="saveDetail1" parameterClass="Detail">
	UPDATE WAYBILL SET
		<isNotNull property="compTyp" prepend=",">compTyp=#compTyp#</isNotNull>
		<isNotNull property="state" prepend=",">state=#state#</isNotNull>
	WHERE transferNo=#transferNo#;
</update>












你可能感兴趣的:(java,sql,SqlMapClient)