spring-boot mybatis

2022-04-09  本文已影响0人  shoyu666

集成

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>
....
<select id="findActiveBlogWithTitleLike"
     resultType="Blog">
  SELECT * FROM BLOG
  WHERE state = ‘ACTIVE’
  <if test="title != null">
    AND title like #{title}
  </if>
</select>
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>
<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>
# 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
上一篇下一篇

猜你喜欢

热点阅读