当前位置: 首页 > news >正文

spring-boot 整合 mybatis

文章目录

  • Spring boot 整合Mybatis将数据返回到浏览器
    • 1、准备数据
    • 2. 创建一个 pojo 包,创建User实体类
    • 3. 创建一个mapper包,写一个UserMapper接口
    • 4. 创建一个service包,写一个UserService接口。
    • 5. 在 Service 包下创建一个子包,impl 包,然后 implements UserService 接口,同时将 UserService 接口和 userMapper 类的抽象方法 findById 实现。
    • 6. 创建 controller 包

Spring boot 整合Mybatis将数据返回到浏览器

1、准备数据

将数据放在本地的mysql数据库中

create database if not exists mybatis;use mybatis;create table user(id int unsigned primary key auto_increment comment 'ID',name varchar(100) comment '姓名',age tinyint unsigned comment '年龄',gender tinyint unsigned comment '性别, 1:男, 2:女',phone varchar(11) comment '手机号'
) comment '用户表';insert into user(id, name, age, gender, phone) VALUES (null,'白眉鹰王',55,'1','18800000000');
insert into user(id, name, age, gender, phone) VALUES (null,'金毛狮王',45,'1','18800000001');
insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002');
insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龙王',42,'2','18800000003');
insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004');
insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');

在这里插入图片描述

2. 创建一个 pojo 包,创建User实体类

里面四个属性,id,姓名,年龄,性别,手机号 跟数据库表里的字段一一对应。

package com.ithiema.springbootdemo2.pojo;public class User {private Integer id;private String name;private Short age;private Short gender;private String phone;public User() {}public User(Integer id, String name, Short age, Short gender, String phone) {this.id = id;this.name = name;this.age = age;this.gender = gender;this.phone = phone;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Short getAge() {return age;}public void setAge(Short age) {this.age = age;}public Short getGender() {return gender;}public void setGender(Short gender) {this.gender = gender;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender=" + gender +", phone='" + phone + '\'' +'}';}
}

3. 创建一个mapper包,写一个UserMapper接口

里面定义一个 findByid这个方法,传入一个 id 查询之后返回一个User对象。

package com.ithiema.springbootdemo2.mapper;import com.ithiema.springbootdemo2.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;/*** ClassName: UserMapper* Packge: com.ithiema.springbootdemo2.mapper* Description:** @Author: aex* @Create 2024/9/29 10:32* @Version 1.0*/
@Mapper
public interface UserMapper {@Select("select * from mybatis.user where id = #{id}")public User findById(Integer id);
}

4. 创建一个service包,写一个UserService接口。

跟刚刚Mapper 包里一样的方法。

package com.ithiema.springbootdemo2.service;import com.ithiema.springbootdemo2.pojo.User;/*** ClassName: UserService* Packge: com.ithiema.springbootdemo2.service* Description:** @Author: aex* @Create 2024/9/29 10:36* @Version 1.0*/
public interface UserService{public User findById(Integer id);
}

5. 在 Service 包下创建一个子包,impl 包,然后 implements UserService 接口,同时将 UserService 接口和 userMapper 类的抽象方法 findById 实现。

package com.ithiema.springbootdemo2.service.impl;import com.ithiema.springbootdemo2.mapper.UserMapper;
import com.ithiema.springbootdemo2.pojo.User;
import com.ithiema.springbootdemo2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** ClassName: UserServiceImpl* Packge: com.ithiema.springbootdemo2.service.impl* Description:** @Author: aex* @Create 2024/9/29 10:40* @Version 1.0*/
@Service
public class UserServiceImpl implements UserService {  // 这个类是service 的容器,@Autowiredprivate UserMapper userMapper;@Overridepublic User findById(Integer id) {return userMapper.findById(id);}
}

6. 创建 controller 包

创建 UserControllerpackage com.ithiema.springbootdemo2.controller;import com.ithiema.springbootdemo2.pojo.User;
import com.ithiema.springbootdemo2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** ClassName: UserController* Packge: com.ithiema.springbootdemo2.controller* Description:** @Author: aex* @Create 2024/9/29 10:52* @Version 1.0*/
@RestController
public class UserController {@Autowired //注入属性private UserService userService;@RequestMapping("/findById")public User findById(Integer id){return userService.findById(id);}}

http://www.mrgr.cn/news/38843.html

相关文章:

  • 9.29今日错题解析(软考)
  • 0108 Spring Boot启动过程
  • 2024年国庆节不能错过好物!旅行必备性价比高西圣充电宝!
  • FPGA IO延迟的约束与脚本
  • [kylin D2000]麒麟系统飞腾D2000下LVDS屏幕BIOS下能亮系统下不亮问题解决方法
  • Python __getattr__()函数的应用
  • 论文阅读(十一):CBAM: Convolutional Block Attention Module
  • 84、Python之鸭子类型:魔术方法自定义类型也可以实现加减乘除
  • 告别“军备竞赛”!L2进入下沉普及周期,谁在领跑本土方案市场?
  • UDS_4_传输存储的数据功能单元
  • Fiddler—使用保姆级教程
  • 苏州数字孪生工业互联网可视化技术,赋能新型工业化智能制造工厂
  • 串口UART的深入使用
  • YOLOv9改进策略 :IoU优化 | Unified-loU,用于高品质目标检测的统一loU ,2024年8月最新IoU
  • AI学习指南深度学习篇-批标准化的基本原理
  • DriveVLM 论文学习
  • STM32F103C8----3-3 蜂鸣器(跟着江科大学STM32)
  • 【CKA】CKA考题和注意事项
  • 减少重复的请求之promise缓存池(构造器版) —— 缓存promise,多次promise等待并返回第一个promise的结果
  • 浅谈Java之Redis缓存