发表于: 2018-10-13 23:25:55

1 538


今天完成的事情:

手写了art表的xml映射文件,表中的字段太多了,很容易出错,主要是条件更新和条件查找这两个接口的映射文件。

中间出现了很多问题,刚开始是无法将mapper注册为bean,最终发现xml文件中有同名的id,删掉就好了。

接着又出现下面这个问题

出现这个错误的原因是,数据库的编码格式为latin1 而我要将utf8的中文插入到数据库中。,修改数据库中相应字段的编码格式。

这是sql拼接的错误


这个问题可以通过<where>标签来解决,如代码所示
 <select id="findArtSelective"  resultType="com.jnshu.entity.Art">
       SELECT FROM art
<where>
           <if test="name != null"OR name LIKE CONCAT('%',#{name},'%')</if>
        <!--   <if test="status == true"> OR status = #{status}  </if>-->
         <if test="secondId != 0"OR second_id = #{secondId}</if>
       </where>
   </select>

<mapper namespace="com.jnshu.mapper.ArtMapper">
   <!--//新增作品 boolean insertArt(Art art);-->
   <insert id="insertArt" parameterType="com.jnshu.entity.Art">
     INSERT INTO art
(author,name,status,first_id,second_id,introduce,img_second_naill,video,
is_link,img_detail,article_detail,create_by,create_at,update_by,update_at)
VALUES
(#{author},#{name},status,#{firstId},#{secondId},#{introduce},#{imgSecondNaill},#{video},
#{isLink},#{imgDetail}, #{articleDetail},#{createBy},#{createAt},#{updateBy},#{updateAt})
</insert>

   <!--//根据查询条件多条件模糊查询获取作品列表-->
   <!--List<Art> findArtSelective(@Param("name")String name,
   @Param("byte")byte status, @Param("secondId")long secondId);-->
   <select id="findArtSelective"  resultType="com.jnshu.entity.Art">
       SELECT * FROM art
<where>
           <if test="name != null"> OR name LIKE CONCAT('%',#{name},'%')</if>
        <!--   <if test="status == true"> OR status = #{status}  </if>-->
         <if test="secondId != 0"> OR second_id = #{secondId}</if>
       </where>
   </select>


   <!--//作品上下架 boolean updateArtSelective(long id,byte status);-->
   <update id="isOnlineArt" parameterType="com.jnshu.entity.Art">
       update art
<set>
           <if test = "status != null">status = #{status}</if>
       </set>
       where id=#{id}
</update>

  <!-- //获取作品详情 Art findArtDetail(long id);-->
   <select id="findArtDetail" parameterType="long" resultType="com.jnshu.entity.Art">
        select * from art where id = #{id}
</select>


  <!-- //根据主键删除作品 boolean deleteArt(long id);-->
   <delete id="deleteArt">
         DELETE FROM art WHERE id=#{id}
</delete>

   <!--//根据主键选择性更新非空字段 boolean updateArtSelective(Art art);-->
   <update id="updateArtSelective" parameterType="com.jnshu.entity.Art">
       update art
<set>
           <if test="author != null">author=#{author},</if>
           <if test="name != null">name=#{name},</if>
           <if test="firstId != null">first_id=#{firstId},</if>
           <if test="secondId != null">second_id=#{secondId},</if>
           <if test="introduce != null">introduce=#{introduce},</if>
           <if test="imgSecondNaill != null">img_second_naill=#{imgSecondNaill},</if>
           <if test="video != null">video=#{video},</if>
           <if test="isLink != null">is_link=#{isLink},</if>
           <if test="imgDetail != null">img_detail=#{imgDetail},</if>
           <if test="articleDetail != null">article_detail=#{articleDetail},</if>
           <if test="updateBy != null">update_by=#{updateBy},</if>
           <if test="updateAt != null">update_at=#{updateAt}</if>
       </set>
       where id=#{id}
</update>


   <!--//查询作品总记录数 long findArtTotal();-->
   <select id="findArtTotal" resultType="long">
       SELECT COUNT(*) from art
</select>

明天计划的事情:使用自动生成插件完成接口的自动生成,完成测试。

遇到的问题:传递boolean类型的字段status时报下面的错误:sql是没有问题的,多参数传递的时候去掉status就正常了。

收获:学会了mybatis映射文件sql语句传递多个参数。


返回列表 返回列表
评论

    分享到