mybaties 增删改查

<?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" >
<mapper namespace="my">
<!--当数据库字段名与java中名称不一致时 修改字段名称使其一致-->
<resultMap>
<result column="id" property="idghy"/>
</resultMap>


<!--当sql语句中又多处一致时,可将其提取出来-->
<sql id="useinfo">id,username,password,age</sql>
<select id="getUserInfo" resultType="map" parameterType="int">
select 
<include refid="userinfo"/>
from userinfo where id=#{0}
</select>
<!--
keyProperty selectKey 语句结果应该被设置的目标属性。
resultType 结果的类型。MyBatis 通常可以算出来,但是写上也没有问题。MyBatis 允许任何简单类型用作主键的类型,包括字符串。
order 这可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE,那么它会首先选择主键,设置 keyProperty 然后执行插入语句。如果设置为 AFTER,那么先执行插入语句,然后是 selectKey 元素-这和如 Oracle 数据库相似,可以在插入语句中嵌入序列调用。
statementType 和前面的相 同,MyBatis 支持 STATEMENT ,PREPARED 和CALLABLE 语句的映射类型,分别代表 PreparedStatement 和CallableStatement 类型。
-->
<insert id="insertinfo" parameType="orm.Userinfo">
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.long">
select idauto.nextval from dual
</selectKey>
insert into userinfo(id,password,age)values(
#{id,jdbcType=INTEGER},#{password,jdbcType=VARCHAR},#{age,jdbcType=INTEGER}
)
</insert>


<!--if标签的使用-->
<insert id="" parameterType="orm.Userinfo">
<if test="password!=null">
insert into userinfo(id,password,age)values(
#{id},#{password},#{age}
)
</if>
<if  test="password==null">
insert into userinfo(id,age)values(#{id},#{age})
</if>
</insert>
<!--choose标签的使用   默认break-->
<select id="" parameterType="org.Userinfo" resultType="map">
select * from userinfo where 1=1
<choose>
<when test="username!=null">and username like '%'||#{username}||'%'</when>
<when test="password!=null">and password like '%'||#{password}||'%'</when>
<otherwise>and age=200</otherwise>
</choose>
</select>


<!--set,if标签使用-->
<update id="" parameterType="orm.Userinfo">
update userinfo
<set>
<if test="username!=null">username=#{username},</if>
<if test="password!=null">password=#{password},</if>
</set>
<if test="id!=null">where id=#{id},</if>
</update>
<!--foreach标签的使用-->
<select id="" parameterType="list" resultType="map">
select * from userinfo where id in 
<foreach collection="idList" item="eachId" index="currentIndex" open="(" separator="," close=")">
#{eachId}
</foreach>
and username like '%'||#{username}||'%'
</select>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值