SpringBoot整合Mabatis
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的更多相关文章
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
- SpringBoot整合RabbitMQ-整合演示
本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...
随机推荐
- Mysql 日期格式化 复杂日期区间查询
前言 最近在做项目涉及到Mysql的复杂日期查询,日期查询其实在数据库中查询其实还是用的挺多的,比如查询开始日期到结束日期的区间信息,查询日期小于有效日期的信息,查询当天的日期,明天的日期,做比较等. ...
- NX二次开发-使用NXOPEN C++向导模板做二次开发
版本 NX9+VS2012 1.怎么往VS软件里添加VC,C#,VB向导模板 先到NX安装目录下UGOPEN文件夹里找到这三个文件夹 拷贝到VS的安装目录下 这里有几个注意事项,VS2017,VS20 ...
- Spring笔记(1)
Spring快速入门 开发步骤 导入坐标 <dependency> <groupId>org.springframework</groupId> <artif ...
- 本地yum源搭建
2021/07/15 1.挂载 # 创建挂载目录 mkdir /mnt/cdrom # 挂载 mount -t iso9660 /dev/cdrom /mnt/cdrom 2.修改 yum 源配置# ...
- 解决方案-问题001:物理机、虚机等等Linux操作系统/usr/bin目录权限误操作,导致无法切换root
导语:平常运维人员会误操作一些目录权限,导致一些问题,那么如何恢复呢? 问题:物理机.虚机等等Linux操作系统/usr/bin目录权限误操作,导致无法切换root? 实验环境: ip地址 是否目录正 ...
- 【流程】Flowable流程定义总结
背景 近几年,互联网企业从消费互联网向产业互联网转型.在消费互联网时期,企业面对的时C端消费者,而产业互联网面对的是B端用户. 产业互联网涉及方方面面,企业信息化的建设就是B端用户的业务之一,在企业就 ...
- Java字符串常量池及字符串判等解析
一.理解"=="的含义 "=="常用于两个对象的判等操作,在Java中,"=="主要有以下两种用法: 1.基础数据类型:比较的是他们的值是否 ...
- Prometheus 2.21.0 新特性
Prometheus 2.21.0 现在(2020.09.11)已经发布,在上个月的 2.20.0 之后又进行了很多的修复和改进. 这个版本使用了 Go 1.15 进行编译,不赞成在TLS证书验证中使 ...
- Sentry 监控 - Dashboards 事件数据可视化大屏
系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...
- Elasticsearch -head 查询报 406错误码
问题:利用Elasticsearch -head插件不能查看数据或者在Elasticsearch -linux的curl命令操作时总是提示: {"error":"Cont ...