mapper.xml书写模板

<?xml version="1.1" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace: 代表xml的名称,类似java包名-->
<mapper namespace="com.liang.dao.ProductDao">
<!--
      select: 代表进行查询操作。
          id: 之前的方法名称,具有唯一性。
   resultType: 返回值类型。
               如果返回的是对象,直接书写对象类型的的完整名。
               如果是集合,书写的是集合的泛型
 parameterType: 参数类型,可以省略。
   -->
    <select id="selectAll" resultType="product" >
        select  *  from product
    </select>
    <select id="selectOne" resultType="product" >
        select  *  from product where id=2
    </select>
    <select id="selectOne2" resultType="product" >
        select  *  from product where id=#{param1} and name=#{param2}
    </select>
    <select id="selectOne3" resultType="product" >
        select  *  from product where id=${param1} and name=#{param2}
    </select>
    <select id="selectLike1" resultType="product" >
        select  *  from product where name like concat ('%','芒果','%')
    </select>
    <sql id="myColumn">
        id,name,address,categoryId
    </sql>
    <select id="selectLike2" resultMap="myresultMap1">
        select  <include refid="myColumn"/>from product where name like concat ('%','芒果','%')
    </select>
    <resultMap id="myresultMap1" type="com.liang.model.Product">
    </resultMap>
    <select id="select" resultMap="myresultMap1">
        select id,name,address,categoryId from product
        <where>
            <if test="id!=null">
                and id=#{id}
            </if>
            <if test="ids!=null">
                and id in
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="categoryId != null">
                and categoryId= #{categoryId}
            </if>
            <if test="categoryIds != null">
                and categoryId in
                <foreach collection="categoryIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="name != null">
                and name like concat('%',#{name},'%')
            </if>

        </where>
    </select>
    <insert id="insert">
        insert into product (name,address) values (#{name},#{address})
    <selectKey resultType="integer" keyColumn="newId" keyProperty="product" order="AFTER">
        select last_insert_id() as newId
    </selectKey>
    </insert>
    <update id="update">
        insert into product (name,address) values (#{name},#{address})
    </update>
    <delete id="delete">
        delete from product where id=#{param1}
    </delete>
</mapper>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值