mybatis配置文件常用语法mapper.xml

本文介绍了一个使用MyBatis进行SQL映射的例子,包括条件查询、单条记录查询、插入操作及多条记录查询等场景。通过具体的XML配置文件展示了如何定义SQL片段并重用,以及如何实现不同类型的数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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.sun.dao.UserDao">
    <!--封装sql条件,封装可以重用-->
    <sql id="user_Where">
        <where>
            <if test="iSecondhandID !=null">
                and iSecondhandID=#{iSecondhandID}
            </if>
            <if test="iStatus !=null">
                and iStatus=#{iStatus}
            </if>
        </where>
    </sql>

    <!-- 查询单条记录 -->
    <select id="selectUserById" parameterType="int" resultType="com.sun.dto.User">
        SELECT * FROM t_secondhand_pic WHERE iAutoID = #{value}
    </select>

    <!--插入单条记录-->
    <insert id="insert" parameterType="int">
        INSERT INTO t_secondhand_pic(iStatus,sImgKey,sExt,iImgType) VALUES(#{value},'19dc9080f7abc866ea7ab857d9cd03c7032e7a96','jpg','2');
    </insert>

    <!--查询多条语句-->
    <select id="selectByUser" parameterType="com.sun.dto.UserVO" resultType="com.sun.dto.User">
        select * from t_secondhand_pic where iSecondhandID=#{user.iSecondhandID} and iStatus=#{user.iStatus}
    </select>

    <!--查询统计个数-->
    <!--UserDao public List<User> selectByUser(UserVO userVO);-->
    <select id="selectCount" parameterType="com.sun.dto.UserVO" resultType="int">
        select count(*) from t_secondhand_pic where iSecondhandID=#{user.iSecondhandID} and iStatus=#{user.iStatus}
    </select>

    <!--查询多个where语句-->
    <select id="selectWhere" parameterType="com.sun.dto.User" resultType="com.sun.dto.User">
        select * from t_secondhand_pic
        <!--调用sql条件-->
        <include refid="user_Where"/>
    </select>

    <!--查询 in 语句-->
<!--    <select id="selectIn" parameterType="com.sun.dto.User" resultType="com.sun.dto.User">
        select * from t_secondhand_pic
        <where>
            <if test="ids !=null" >
                &lt;!&ndash;
                    foreach:循环传入的集合参数
                    collection:出入集合的变量名称
                    item:每次循环将循环出的数据传入到这个变量中
                    open:循环开始拼接的字符串
                    close:循环结束拼接的字符串
                    separator:循环中的分隔符
                &ndash;&gt;
                <foreach collection="ids" item="id" open="id in (" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>-->
    <!--一对一自动映射-->
  <!--  <select id="selectOneToOne" resultType="CustomOrder">
        select
    </select>-->
    <!--一对一手动映射-->

    <!--多对多映射-->
</mapper>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值