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);
热门推荐
Lua表合并技巧全解析
Lua游戏开发:元表实现高效table合并
全球首个胰岛素周制剂在华上市,CGM成血糖监测新趋势
研究发现:狗狗抬腿撒尿藏着社交密码
改色膜风靡,车管所教你合法变装
车身改色法规详解:合法改装指南
明星豪车改色引热议,新规下如何合法变装?
广东交警发布最新提醒:车身改色这些事一定要知道!
“南无阿弥陀佛”读作“námó”更准确,专家解析背后原因
念佛助情绪管理,哈佛研究证实有效
净土宗的核心:从玄奘取经到善导大师的“南无阿弥陀佛”
1.73亿人患精神疾病,专家建议:念佛可助心灵疗愈
“南无阿弥陀佛”:佛教修行与中华文化的重要符号
牛皮头层皮革市场供需大揭秘:全球格局与中国机遇
粉丝文化:当代年轻人的社交革命与情感寄托
粉丝身份认同的秘密:心理学揭秘
儿童青少年超重率近两成,16部门联手推进“体重管理年”
每天12种食物,多样化饮食助力健康生活
租房防坑指南:从看房签约到维权举报全流程解析
中原大战:蒋介石、阎锡山、冯玉祥的权力博弈
稻城亚丁美食地图:三道特色藏餐和最佳用餐地点推荐
美国加州山火致5死,应急体系缺陷暴露
洛杉矶山火致7死1100栋房屋被毁,拜登取消访意行程应对灾情
洛杉矶冬季山火致3万民众撤离,专家警告极端气候或致灾害加剧
学好高等数学三大核心课程,这些基础知识必不可少
数字技术重塑舞台艺术,线上线下融合成新趋势
演艺新空间:当戏剧遇见生活场景,艺术创新正当时
中山医院国际部:高端医疗的新宠儿?
冷空气来袭,取暖全靠抖?这份取暖“神器”选购和使用指南,请查收→
告别冬日寒冷,房屋保暖实用技巧看过来!