MyBatis查询

MyBatis主要方法:
1.编写接口方法:Mapper接口
eg:List selectAll();
2.编写SQL语句:
SQL映射文件eg:

点击查看代码
<select id="selectAll" resultType="Brand">
  select *
  from tb_brand;
</select>
3.执行测试方法

1)查询所有:

点击查看代码
<!--
        id:完成主键映射
            column:表的列名
            property:实体类的属性名
        result:完成一般映射
            column:表的列名
            property:实体类的属性名
-->
        <result column="brand_name" property="brandName"/>
        <result column="compant_name" property="companyName"/>
    </resultMap>
    
<!--    <sql id="huhu">-->
<!--        id, brand_name as brandName, company_name as companyName, ordered, description, status-->
<!--    </sql>-->
<!--    <select id="selectAll" resultType="brand">-->
<!--        select *-->
<!--        from tb_brand;-->
<!--    </select>-->


<!--    <select id="selectAll" resultType="brand">-->
<!--        select-->
<!--            <include refid="huhu"/>-->
<!--        from tb_brand;-->
<!--    </select>-->

    <select id="selectAll" resultMap="brandResultMap">
        select
        *
        from tb_brand;
    </select>
2)查看详情
点击查看代码
<!--
    * 参数占位符
        1.#{}:替换为? 防止SQL注入
        2.${}:拼sql 存在SQL注入
        3.使用时机
            * 参数传递——#{}
            *表名或列名不固定——${}

    * 参数类型:parameterType:可以省略
    * 特殊字符处理:
        1.转义字符:< ___ &lt;
        2.CDATA
            <![CDATA[

            ]]>

-->
    <select id="selectById" parameterType="int" resultMap="brandResultMap">
        select *
        from tb_brand where id <![CDATA[

        ]]> = #{id};
    </select>
3)多条件查询:
点击查看代码
/**
     * 条件查询
     *  * 参数接受
     *      1.散装参数:如果有多个参数,需要使用@Param("SQL参数占位符名称")
     *      2.对象参数:对象属性名称要与参数占位符名称一致
     *      3.map合集参数
     *
     * @param status
     * @param companyName
     * @param brandName
     * @return
     */
//    List<Brand> selectByCondition(@Param("status")int status,
//                                @Param("companyName")String companyName,
//                                @Param("brandName")String brandName);


    List<Brand> selectByCondition(Brand brand);
    List<Brand> selectByCondition(Map map);


//SQL语句:
<select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where status = #{status}
        and company_name like #{companyName}
        and brand_name like #{brandName}
    </select>
4)多条件动态查询
点击查看代码
<!--    动态条件查询
        *if 条件查询
            *test 逻辑表达式
        *问题:
            *恒等式
            *<where>替换where
    -->

    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <if test="status! = null">
                status = #{status}
            </if>
            <if test="companyName !=null and companyName !=' ' ">
                and company_name like #{companyName}
            </if>
            <if test="brandName !=null and brandName!= ' ' ">
                and brand_name like #{brandName}
            </if>
        </where>
    </select>
5)单条件动态查询
点击查看代码
<!--    <select id="selectByConditionSingle" resultMap="brandResultMap">-->
<!--        select *-->
<!--        from tb_brand-->
<!--        where -->
<!--            <choose> &lt;!&ndash; 相当于switch&ndash;&gt;-->
<!--                <when test="status!=null">&lt;!&ndash;相当与case&ndash;&gt;-->
<!--                    status =#{status}-->
<!--                </when>-->
<!--                <when test="companyName != null and companyName != ''">&lt;!&ndash;相当与case&ndash;&gt;-->
<!--                    company_name like #{companyName}-->
<!--                </when>-->
<!--                <when test="brandName != null and brandName != '' ">&lt;!&ndash;相当与case&ndash;&gt;-->
<!--                    brand_name like #{brandName}-->
<!--                </when>-->
<!--                <otherwise>-->
<!--                    1=1-->
<!--                </otherwise>-->
<!--            </choose>-->
<!--    </select>-->
    <select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
        <choose> <!-- 相当于switch-->
            <when test="status!=null"><!--相当与case-->
                status =#{status}
            </when>
            <when test="companyName != null and companyName != ''"><!--相当与case-->
                company_name like #{companyName}
            </when>
            <when test="brandName != null and brandName != '' "><!--相当与case-->
                brand_name like #{brandName}
            </when>

        </choose>
        </where>
    </select>
posted @   QixunQiu  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示