个人博客网:https://wushaopei.github.io/    (你想要这里多有)

1.增加持久化层

		<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

2.Mapper相关

实体类

public class Emp {

	private Integer empId;
private String empName;
private Integer empAge;

数据库表

CREATE TABLE `table_emp` (
`emp_id`  int NOT NULL AUTO_INCREMENT ,
`emp_name`  varchar(100) NULL ,
`emp_age`  int NULL ,
PRIMARY KEY (`emp_id`)
)

Mapper配置文件

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.springboot.mappers.EmpMapper">
<select id="selectAll" resultType="com.atguigu.springboot.bean.Emp">
select emp_id empId, emp_name empName, emp_age empAge
from table_emp
</select>
</mapper>

Mapper接口

public interface EmpMapper {

	List<Emp> selectAll();

}

Service接口

@Transactional
public interface EmpService { List<Emp> getAll(); }

Service 接口实现

@Service
public class EmpServiceImpl implements EmpService { @Autowired
private EmpMapper empMapper; @Override
public List<Emp> getAll() {
return empMapper.selectAll();
} }

Handler调用

@Autowired
private EmpService empService; @ResponseBody
@RequestMapping("/getAll")
public List<Emp> getAll() {
return empService.getAll();
}

3.增加application.yml配置

spring:
datasource:
name: mydb
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/sb_db
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath*:/mybatis/*Mapper.xml

4.在主启动类上使用注解扫描Mapper

@MapperScan("com.webcode.springboot.mappers")

SpringBoot 集成 Mybatis(三)的更多相关文章

  1. SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源

    SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...

  2. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  3. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

  4. SpringBoot集成MyBatis底层原理及简易实现

    MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...

  5. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  6. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  7. SpringBoot 集成Mybatis 连接Mysql数据库

    记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...

  8. Springboot集成mybatis(mysql),mail,mongodb,cassandra,scheduler,redis,kafka,shiro,websocket

    https://blog.csdn.net/a123demi/article/details/78234023  : Springboot集成mybatis(mysql),mail,mongodb,c ...

  9. BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析

    重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...

  10. SpringBoot集成Mybatis配置动态数据源

    很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...

随机推荐

  1. Qt之分模块log

    说明 对于一般的log,使用 qInstallMessageHandler 重定向到文件即可,甚至可以根据日志等级,分类存储.但是并不是适用所有情况,比如,程序运行时动态创建模块,而每个模块需要创建不 ...

  2. 学习python的第一天,python的简单知识

    python 是现如今比较火的一种编程语言.在抱着试试的态度我来进行学习下python.要学习python 要先进行环境的安装. 下面是下载链接:https://pan.baidu.com/s/1PW ...

  3. 小心了!Kubernetes自动化操作工具将让你失去工作

    运行Kubernetes的人已经花费太多时间在操作上,企业正在考虑为Kubernetes编写自动化工具. 尽管IT部门的大部分职位都会增加,但职业顾问说,计算机操作员预计会减少.这个角色涉及运行She ...

  4. transform-translate3d

    translate3d 开启硬件加速,做动效效率比 position 定位置后,改变位置,效果好,比如下拉背景放大效果,上滑的时候背景跟着上滑,可以用 translate3d.亲测效果更好,记下来

  5. PHP对象基础

    class demo1 { public function test1(){ echo '这是一个公有方法,可以随意调用!' } protected function test2(){ $this-& ...

  6. Spring处理@Configuration的分析

    Spring处理@Configuration的分析 声明:本文若有任何纰漏.错误,还请不吝指出! 序言 @Configuration注解在SpringBoot中作用很大,且不说SpringBoot中的 ...

  7. CSS学习—day1

    摘要:web前端设计三剑客分为是html.CSS.Javascript,前面我们已经对html基础知识做了介绍,它定义了页面基本组成,而CSS则控制网页的样式和布局. 首先,明确一个问题,什么是CSS ...

  8. linux 被入侵后扫尾工作

    比较恶心,安装了后门,还把ssh sshd 给改了,服务器在机房,还不大好处理,如果贸然上去改,还容易把自己关在外面. 但是我有salt ,上面可以用cmd.run运行一切命令.不走sshd服务. l ...

  9. 「雕爷学编程」Arduino动手做(28)——RGB全彩LED模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

  10. 最短Hamilton路径 数位dp

    最短Hamilton路径 #include<bits/stdc++.h> using namespace std; ; <<maxn][maxn]; int maps[maxn ...