1、导入 MyBatis 所需要的依赖

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

2、配置数据库连接信息(延用不变,红色必要)  

spring:
datasource:
username: root
password: 123456
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding
=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver

type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

3、创建mapper目录以及对应的 Mapper 接口


@Mapper //ibatis的注解,也可以在启动类上用mapperScan代替
@Repository
public interface TestCatMapper {

List<TestCat> getAllCat();

TestCat getCatById( Integer catId );

int addCat( TestCat testCat );

int updateCat( TestCat testCat );

int deleteCat( Integer catId );

}

4、对应的Mapper映射文件,用XML就不能在方法上直接写SQL了

<?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="com.hwl.swgger01.mapper.TestCatMapper"> <select id="getAllCat" resultType="TestCat">
select * from test_cat;
</select> <select id="getCatById" resultType="TestCat" parameterType="int">
select * from test_cat where cat_id = #{catId};
</select> <insert id="addCat" parameterType="TestCat">
insert into test_cat(cat_name,cat_age,cat_color) value(#{catName},#{catAge},#{catColor});
</insert> <update id="updateCat" parameterType="TestCat">
update test_cat set cat_name = #{catName} where cat_id = #{catId};
</update> <delete id="delete" parameterType="int">
delete from test_cat where cat_id = #{catId};
</delete> </mapper>

5、编写Controller、service进行测试!

@RestController
@RequestMapping("/cat2")
public class TestCatController2 { @Autowired
TestCatService testCatService; @GetMapping("/getAllCat")
public List<TestCat> getAllCat(){
return testCatService.getAllCat();
} @GetMapping("/getCatById/{catId}")
public TestCat getCatById( @PathVariable("catId") Integer catId ){
return testCatService.getCatById( catId );
} @GetMapping("/add")
public int addCat(){
TestCat testCat = new TestCat("花花",2,"黑色");
return testCatService.addCat( testCat );
} @GetMapping("/update/{catId}")
public int updateCat( @PathVariable("catId") Integer catId ){
TestCat testCat = new TestCat("修改猫",catId);
return testCatService.updateCat( testCat );
} @GetMapping("/deleteCat/{catId}")
public int deleteCat( @PathVariable("catId") Integer catId ){
return testCatService.deleteCat( catId );
}
}

service:

@Service
public class TestCatService { @Autowired
TestCatMapper testCatMapper; public List<TestCat> getAllCat(){
return testCatMapper.getAllCat();
} public TestCat getCatById( Integer catId ){
return testCatMapper.getCatById( catId );
} public int addCat( TestCat testCat ){
return testCatMapper.addCat( testCat );
} public int updateCat( TestCat testCat ){
return testCatMapper.updateCat( testCat );
} public int deleteCat( Integer catId ){
return testCatMapper.deleteCat( catId );
}
}

6、启动项目访问进行测试!

SpringBoot整合Mabatis的更多相关文章

  1. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

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

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

  3. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  4. springboot整合mybaits注解开发

    springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...

  5. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  6. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  7. SpringBoot整合Kafka和Storm

    前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...

  8. SpringBoot整合SpringCloud搭建分布式应用

    什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...

  9. SpringBoot整合RabbitMQ-整合演示

    本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...

随机推荐

  1. Redis(二):基本数据类型

    基础 # redis默认有16个数据库,数组下标从0开始,默认使用0号库 # 当我们启动服务器并连接客户端之后: set <key> <value> # 向数据库中添加数据用于 ...

  2. linux centos7 增加操作日志记录

    2021-08-24 1. 需求产生原因 linux 系统中的日志存放在目录 /var/log/ 下,今天想看看我之前的操作记录,发现系统中的日志并不包括各个用户操作文件的记录,所以打算自己建一个. ...

  3. 如何在C#中打开和读取EXCEL文件

    这篇文章向您展示如何在C#Windows Forms Application中使用ExcelDataReader,ExcelDataReader.DataSet打开和读取Excel文件.创建一个新的W ...

  4. MYSQL order by 排序的一个小问题探究

    小问题发现: select * from `sql` where id=1 order by (select 1 union select 2) 正常返回结果 mysql> select * f ...

  5. vue中如何深度监听一个对象?

    大家都知道,Vue项目中对数据的监听,提供了一个很好的钩子watch,watch可以极其方便的监听我们常用数据类型值的变化,但通常当我们想监听一个对象中,某个属性值的变化时,很难达到我们预期的效果.那 ...

  6. Spring Boot 2.x 之 H2 数据库

    1. Spring Boot下H2数据库的常用配置项 # 指定数据库的类型 spring.datasource.platform=h2 # 数据库连接地址(文件模式) ## AUTO_SERVER=T ...

  7. 【OI】计算分子量 Molar mass UVa 1586 题解

    题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...

  8. 小学生都能读懂的网络协议之:WebSocket

    目录 简介 webSocket vs HTTP HTTP upgrade header websocket的优点 webScoket的应用 websocket的握手流程 WebSocket API 总 ...

  9. rationrose安装步骤

    Rational Rose是Rational公司出品的一种面向对象的统一建模语言的可视化建模工具.用于可视化建模和公司级水平软件应用的组件构造. 就像一个戏剧导演设计一个剧本一样,一个软件设计师使用R ...

  10. javascript 分时函数 分批次添加DOM节点 timeChunk

    创建1000个webqq的qq好友列表, 一个好友用一个节点来表示 * timeChunk var timeChunk = function(a, fn, sz, done) { var obj, t ...