首先,你得有个Spring Boot项目。

平时开发常用的repository包在mybatis里被替换成了mapper。

配置:

1.引入依赖:

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

2.编辑 application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.servlet.multipart.max-request-size=2050MB
spring.servlet.multipart.max-file-size=2048MB spring.jpa.hibernate.use-new-id-generator-mappings=false
server.port=8080
server.servlet.context-path=/mybatisDemo
spring.jmx.enabled=false mybatis.type-aliases-package=tgc.edu.wx.entity
mybatis.mapperLocations=classpath:mapping/*.xml

这里要注意mybatis的两个相关配置,一是扫描的包,二是映射文件的地址。

3.建立构建web项目所需的类,以及在数据库建立实体类对应的表,完成后如下图:

PeopleMapper.java

package tgc.edu.wx.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import tgc.edu.wx.entity.People;

@Mapper
public interface PeopleMapper { public List<People> findAll();
}

此文件注解为@Mapper而不再是@Repository。

PeopleService.java

package tgc.edu.wx.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import tgc.edu.wx.entity.People;
import tgc.edu.wx.mapper.PeopleMapper; @Service
public class PeopleService { @Autowired
private PeopleMapper peopleDAO; public List<People> findAll() {
return peopleDAO.findAll();
}
}

PeopleController.java

package tgc.edu.wx.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; import tgc.edu.wx.entity.People;
import tgc.edu.wx.service.PeopleService; @Controller
@RequestMapping("/people")
public class PeopleController { @Autowired
private PeopleService peopleService; @RequestMapping("/list")
public String list(ModelMap map) {
List<People> peoples = peopleService.findAll();
map.put("peoples", peoples);
return "peopleList";
}
}

数据库:

4.依照 application.properties 中编写的地址在对应目录下创建xml格式的mybatis映射文件。

<?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="tgc.edu.wx.mapper.PeopleMapper"> <select id="findAll" resultType="tgc.edu.wx.entity.People">
SELECT * FROM people
</select>
</mapper>

其中<select>标签内的 id 对应的是dao层内的方法名,resultType对应的是查询返回结果集的类型,select内填写对应方法的语句即可。

5.最后,在启动类里加上注解用于给出需要扫描的mapper文件路径

package tgc.edu.wx;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("tgc.edu.wx.mapper")
public class MybatisDemoApplication { public static void main(String[] args) {
SpringApplication.run(MybatisDemoApplication.class, args);
} }

启动项目测试一下:

以上。

Spring Boot整合Mybatis配置详解的更多相关文章

  1. spring boot application properties配置详解

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  2. (转)Spring boot——logback.xml 配置详解(四)<filter>

    文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 filter的使用 <filter>: Logback的过滤器基于三值逻辑( ...

  3. (转)Spring boot——logback.xml 配置详解(二)

    文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 根节点<configuration>包含的属性 scan: 当此属性设置为t ...

  4. Spring boot——logback.xml 配置详解(四)<filter>

    阅读目录 1 filter的使用 2 常用的过滤器 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 filter的使用 < ...

  5. Spring boot——logback.xml 配置详解(二)

    阅读目录 1 根节点包含的属性 2 根节点的子节点 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 根节点<config ...

  6. (转)Spring boot——logback.xml 配置详解(三)<appender>

    文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 appender <appender>是<configuration& ...

  7. Spring boot——logback.xml 配置详解(三)<appender>

    阅读目录 1 appender 2  encoder 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 appender < ...

  8. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  9. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

随机推荐

  1. 关于 ASP.NET Core 中的 RazorPages

    Contact.cshtml @page @model ContactModel @{ ViewData["Title"] = "Contact"; } < ...

  2. FastJson中的ObjectMapper对象的使用详解

    写在前面:开发中经常用到json和对象的相互转换,下面将列出FastJson中ObjectMapper对象的API的使用 一.maven工程中pom导入<dependency> <g ...

  3. Python爬取6271家死亡公司数据,看十年创业公司消亡史

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 朱小五 凹凸玩数据 PS:如有需要Python学习资料的小伙伴可以加 ...

  4. php 利用curl_*测试数据并发

    工作时遇到一个数据并发问题,因为上线之前没有测试数据并发,导致有时候网络比较差的时候导致数据重复插入数据库 , 所以利用curl_*函数专门写了一个测试数据并发的测试用例,如下: function t ...

  5. Java生鲜电商平台-秒杀系统微服务架构设计与源码解析实战

    Java生鲜电商平台-秒杀系统微服务架构设计与源码解析实战 Java生鲜电商平台-  什么是秒杀 通俗一点讲就是网络商家为促销等目的组织的网上限时抢购活动 比如说京东秒杀,就是一种定时定量秒杀,在规定 ...

  6. tinyriscv---一个从零开始写的极简、易懂的开源RISC-V处理器核

    本项目实现的是一个微riscv处理器核(tinyriscv),用verilog语言编写,只求以最简单.最通俗易懂的方式实现riscv指令的功能,因此没有特意去对代码做任何的优化,因此你会看到里面写的代 ...

  7. CSS3 动画--- CSS3 animation

    动画是CSS3中具有颠覆性的特征之一,可通过设置多个节点来精确控制一个或一组动画,常用来实现复杂的动画效果. 语法格式: animation:动画名称 花费时间 运动曲线 何时开始 播放次数 是否反方 ...

  8. .NET MVC后台获得VIEW对应的html

    一..Net Core Mvc下获得 建立一个帮助类,如下: using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Render ...

  9. jqgrid后台处理搜索

    jqgrid后台处理搜索, 如果点击jqgrid自带的搜索,则向后台传递“_search”参数,和searchField.searchOper.searchString三个值.如下所示: string ...

  10. 为Dynamics 365 USD设置使用Chrome进程来驻留Web应用程序

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...