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文件中的格式形同导致出现某些字符无法正常使用
<select id="selectAllByd" parameterType="int" resultMap="brandResultMap">
# "小于号《" 报错 所以需要代替 解决方法
# 1. < --> "小于号";
select *
from tb_brand
where id < #{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);
即用户必须输入所需的所有条件才能查询到结果假若只查找一个条件则无法查出相应的结果
解决方法 : 动态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语法会出现错误
解决方案
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>