SpringBoot3集成Mybatis入门教程
创作时间:
作者:
@小白创作中心
SpringBoot3集成Mybatis入门教程
引用
1
来源
1.
https://www.cnblogs.com/shidawuyu/p/18771337
本文详细介绍了如何在SpringBoot3项目中集成Mybatis,包括基本配置、XML映射、Mapper接口定义、Service实现以及增删改查等常见操作的实现。通过本文,读者可以快速掌握SpringBoot与Mybatis的整合方法,为开发基于SpringBoot的数据库应用提供参考。
Mybatis
Mybatis是一款优秀的持久层框架,支持自定义SQL,Mybatis可以通过简单的XML或注解来配置和映射原始类型、接口和Java对象为数据库中的记录
SpringBoot配置Mybatis
前提pom.xml中已经导入mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
#配置Mybatis实体和xml映射
mybatis:
#映射XML
mapper-locations: classpath:mapper/*.xml
configuration:
#配置日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#驼峰命名
map-underscore-to-camel-case: true
基本xml文件
<?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="">
</mapper>
xml与mapper下的接口一一对应关系
告诉Springboot如何扫描到mapper包路径(在springbootApplicantion中添加注解)
@MapperScan("com.example.springboot1.mapper")
创建service并且标注为Springboot里面的一个bean
package com.example.springboot1.service;
import org.springframework.stereotype.Service;
@Service
public class YongHuService {
}
mapper和Mapper.xml对应关系
接口路径(/yonghu/selectAll)
数据请求流程:前端浏览器->Springboot浏览器->Mybatis->Mysql->Java对象->Json对象->浏览器
通过注解查询单个用户
@Select("select * from yonghu where id=#{id}")
YongHu selectById(Integer id);
接口传参方式
@PathVariable
@RequestParam
传递多个参数
/**
* 查询单个
* @return
*/
@GetMapping("/selectOne")
public Result selectOne(@RequestParam Integer id,@RequestParam(required = false) String xingBie){
YongHu yongHu1=yongHuService.selectById(id);
return Result.success(yongHu1);
}
参数传递
分页查询,引入pagehelper插件
<!--分页插件pageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
在Service里面通过三行代码实现分页查询
public PageInfo<YongHu> selectPage(Integer pageNum,Integer pageSize){
PageHelper.startPage(pageNum,pageSize);
List<YongHu> list=yongHuMapper.selectAll();
return PageInfo.of(list);
}
分页接口
/**
* 分页查询
* @param pageNum 当前页码
* @param pageSize 每页个数
* @return
*/
@GetMapping("/selectPage")
public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize){
PageInfo<YongHu> pageInfo=yongHuService.selectPage(pageNum,pageSize);
return Result.success(pageInfo);
}
使用Mybatis实现增删改操作
GET:查询;POST:新增;PUT:修改;DELETE:删除; @RequstBody可以把前端传来的Json字符串映射成Java对象或者数组
Mybatis里面写sql使用下划线,涉及到绑定Java对象值就写驼峰
<insert id="add" parameterType="com.example.springboot1.entity.YongHu">
insert into yonghu (addtime,yonghuzhanghao,mima,yonghuxingming,xingbie,lianxifangshi,shenfenzheng,youxiang,touxiang)
values (#{addTime},#{yongHuZhangHao},#{miMa},#{yongHuXingMing},#{xingBie},#{lianXiFangShi},#{shenFenZheng},#{youXiang},#{touXiang})
</insert>
<update id="updateById" parameterType="com.example.springboot1.entity.YongHu">
update yonghu set addtime=#{addTime},yonghuzhanghao=#{yongHuZhangHao},mima=#{miMa},yonghuxingming=#{yongHuXingMing},xingbie=#{xingBie},
lianxifangshi=#{lianXiFangShi},shenfenzheng=#{shenFenZheng},youxiang=#{youXiang},touxiang=#{touXiang}
where id=#{id}
</update>
@Delete("delete from yonghu where id=#{id}")
void deleteById(Integer id);
/**
* 新增数据
* @param yongHu
* @return
*/
@PostMapping("/add")
public Result add(@RequestBody YongHu yongHu){
yongHuService.add(yongHu);
return Result.success();
}
/**
* 更新数据
* @param yongHu
* @return
*/
@PutMapping("/update")
public Result update(@RequestBody YongHu yongHu){
yongHuService.update(yongHu);
return Result.success();
}
/**
* 删除数据
* @param id
* @return
*/
@DeleteMapping("/deleteById/{id}")
public Result deleteById(@PathVariable Integer id){
yongHuService.deleteById(id);
return Result.success();
}
/**
* 查询所有
* @return
*/
@GetMapping("/selectAll")
public Result selectAll(){
List<YongHu> list=yongHuService.selectAll();
return Result.success(list);
}
Resouce.getResouceAsSteam("sqlMapConfig.xml")factory=new ...Builder.build(is)sqlSession =factory.openSession(true);
热门推荐
未成年人充值游戏,怎样申请退款
当年“风靡全网”的5部经典港剧,每一部都是天花板级别的神作
蜜蜂吃什么,工蜂和雄蜂吃花粉和花蜜
2024南京大学王牌专业排名 哪些专业比较好
了解症状,注意改变
干部如何能带领团队工作
js怎么解析lrc歌词文件格式
福建同心地标 | 古田临水宫:陈靖姑信俗文化从这里走向世界
逸青糠酸莫米松鼻喷雾剂:鼻子不通畅是鼻炎吗?
60个最容易读错的姓氏,最后一个也太难了
动作小巧,前臂发力,让乒乓球发球速度飙升
江川:“人才+”,助力乡村振兴“一路生花”
十大适合孕妇吃的鱼
你的减肥方法科学吗?对照《体重管理指导原则》测一测
皮肤痒的主要原因是什么
湖北省15个5A景区全攻略:从黄鹤楼到神农架,领略荆楚大地人文与自然之美
数字通信信号编码详解:原理与应用案例
家访“访学生” 更“访家长”
重温经典动画:记忆的回响,文化的传承
声光调制器(AOM):原理与应用详解
科技创新为经济发展注入强劲动能:临沂市临沭县的实践与探索
浑身是宝的“生命之树”——侧柏
2024年四季度及全年中国黄金市场回顾与趋势分析
体检前的那些事儿,您知道多少?
治愈系的语录59条
流感病毒也分“正”“副”?!看这篇就够了.....
福建福清南少林寺:南拳之源的历史与传承
努力的意义:不是为了鹤立鸡群,而是远离鸡群
黄山属于哪个地区哪个省(中国名山记录—安徽黄山)
惠州惠医保,了解哪些费用可以报销