SpringBoot 集成 Mybatis(三)
个人博客网: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(三)的更多相关文章
- SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源
SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
- SpringBoot集成MyBatis底层原理及简易实现
MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
- 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 ...
- BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析
重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...
- SpringBoot集成Mybatis配置动态数据源
很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...
随机推荐
- String、StringBuffer和StringBuilder比较
从执行速度效率来说: String为字符串常量,一旦创建就不能修改,第一次创建一个String对象,实例化后有初始值:当第二次创建String对象后,再次赋值,表面上看是改变了第一个String对象的 ...
- Mysql常用sql语句(九)- like 模糊查询
测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html 前言 ...
- [hdu5266]区间LCA
题意:给一棵树,求节点L,L+1,...R的最近公共祖先 思路:先对树dfs一下,从根1出发,经过每条边时记录一下终点和到达这个点的时间截,令r[u]表示到达u这个节点的最早时间截,t[x]表示在时间 ...
- 一文讲透Cluster API的前世、今生与未来
作者:Luke Addison 原文链接:https://blog.jetstack.io/blog/cluster-api-past-present-and-future/ Cluster API是 ...
- tp5分页数据
paginate分页完成之后,生成的分页数据是对象形式存在的,所以如果要调用其分页的数据,比如想要遍历修改数据中的值,等需要进行 foreach操作的地方,需要使用 $re=Db::->tabl ...
- 题解 P4296 【[AHOI2007]密码箱】
由题意有 \(x^2\equiv 1\;mod\;n\) 对题目的公式进行变形 \(x^2-1=k\times n\) \((x+1)(x-1)=k\times n\) 由唯一分解定理,我们构造\(a ...
- 快手4-5月Java岗面经
快手面试准备 我的牛客网帖子链接:https://www.nowcoder.com/discuss/429362 一面: 基础知识 1.java基本数据类型(8种) 1.基本数据类型有哪些,各占多少位 ...
- PART(Persistent Adaptive Radix Tree)的Java实现源码剖析
论文地址 Adaptive Radix Tree: https://db.in.tum.de/~leis/papers/ART.pdf Persistent Adaptive Radix Tree: ...
- SVN:TortoiseSVN SendRpt.exe not found 完美解决
今天来上班的时候,发现公司用的svn版本和我自己的不一致,其实可以使用但是还是神经质的更新了一下.结果每次右键都会出一个异常.下图所示. 期初以为是哪里配置错误,或者版本冲突,经过网上查找办法,完美解 ...
- Spring 依赖注入(DI)简介
依赖注入的英文表示为dependency injection,缩写为DI. Spring框架的核心功能之一就是通过依赖注入的方式来管理Bean之间的依赖关系. 当编写一个复杂的 Java 应用程序时, ...