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. Linux·命令收藏

    时间:2018-11-20 记录:byzqy 标题:Linux命令大全(手册) 地址:http://man.linuxde.net/ 标题:Linux script命令 -- 终端里的记录器 地址:h ...

  2. Tomcat集群Cluster实现原理

    1.Tomcat集群         Tomcat集群的问题之一是如何处理Session,Session是有状态的,请求到了Tomcat,后续流传是要根据上下文(Context)来进行的.我们可以改造 ...

  3. Linux 安装 Harbor 私有镜像仓库

    下载 最新发行:https://github.com/goharbor/harbor/releases # 下载文件 wget https://github.com/goharbor/harbor/r ...

  4. 记一次 .NET 某新能源汽车锂电池检测程序 UI挂死分析

    更多高质量干货:参见我的 GitHub: dotnetfly 一:背景 1. 讲故事 这世间事说来也奇怪,近两个月有三位朋友找到我,让我帮忙分析下他的程序hangon现象,这三个dump分别涉及: 医 ...

  5. MyBatis学习总结(三)——MyBatis配置文件配置的优化

    一.连接数据库的配置单独放在一个properties文件中 上文 连接数据库的配置写在 mybatisConf.xml中,本文直接放在 db.properties 中, 在mybatisConf.xm ...

  6. 性能测试必备命令(4)- pstree

    性能测试必备的 Linux 命令系列,可以看下面链接的文章哦 https://www.cnblogs.com/poloyy/category/1819490.html 介绍 显示进程树 语法格式 ps ...

  7. Servlet生命周期和方法

    一.五个生命周期方法,有三个很重要,初始化方法.提供服务方法和销毁方法 1.三个主要方法 2.另外两个重写的成员方法只做了解 二.生命周期详解 其中,每次刷新页面都是一次对servlet访问: 页面访 ...

  8. 依赖注入Bean属性——手动装配Bean

    一.构造方法注入 其中,可以根据不同的参数列表调用不同的重载的构造方法: 其中,基本数据类型没有包,引用类型都有包路径,基本类型对应封装类: 二.通过property标签调用类的set方法注入 三.通 ...

  9. 在linux查询本机的公网IP

    linux服务器查看公网IP信息的方法 最近在解决网络问题时,需要查看本机的出口公网IP信息,所以在网络上搜索和请求运维达人,获得如下两个方法: curl ifconfig.me 在linux系统中输 ...

  10. Ubuntu 引导修复

    Ubuntu 引导修复 前言 最近还在看 Docker 的教程,看到了"跨宿主机网络通信"的一节,于是想到去 Ubuntu 中 实践一番.结果发现 Ubuntu 进不去了.由于考虑 ...