JavaWeb--MyBatis 通过id查看表中数据、特殊字符处理(CDATA区)、条件查询(if、choose、when、otherwise)

JavaWeb–MyBatis

通过id查看表中数据

1.在mapper接口中构建selectAllById方法通过MyBatisX映射到sql映像中去
注意分析此时返回的应该是一个对象

Brand selectAllById(int id);
 <resultMap id="brandResultMap" type="brand">
        <!--   主键的映射     <id></id>-->
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
 </resultMap>
<select id="selectAllById" resultMap="brandResultMap">
        select *
        from tb_brand
        where id = #{id};
</select>

这里还有一个属性parameterType=“int”,因为严格要求过传过来的是int类型的
所以这里忽略也没有问题

注意事项

SQL语句一定要写正确
${id}:为字符串拼接不能防止sql注入问题,因此常用于索要查找的某些字段不确定时代替变量成为类似占位符的符号
#{id}:参数传递,防止啦sql注入,非字符串拼接。常用于做sql语句的条件判断。

特殊字符处理

因为sql语句中的某些字符会与xml文件中的格式形同导致出现某些字符无法正常使用

  1. 转译字符
    2.CDATA区
 <select id="selectAllByd" parameterType="int" resultMap="brandResultMap">
# "小于号《" 报错 所以需要代替 解决方法
# 1. &lt; --> "小于号";
         select *
        from tb_brand
        where id &lt; #{id};
# 2.CDATA
        select *
        from tb_brand
        where id <![CDATA[
        <
        ]]> #{id};
</select>

条件查询

传递多个参数

1.@Param注解进行标记

在这里插入图片描述

//BrandMapper.xml
<select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where status = #{status}
        and company_name like  #{companyName}
        and brand_name like #{brandName};
    </select>
//Mapper
List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
//Test
//接收参数时要对参数进行处理
  companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";
List<Brand> brands = mapper.selectByCondition(status, companyName, brandName);

2.如果所有参数为同一个对象的内容封装成为一个对象

//mapper
List<Brand> selectByCondition(Brand brand);
//BrandMapper
<select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where status = #{status}
        and company_name like  #{companyName}
        and brand_name like #{brandName};
    </select>
//Test
 //参数进行处理
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";

        // 封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
//调用方法
List<Brand> brands = mapper.selectByCondition(brand);

3.封装成为Map集合。

//mapper
List<Brand> selectByCondition(Map map); 
//Brandmapper.xml
<select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where status = #{status}
        and company_name like  #{companyName}
        and brand_name like #{brandName};
    </select>
//Test
//接收参数时要对参数进行处理
  companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";
 //Map集合
        Map map = new HashMap();
        map.put("status",status);
        map.put("companyName",companyName);
        map.put("brandName",brandName);
//调用方法
 List<Brand> brands = mapper.selectByCondition(map);
        
       

结果 三个方法均可得出结果
在这里插入图片描述

缺陷存在bug

即用户必须输入所需的所有条件才能查询到结果假若只查找一个条件则无法查出相应的结果
解决方法 : 动态SQL查询

动态SQL查询

动态 SQL 是 MyBatis 的强大特性之一。

if
choose (when, otherwise)
trim (where, set)
foreach

 <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>
    </select>

改为这样之后能够进行动态查询,但是仍然存在问题
就是当第一个条件为空sql语法会出现错误
解决方案

  1. 恒等式
  2. 标签自动解决
 3. <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>

单条件的动态查询
选择条件 输入条件后进行查询
在这里插入图片描述
where 标签也可解决
chose when 的使用

 <select id="selectByConditionsingle" resultType="com.itheima.pojo.Brand">
    select *
    from tb_brand
    <where>
        <choose>
            <when test="status != null">
                status = #{status}
            </when>
            <when test="companyName != null and companyName !=''">
                company_name like #{companyName}
            </when>
            <when test="companyName != null and companyName !=''">
                brand_name like #{brandName};
            </when>
        </choose>
    </where>

</select>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值