mybatis的知识点?

发布于:2022-12-27 ⋅ 阅读:(369) ⋅ 点赞:(0)

数据库的字段名称和实体名属性名称不一致,则不能进行封装

第一种解决方案:

起别名: 对不一样的列名起别名,让实体类的属性名要一致 (缺点:每次查询都需要定义一次别名)
2.定义sql的片段: sql缺点不灵活

3.resultMap: id:是唯一的标识  type :类型  (哪一个类型进行映射)

实例图 resultMap

 sql片段实例:

 

条件查询  status brandName companyName 三个参数 (sql设置多个参数几种方式)

在 Mapper接口中定义多条件查询的方法。

而该功能有三个参数,我们就需要考虑定义接口时,参数应该如何定义。Mybatis针对多参数有多种实现

第一种:

使用 @Param("参数名称") 标记每一个参数,在映射配置文件中就需要使用 #{参数名称} 进行占位

@Param("内容")和映射配置文件的SQL中使用 #{内容} 时,里面的内容必须和实体类属性名保持一致。

List<Brand> selectByCondition(@Param("status") int status,
 @Param("companyName") String companyName,
@Param("brandName") String brandName);

第二种:

多个参数封装成一个 实体对象 ,将该实体对象作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 #{内容} 时,里面的内容必须和实体类属性名保持一致。

List<Brand> selectByCondition(Brand brand);

第三种:
多个参数封装到map集合中,将map集合作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 #{内容} 时,里面的内容必须和map集合中键的名称一致

List<Brand> selectByCondition(Map map);
  String companyName = "华为";


map.put("companyName", companyName);

mapper.xml的文件:

<select id="selectByCondition" resultMap="brandResultMap">
    select *
    from tb_brand
    where status = #{status}
    and company_name like #{companyName}
    and brand_name like #{brandName}
</select>

这种查询不是特别人性化,必须三个条件一起满足才可以,所以一般不会使用的
使用的动态条件查询

动态条件查询:

<select id="selectByCondition" resultMap="brandResultMap">
    select *
    from tb_brand
    where
        <if test="status != null">
            and 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>


if:条件判断
text:逻辑表达式

这种查询会出现问题的如果第一个为条件为空的话,会出现sql语句语法的问题
报错:  select * from tb_barnd  and where  like ?  多一个and 

解决:恒等式  where  1=1 

<select id="selectByCondition" resultMap="brandResultMap">
    select *
    from tb_brand
    where 1=1 
        <if test="status != null">
            and 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>

解决方案二<where>标签

<select id="selectByCondition" resultMap="brandResultMap">
    select *
    from tb_brand
    <where>
        <if test="status != null">
            and 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>

单条件动态条件查询:

choose(when otherwise) :选择类似于java里面的swith语句

xml mapper文件:

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



第二种:

<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>
              <otherwise>
                1=1
             <otherwise>  <!-- default类似于 不加 1=1报错-->
        </choose>

</select>

  

单条件查询场景: 


网站公告

今日签到

点亮在社区的每一天
去签到