• JSD-2204-MyBatis动态sql,修改,查询-Day04


    1.MyBatis的动态SQL--foreach

    动态SQL:根据参数值的不同,将生成不同的SQL语句。

    假设存在需求:根据若干个id删除相册数据,即批量删除。

    需要执行的SQL语句大致是:

    delete from pms_album where id=? or id=? or id=? ...
    

    或者:

    delete from pms_album where id in (?, ?, ?, ... ?);
    

    当实现以上功能时,关于抽象方法,可以设计为:

    int deleteByIds(Long[] ids);
    

    或者:

    int deleteByIds(Long... ids);
    

    或者:

    int deleteByIds(List ids);
    

    在配置SQL时,需要使用到节点对参数进行遍历:

    1. <delete id="deleteByIds">
    2. DELETE FROM pms_album
    3. WHERE id IN (
    4. <foreach collection="array" item="id" separator=",">
    5. #{id}
    6. foreach>
    7. )
    8. delete>

    关于节点的配置:

    • collection属性:当抽象方法的参数只有1个且没有添加@Param注解时,当参数是数组类型时(包括类型为可变参数时),此属性取值为array,当参数是List集合类型时,此属性取值为list
    • item属性:遍历过程中的每个元素的变量名,是自定义的名称
    • separator属性:遍历过程中各元素之间的分隔符号

    练习:批量插入相册数据

    需要执行的SQL语句大致是:

    insert into pms_album (name,description,sort) values (?,?,?), (?,?,?), (?,?,?)
    

    AlbumMapper中添加抽象方法:

    int insertBatch(List albums);
    

    AlbumMapper.xml中配置SQL语句:

    1. <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
    2. INSERT INTO pms_album (
    3. name, description, sort
    4. ) values
    5. <foreach collection="list" item="album" separator=",">
    6. (#{album.name}, #{album.description}, #{album.sort})
    7. foreach>
    8. insert>

    2.使用Mybaits修改数据

    通常,修改数据时,也会使用到动态SQL的机制,当传入某个字段对应的值时,SQL中才会包含修改此字段的部分,反之,如果没有传入某个字段对应的值,则SQL语句不会包含修改此字段的部分!

    这样的功能可以通过动态SQL的标签来实现!

    假设需要实现修改相册数据,传入的参数中包含哪些数据,就修改哪些数据,不包含的部分将不会被修改。

    AlbumMapper接口中添加抽象方法:

    int update(Album album);
    

    AlbumMapper.xml中配置SQL语句:

    1. <update id="update">
    2. UPDATE pms_album
    3. <set>
    4. <if test="name != null">
    5. name=#{name},
    6. if>
    7. <if test="description != null">
    8. description=#{description},
    9. if>
    10. <if test="sort != null">
    11. sort=#{sort},
    12. if>
    13. set>
    14. WHERE id=#{id}
    15. update>

    2.1使用Mybatis查询--统计

    假设需要实现:统计相册表中的数据的数量

    需要执行的SQL语句大致是:

    select count(*) from pms_album
    

    关于抽象方法:在查询时,方法的返回值类型只要求能够存入查询结果即可。

    则在AlbumMapper中添加抽象方法:

    int count();
    

    然后,在AlbumMapper.xml中配置SQL语句,将使用上使用resultType属性,取值却是的id时,将出现以下错误:

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [C:\Users\pc\IdeaProjects\jsd2204-csmall-product-teacher\target\classes\mapper\BrandMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [C:\Users\pc\IdeaProjects\jsd2204-csmall-product-teacher\target\classes\mapper\BrandMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'StandardResultMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: StandardResultMap
    

    当在