MyBatis案例:查看详情

MyBatis案例:查看详情

  1. 编写接口方法:Mapper接口 Brand selectById(int id);

    1. 参数:id
    2. 结果: Brand
  2. 编写SQL语句:SQL映射文件

    <select id = "selectById" parameterType = "int" resultType="brand">
    select * from tb_brand where id = #{id};
    </select>
    
  3. 执行方法

<!--BrandMapper.xml文件部分-->
<!--
        * 参数占位符:
            1. #{}:会将其替换为?,为了防止SQL注入,
            2. ${}: 拼接sql,会存在SQL注入问题
            3. 使用时机
                * 参数传递的时候:#{}
                * 表名或者列名不固定的情况下:${},但也会存在SQL注入问题
        * 参数类型:parameterType:可以省略
        * 特殊字符处理:
            1. 转义字符:
            2. CDATA区
    -->
    <select id="selectById" parameterType="int" resultMap="brandResultMap">
        select * from tb_brand where id = #{id};
    </select>

    <!--<select id="selectById" parameterType="int" resultMap="brandResultMap">
        select * from tb_brand where id <![CDATA[
            >, <
        ]]>
        #{id};
    </select>-->