https://www.cnblogs.com/xdp-gacl/p/4261895.html
今天整合springboot和mybatis mysql

1.pom依赖

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

2.项目结构

3.创建application.yml

mybatis:
    # 配置mapper的扫描,找到所有的mapper.xml映射文件
    mapper-locations: classpath:/mapper/*Mapper.xml
    ## 配置类型别名
    type-aliases-package: com.liuchenhong.model
    # 加载全局的配置文件 --这个我做的时候暂时没有配置 不过估计最后项目成型后就有了
    configLocation: classpath:mybatis/mybatis-config.xml

4.mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <properties>
        <property name="dialect" value="mysql" />
    </properties>
    <settings>
        <!-- 开启驼峰匹配 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- 这个配置使全局的映射器启用或禁用缓存。系统默认值是true,设置只是为了展示出来 -->
        <setting name="cacheEnabled" value="true" />
        <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 系统默认值是true,设置只是为了展示出来 -->
        <setting name="lazyLoadingEnabled" value="true" />
        <!-- 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动)。 系统默认值是true,设置只是为了展示出来 -->
        <setting name="multipleResultSetsEnabled" value="true" />
        <!--使用列标签代替列名。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动。 系统默认值是true,设置只是为了展示出来 -->
        <setting name="useColumnLabel" value="true" />
        <!--允许 JDBC 支持生成的键。需要适合的驱动。如果设置为 true 则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如 
            Derby)。 系统默认值是false,设置只是为了展示出来 -->
        <setting name="useGeneratedKeys" value="false" />
        <!--配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。BATCH 执行器重用语句和批量更新 系统默认值是SIMPLE,设置只是为了展示出来 -->
        <setting name="defaultExecutorType" value="SIMPLE" />
        <!--设置超时时间,它决定驱动等待一个数据库响应的时间。 系统默认值是null,设置只是为了展示出来 -->
        <setting name="defaultStatementTimeout" value="25000" />
    </settings>

    <!-- 分页助手 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 数据库方言 -->
            <property name="dialect" value="mysql" />
            <property name="offsetAsPageNum" value="true" />
            <!-- 设置为true时,使用RowBounds分页会进行count查询 会去查询出总数 -->
            <property name="rowBoundsWithCount" value="true" />
            <property name="pageSizeZero" value="true" />
            <property name="reasonable" value="true" />
        </plugin>
    </plugins>
</configuration> 

5.创建model对象

这个里面没什么就是普通的javabean

public class GalaxyEvent implements Serializable{
    private String eventId;
    private String eventName;
    private String eventGroup;
    private String eventDesc;
    private Long createTime;
    private Long updateTime;
    set.get方法。..
    }

6.在mapper文件夹内创建sql映射文件

这个里面东西比较多 详见

<?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="com.liuchenhong.dao.mapper.GalaxyEventMapper">
    <resultMap id="GalaxyEvent" type="com.liuchenhong.model.GalaxyEvent" >
        <id property="eventId" column="event_id"/>
        <result property="eventName" column="event_name"/>
        <result property="eventGroup" column="event_group"/>
        <result property="eventDesc" column="event_desc"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="getAllGalaxyEvent" resultMap="GalaxyEvent">
        select event_id,event_name,event_group,event_desc,create_time,update_time from t_galaxy_event
    </select>
...

</mapper>

7.创建dao层接口

public interface GalaxyEventMapper {

    public int insertGalaxyEvent(GalaxyEvent galaxyEvent);

    public int deleteGalaxyEventById(int id);

    public int updateGalaxyEvent(GalaxyEvent galaxyEvent);

    public GalaxyEvent getGalaxyEventById(int id);

    public List<GalaxyEvent> getAllGalaxyEvent();
}

8.创建service类

@Service
public class GalaxyEventService {

    @Autowired
    private GalaxyEventMapper galaxyEventMapper;

    public List<GalaxyEvent> getAllGalaxyEvent(){
        return galaxyEventMapper.getAllGalaxyEvent();
    }
}

9.创建controller

@RestController
public class GalaxyEventController {
    @Autowired
    private GalaxyEventService galaxyEventService;

    @GetMapping("/user")
    public List<GalaxyEvent> getAllGalaxyEvent(){
        List<GalaxyEvent> list=galaxyEventService.getAllGalaxyEvent();
        return  list;
    }
}

最后记住 在dao层没有加@Mapper注解 是因为我在applicaton类加了全局扫描的注解

@SpringBootApplication
@MapperScan("com.liuchenhong.dao.mapper")
public class LchApplication {

    public static void main(String[] args) {
        SpringApplication.run(LchApplication.class, args);
    }
}

最后运行结果完成