Spring Boot整合Mybatis配置详解
首先,你得有个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配置详解的更多相关文章
- spring boot application properties配置详解
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...
- (转)Spring boot——logback.xml 配置详解(四)<filter>
文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 filter的使用 <filter>: Logback的过滤器基于三值逻辑( ...
- (转)Spring boot——logback.xml 配置详解(二)
文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 根节点<configuration>包含的属性 scan: 当此属性设置为t ...
- Spring boot——logback.xml 配置详解(四)<filter>
阅读目录 1 filter的使用 2 常用的过滤器 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 filter的使用 < ...
- Spring boot——logback.xml 配置详解(二)
阅读目录 1 根节点包含的属性 2 根节点的子节点 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 根节点<config ...
- (转)Spring boot——logback.xml 配置详解(三)<appender>
文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 appender <appender>是<configuration& ...
- Spring boot——logback.xml 配置详解(三)<appender>
阅读目录 1 appender 2 encoder 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 appender < ...
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- Spring MVC、MyBatis整合文件配置详解
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
随机推荐
- GPU跑tf-faster-rcnn demo以及训练自己的数据
https://blog.csdn.net/qq_39123369/article/details/85245512
- DVR登录绕过漏洞_CVE-2018-9995漏洞复现
DVR登录绕过漏洞_CVE-2018-9995漏洞复现 一.漏洞描述 此漏洞允许攻击者通过修改”Cookie:uid=admin”之后访问特定DVR的控制面板,返回此设备的明文管理员凭证. 二.影响软 ...
- LOAD_DLL_DEBUG_EVENT 时读取 DllName
这句话是说 lpImageName 和 hFile 存在关联(associated),不是一定指向! 继续读后面那句,“这个数字可能为NULL,或者包含着被调试进程空间中的一个字符串地址.这个地址,相 ...
- 死磕 java同步系列之ReentrantLock源码解析(二)——条件锁
问题 (1)条件锁是什么? (2)条件锁适用于什么场景? (3)条件锁的await()是在其它线程signal()的时候唤醒的吗? 简介 条件锁,是指在获取锁之后发现当前业务场景自己无法处理,而需要等 ...
- 通过JS屏蔽鼠标右键
我也是第一次接触这个功能,只需一行代码即可搞定,直译过来就是“屏蔽上下文菜单”,特此记录一下吧. document.oncontextmenu = () => false;
- C++ const常量对象、常量成员函数和常引用
01 常量对象 如果不希望某个对象的值被改变,则定义该对象的时候可以在前面加const关键字 class CTest { public: void SetValue() {} private: int ...
- CSS语法规范一
CSS语法规范 CSS规则由两个主要的部分构成:选择器以及一条或多条声明. p{ color: red; font-size: 12px; } CSS代码风格 样式格式书写 紧凑格式 h3 {colo ...
- cesium-webpack 入门开发系列一初探篇(附源码下载)
前言 cesium-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载地址 we ...
- DDCTF 2019 部分WP
WEB 滴~ http://117.51.150.246/index.php?jpg=TmpZMlF6WXhOamN5UlRaQk56QTJOdz09 观察链接可发现jpg的值是文件名转hex再bas ...
- 滴滴出行开源项目doraemonkit食用指南
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/94 doraemonkit 功能介绍 一两周前在地铁上刷任 ...