SQL:mapper中常用方法

MyBatis Mapper配置详解
本文详细解析了MyBatis框架中Mapper配置文件的使用方法,包括实体类映射、SQL语句配置、参数传递及结果映射等关键内容。通过具体示例,介绍了分页查询、单个对象查询、全表查询、数据插入、删除、更新操作的实现方式,以及统计查询的配置。适合初学者和开发者深入理解MyBatis的数据持久层技术。
  1. resultMap 中定义实体类的名称
方法名方法参数返回值
分页查询findByPage实体类对象List
根据ID查询findById实体类对象实体类对象
查询全部find实体类对象List
插入insertCart-
删除delete实体类对象-
更新update实体类对象-
更新全部updateAll--
统计count实体类对象int
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jy.repository.order.CartDao">

    <resultMap id="base" type="Cart">
        <id column="id" jdbcType="INTEGER" property="id"/>
    </resultMap>

    <!-- 分页查询List -->
    <select id="findByPage" resultMap="base" parameterType="Cart">
        SELECT s.*  FROM
        sc_cart s
        where 1=1
        and s.userId = #{param.userId}
        and s.rentType = #{param.rentType}
        <if test="param.selected == 1">
            and s.selected = #{param.selected}
        </if>
        ORDER BY id desc
    </select>

    <!-- 查询单个对象 -->
    <select id="findById" resultMap="base" parameterType="Cart">
        SELECT s.* FROM
        sc_cart s
        where  s.id = #{id}
    </select>

    <select id="find" resultMap="base"  parameterType="Cart">
        SELECT s.* FROM
        sc_cart s
        WHERE  1 = 1
        and s.goodsId = #{goodsId}
        and s.userId = #{userId}
    </select>
    <select id="count" resultType="int" parameterType="Cart">
        SELECT count(*) FROM sc_cart s where s.userId = #{userId}
    </select>
    <insert id="insert"  parameterType="Cart">
        INSERT INTO sc_cart(
        goodsId,
        num,
        money,
        userId,
        goodsName,
        goodsImg,
        state,
        createTime,
        firstWeekPrice,
        subsequentPrice,
        rentType
        ) VALUES (
        #{goodsId},
        #{num},
        #{money},
        #{userId},
        #{goodsName},
        #{goodsImg},
        #{state},
        now(),
        #{firstWeekPrice},
        #{subsequentPrice},
         #{rentType}
        )
    </insert>
    <delete id="delete" parameterType="Cart">
        delete from sc_cart where 1=1
        <if test='id!="" and id != null'>
        and id = #{id} and userId=#{userId}
         </if>
        <if test='goodsId !="" and goodsId != null'>
            and goodsId = #{goodsId}  AND userId=#{userId}
        </if>

    </delete>
    <update id="update">
        UPDATE sc_cart s set s.selected =  #{selected} where s.id = #{id}
    </update>
    <update id="updateAll">
         UPDATE sc_cart s set s.selected =  #{selected}   WHERE s.userId = #{userId}
    </update>
    <select flushCache="true" id="select_ids" parameterType="Map" resultType="Cart">
        SELECT s.*, c.attchentUrl img,a.goodsType goodsType,a.goodsSubType goodsSubType, b.userId agencyUserId,b.account agencyAccount,b.agencyName  FROM
        sc_cart s
        LEFT JOIN cxy_attachment c on s.goodsId = c.goodsId  and  c.goodType=1 and attchentName='small1'
        LEFT JOIN sc_goods a on s.goodsId=a.id
        LEFT JOIN sc_service_info b on a.userId=b.userId
        where s.id in
        <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
            #{item}
        </foreach>
        and s.userId = #{userId}  order by s.id desc
    </select>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值