spring boot 使用 mybatis

 

1. 引入依赖

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>

2. 指定mapper.xml文件的地址

mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

 

3.  启动类上指定 @Mapper注解了的接口的位置

@MapperScan("xxx.mapper")

 

4. mapper.xml示例

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.PostMapper">

    <select id="postWithPoints"
            parameterType="xxx.TopicFilter" resultType="java.util.Map">
        select a.*,b.points from topic_post a
            left join topic_post_selected b on a.id=b.post_id
            <where>
                <if test="state != null">
                    and a.state = #{state}
                </if>
            </where>
            order by a.id desc
            limit #{limit} , #{pageSize}
    </select>

</mapper>