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. HCNP Routing&Switching之OSPF特殊区域

    前文我们了解了OSPF LSA更新规则以及路由汇总相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15231880.html:今天我们来聊一聊OSPF的 ...

  2. Spring事物入门简介及AOP陷阱分析

    转载请注明出处: https://www.cnblogs.com/qnlcy/p/15237377.html 一.事务的定义 事务(Transaction),是指访问并可能更新数据库中各种数据项的一个 ...

  3. SQL语句之高级使用

    1.select top select top  用于规定要返回的数据的数目 注意:并非所有的数据库系统都支持 SELECT TOP 语句. MySQL 支持 LIMIT 语句来选取指定的条数数据, ...

  4. Onenote实现OCR识别图片

    OCR识别推荐两个软件: 1.       Tesseract:一个开源的,由谷歌维护的OCR软件. 2.       Onenote:微软Office附带或者可以自己独立安装. 3.       O ...

  5. Linux内核编译配置脚本

    环境 宿主机平台:Ubuntu 16.04.6 目标机:iMX6ULL Linux内核编译配置脚本 在linux开发过程中熟练使用脚本可以大大简化命令行操作,同时对于需要经常重复操作的指令也是一种备忘 ...

  6. Python - //和/的区别

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

  7. Python 高级特性(3)- 列表生成式

    range() 函数 日常工作中,range() 应该非常熟悉了,它可以生成一个迭代对象,然后可以使用 list() 将它转成一个 list # 判断是不是迭代对象 print(isinstance( ...

  8. Mysql常用sql语句(15)- cross join 交叉连接

    测试必备的Mysql常用sql语句 https://www.cnblogs.com/poloyy/category/1683347.html 前言 交叉连接就是求多表之间的笛卡尔积 讲道理..这个我都 ...

  9. JNDI注入基础

    JNDI注入基础 一.简介 JNDI(The Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API,命名服务 ...

  10. 剑指offer计划16( 排序简单)---java

    1.1.题目1 剑指 Offer 45. 把数组排成最小的数 1.2.解法 这题看的题解,发现自己思路错了. 这里直接拿大佬的题解来讲吧. 一开始这里就把创一个string的数组来存int数组 Str ...