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. 移动端动画——requestAnimationFrame

    window.requestAnimationFrame() 告诉浏览器--你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画.该方法需要传入一个回调函数作为参数,该回调函数会 ...

  2. 高德地图&兴趣点(poi)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. MySQL锁(表锁,行锁,共享锁,排它锁,间隙锁)使用详解

    锁,在现实生活中是为我们想要隐藏于外界所使用的一种工具.在计算机中,是协调多个进程或县城并发访问某一资源的一种机制.在数据库当中,除了传统的计算资源(CPU.RAM.I/O等等)的争用之外,数据也是一 ...

  4. GUI实现超简单的计算器

    计算器样式 实现代码 //实现超简易的计算器 public class Test02 { public static void main(String[] args) { Counter counte ...

  5. Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!

    在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...

  6. Python - //和/的区别

    / 表示浮点数除法,返回浮点结果; // 表示整数除法,返回不大于结果的一个最大的整数 print("6 // 4 = " + str(6 // 4)) print("6 ...

  7. 浅谈 JVM GC 的安全点与安全区域

    OopMap 前文我们说到,JVM 采用的可达性分析法有个缺点,就是从 GC Roots 找引用链耗时. 都说他耗时,他究竟耗时在哪里? GC 进行扫描时,需要查看每个位置存储的是不是引用类型,如果是 ...

  8. redis存取数据String

    一.连接不同数据库和存取String类型值 1.连接数据库 2.set和get多个 3.取值并赋值 取值返回的是赋值改变之前的值: 4.递增和递减 5.字符串尾部加值 6.商品编号自增应用

  9. linu命令进阶篇

    预备知识: 本实验要求实验者具备如下的相关知识. 前面我们学习了linux的文件系统,了解的文件系统的结构,也学了linux档案的属性和权限,以及其设定. 当我们执行命令操作一个文件的时候,却不知道这 ...

  10. python库--tensorflow

    方法 返回值类型 参数 说明 张量    .constant() Tensort 张量 实例t value 创建一个常量tensor dtype=None 输出类型 shape=None 返回tens ...