1、添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

2、application配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=xxxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

关于更多更详细的配置请参考:https://blog.csdn.net/ajlzhu/article/details/81009845

3、新建包类

新建controller、dao、domain、service包,新建TestController、StudentDao、Student、StudentService类;

创建后的目录结构(多余的包和类与本文内容无关,不用创建):

4、创建student表

CREATE TABLE `student` (
   `sno` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `sname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
   `sage` bigint(20) DEFAULT NULL,
   `sex` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
   PRIMARY KEY (`sno`)
 ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

5、student类

public class Student {
private Long sno;
private String sname;
private int sage;
private String sex; public Long getSno() {
return sno;
} public void setSno(Long sno) {
this.sno = sno;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public int getSage() {
return sage;
} public void setSage(int sage) {
this.sage = sage;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}

6、StudentDao接口

@Mapper
public interface StudentDao { @Select("SELECT * FROM student where sno=#{id}")
public Student getById(@Param("id")int id); }

7、StudentService类

@Service
public class StudentService { @Autowired
StudentDao studentDao; public Student getById(int id){
return studentDao.getById(id);
}
}

8、控制器

@RestController
public class Test { @Autowired
StudentService studentService; @GetMapping("/demo")
public String student(){
Student student = studentService.getById(1);
return student.getSname();
} }

9、测试

数据库数据:

浏览器访问:

到这里就完全整合完成了,甚至都不需要进行任何多余的配置。

-----------------------------------------------------------------------------------

找到一篇讲的很好的文章:https://www.cnblogs.com/ityouknow/p/6037431.html

【mybatis】-- springboot整合mybatis的更多相关文章

  1. MyBatis原理,Spring、SpringBoot整合MyBatis

    1. MyBatis概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  2. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  3. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  4. SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]

    SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...

  5. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  6. springboot整合mybatis出现的一些问题

    springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...

  7. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

  8. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  9. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

  10. SpringBoot整合Mybatis完整详细版

    记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架,立马感叹:SpringBoot是世界上最好的框架.哈哈! 当初跟着教程练习搭建了一个框架,传送门:spring boot + ...

随机推荐

  1. vsftp配置文件详解

    匿名用户相关参数 anonymous_enable=YES # 是否允许匿名用户登陆 no_anon_password=NO # 是否忽略对匿名用户的密码检测 anon_root # 匿名登陆后尝试更 ...

  2. java java.net.URLConnection 实现http get,post

    抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接.此类的实例可用于读取和写入此 URL 引用的资源.通常,创建一个到 URL 的连接需要几个步骤: open ...

  3. django系列7:修改404页面展示,优化模板,降低urlconf和模板之间的耦合,命名app将模板和app绑定

    为了增加程序的友好和健壮性,修改view代码,处理以下如果出现404,页面的UI展示. 修改view代码 from django.http import Http404 from django.sho ...

  4. 微服务下的容器部署和管理平台Rancher

    Rancher是什么 Rancher是一个开源的企业级容器管理平台.通过Rancher,企业再也不必自己使用一系列的开源软件去从头搭建容器服务平台.Rancher提供了在生产环境中使用的管理Docke ...

  5. SQL server 统计数据库表数量和列出所有表名称

    统计表数量 SELECT count(*) FROM sys.objects WHERE type='U' 列出表名称  SELECT NAME  FROM sys.objects WHERE typ ...

  6. SEO总结

    学习了这么久了seo,对学过的知识进行一下总结. 服务器和域名 首先的一个就是对服务器和域名的选择,服务器最好能是独立的,因为如果不是独立服务器很容易被别人放在服务器上的网站所影响,然后就是域名,需要 ...

  7. JavaScript 正则表达式基础语法

    前言 正则表达式在人们的印象中可能是一堆无法理解的字符,但就是这些符号却实现了字符串的高效操作.通常的情况是,问题本身并不复杂,但没有正则表达式就成了大问题.javascript中的正则表达式作为相当 ...

  8. [物理学与PDEs]第3章习题3电磁场的矢势在 Lorentz 规范下满足的方程

    设 $\phi$ 及 ${\bf A}$ 分别为电磁场的标势及矢势 (见第一章 $\S$ 6). 试证明: 若 $\phi$ 及 ${\bf A}$ 满足条件 $$\bex \phi+\cfrac{1 ...

  9. Codeforces 1096F(dp + 树状数组)

    题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...

  10. java和数据库中日期类型的常见用法

    (1)java中日期类型:Date.Timestamp(2)数据库中:Date.Timestamp(3)字符串和Date之间的格式化转换:    SimpleDateFormat类方法: format ...