问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

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);

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号