美文网首页
spring-boot mybatis

spring-boot mybatis

作者: shoyu666 | 来源:发表于2022-04-09 11:20 被阅读0次

    集成

    https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
    https://mybatis.org/mybatis-3/zh/dynamic-sql.html

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.2</version>
    </dependency>
    
    @Mapper
    public interface BallSqlEventMapper {
        List<BallEventDO> recommends(@Param("item") BallEventQuery ballEventQuery);
    }
    
    ....
        <select id="recommends" resultMap="BaseResultMap">
               SELECT id from ....
        </select>
    ....
    
    • if
    <select id="findActiveBlogWithTitleLike"
         resultType="Blog">
      SELECT * FROM BLOG
      WHERE state = ‘ACTIVE’
      <if test="title != null">
        AND title like #{title}
      </if>
    </select>
    
    • choose (when, otherwise)
    select id="findActiveBlogLike"
         resultType="Blog">
      SELECT * FROM BLOG WHERE state = ‘ACTIVE’
      <choose>
        <when test="title != null">
          AND title like #{title}
        </when>
        <when test="author != null and author.name != null">
          AND author_name like #{author.name}
        </when>
        <otherwise>
          AND featured = 1
        </otherwise>
      </choose>
    </select>
    
    • trim (where, set)
    <select id="findActiveBlogLike"
         resultType="Blog">
      SELECT * FROM BLOG
      WHERE
      <if test="state != null">
        state = #{state}
      </if>
      <if test="title != null">
        AND title like #{title}
      </if>
      <if test="author != null and author.name != null">
        AND author_name like #{author.name}
      </if>
    </select>
    
    • foreach
    # application.properties
    mybatis.type-aliases-package=com.example.domain.model
    mybatis.type-handlers-package=com.example.typehandler
    mybatis.configuration.map-underscore-to-camel-case=true
    mybatis.configuration.default-fetch-size=100
    mybatis.configuration.default-statement-timeout=30
    

    MyBatis的Mapper原理

    https://juejin.cn/post/6844903824713334797

    image.png
    image.png image.png

    相关文章

      网友评论

          本文标题:spring-boot mybatis

          本文链接:https://www.haomeiwen.com/subject/rospsrtx.html