Student.xml : <?xmlversion="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> <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 --> <typeAliasalias="Student"type="com.iflytek.entity.Student"/> <!-- 这样以后改了sql,就不需要去改java代码了 --> <!-- id表示select里的sql语句,resultClass表示返回结果的类型 --> <selectid="selectAllStudent"resultClass="Student"> select * from tbl_student </select> <!-- parameterClass表示参数的内容 --> <!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 --> <selectid="selectStudentById"parameterClass="int"resultClass="Student"> select * from tbl_student where id=#id# </select> <!-- 注意这里的resultClass类型,使用Student类型取决于queryForList还是queryForObject --> <selectid="selectStudentByName"parameterClass="String" resultClass="Student"> select name,birth,score from tbl_student where name like ‘%$name$%‘ </select> <insertid="addStudent"parameterClass="Student"> insert into tbl_student(name,birth,score) values (#name#,#birth#,#score#) <selectKeyresultClass="int"keyProperty="id"> select @@identity as inserted <!-- 这里需要说明一下不同的数据库主键的生成,对各自的数据库有不同的方式: --> <!-- mysql:SELECT LAST_INSERT_ID() AS VALUE --> <!-- mssql:select @@IDENTITY as value --> <!-- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL --> <!-- 还有一点需要注意的是不同的数据库生产商生成主键的方式不一样,有些是预先生成 (pre-generate)主键的,如Oracle和PostgreSQL。 有些是事后生成(post-generate)主键的,如MySQL和SQL Server 所以如果是Oracle数据库,则需要将selectKey写在insert之前 --> </selectKey> </insert> <deleteid="deleteStudentById"parameterClass="int"> <!-- #id#里的id可以随意取,但是上面的insert则会有影响,因为上面的name会从Student里的属性里去查找 --> <!-- 我们也可以这样理解,如果有#占位符,则ibatis会调用parameterClass里的属性去赋值 --> delete from tbl_student where id=#id# </delete> <updateid="updateStudent"parameterClass="Student"> update tbl_student set name=#name#,birth=#birth#,score=#score# where id=#id# </update> </sqlMap>
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > <sqlMap> <typeAlias alias="userLotteryInfoDO" type="com.taobao.chest.task.freeorder.dataobject.UserLotteryInfoDO"/> <resultMap id="userLotteryResultMap" class="userLotteryInfoDO"> <result property="id" column="id"/> <result property="gmtCreate" column="gmt_create"/> <result property="gmtModified" column="gmt_modified"/> <result property="userId" column="user_id"/> <result property="activityId" column="activity_id"/> <result property="umpActivityId" column="ump_activity_id"/> <result property="win" column="win"/> <result property="message" column="message"/> <result property="lotteryTime" column="lottery_time"/> <result property="orderId" column="order_id"/> </resultMap> <sql id="user.lottery.info.selector"> <![CDATA[ select id,activity_id,win,ump_activity_id,user_id,order_id,gmt_create,gmt_modified,lottery_time,message from user_lottery_info ]]> </sql> <sql id="user.lottery.info.query_segment"> <dynamic prepend="where"> <isNotNull prepend=" and " property="id"><![CDATA[id=#id#]]></isNotNull> <isNotNull prepend=" and " property="userId"><![CDATA[user_id=#userId#]]></isNotNull> <isNotNull prepend=" and " property="orderId"><![CDATA[order_id=#orderId#]]></isNotNull> <isNotNull prepend=" and " property="activityId"><![CDATA[activity_id=#activityId#]]></isNotNull> <isNotNull prepend=" and " property="umpActivityId"><![CDATA[ump_activity_id=#umpActivityId#]]></isNotNull> </dynamic> </sql> <!-- insert into 基本语句 --> <insert id="user.lottery.info.insert" parameterClass="userLotteryInfoDO"> insert into user_lottery_info(gmt_create,gmt_modified,user_id,activity_id,ump_activity_id,win,message,lottery_time,order_id) values(now(),now(),#userId#,#activityId#,#umpActivityId#,#win#,#message#,now(),#orderId#) <selectKey keyProperty="id" resultClass="long"> select LAST_INSERT_ID() </selectKey> </insert> <!--更新--> <update id="user.lottery.info.update" parameterClass="userLotteryInfoDO"> update user_lottery_info set win = #win#,message = #message#,gmt_modified=now() where order_id=#orderId# </update> <select id="user.lottery.info.select.by.id" parameterClass="long" resultMap="userLotteryResultMap"> select id,activity_id,win,ump_activity_id,user_id,order_id,gmt_create,gmt_modified,lottery_time,message from user_lottery_info WHERE order_id=#orderId# </select> <select id="user.lottery.info.query" parameterClass="userLotteryInfoDO" resultMap="userLotteryResultMap"> <include refid="user.lottery.info.selector"/> <include refid="user.lottery.info.query_segment"/> </select> <!--6.12 新增加根据userId查询的sql语句--> <select id="user.lottery.info.select.by.user.id" parameterClass="long" resultMap="userLotteryResultMap"> <![CDATA[ select id,activity_id,win,ump_activity_id,user_id,order_id,gmt_create,gmt_modified,lottery_time,message from user_lottery_info where user_id = #userId# ]]> </select> </sqlMap>
这里面有sql语句有关键词select,insert等,类型比较多
时间: 2024-11-12 00:50:50