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.xdnf.cn/news/1550029.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

如何制作Linux系统盘

文章目录 引言系统盘制作准备工作开始烧录 引言 有时候不想使用虚拟机来运行 linux 或者想要装个双系统,这时候就需要物理安装 linux 了。 其实跟装 windows 是一模一样的。 先烧录 u 盘,然后电脑插上 u 盘,重启电脑时换个启动盘&#xff…

吐血整理:国内一站式儿童有声绘本创作平台

儿童绘本儿童故事这个领域在Stable Diffusion、Midjourney、ChatGPT产品推出后开始有大量自媒体达人纷纷发布教程,热度一直都在。但由于创作门槛较高、需要对AI类和制作类软件都需要掌握、流程制作复杂,且由于创作者提示词的影响出图效果不稳定&#xff…

四气两尘监测站中空气质量传感器推荐

在快速发展的工业化进程中,空气质量已成为衡量一个地区环境健康水平的重要指标。随着公众环保意识的增强,对空气质量的关注不再局限于直观的蓝天白云,而是深入到更为细微、复杂的污染物层面,其中,“四气两尘”便是这一…

在matlab中Application Compiler后的软件无法打开

🏆本文收录于《全栈Bug调优(实战版)》专栏,主要记录项目实战过程中所遇到的Bug或因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&am…

jenkins 构建报错ERROR: Error fetching remote repo ‘origin‘

问题描述 修改项目的仓库地址后,使用jenkins构建报错 Running as SYSTEM Building in workspace /var/jenkins_home/workspace/【测试】客户端/client-fonchain-main The recommended git tool is: NONE using credential 680a5841-cfa5-4d8a-bb38-977f796c26dd&g…

基于SSM的学校运动会信息管理系统【附源码】

基于SSM的学校运动会信息管理系统(源码L文说明文档) 目录 4 系统设计 4.1 系统概述 4.2 系统结构设计 4.3 数据库设计 4.3.1数据库E-R图设计 4.3.2数据库表设计 5.1 用户功能模块的实现 5.1.1用户注册界面 5.1.2用户登录界…

PID控制算法(六)

#include <stdio.h> #include <stdlib.h>// 定义PID结构体 typedef struct {float SetSpeed;float ActualSpeed;float err;float integral;float vo_out; //控制器输出float err_last;float Kp;float Ki;float Kd;float limit_min; // 输出限制最小值flo…

2024最新版网络安全图成长路线图,从零基础到精通_网络安全防御技术发展路线图

学习网络安全一定要培养兴趣&#xff0c;兴趣是最好的老师 刚开始抱着去尝试的心态。先坚持看完一套基础视频&#xff0c;或者看几本入门书。了解信息安全到底是干嘛的。 带着尝试的心态去干靶场。 不断去练习&#xff0c;不管是打靶场还是去挖漏洞&#xff0c;遇到问题去请教…

使用Flask和Python开发自己的API

点击下方卡片&#xff0c;关注“小白玩转Python”公众号 本文章将一步一步教你如何使用Flask和Python从头开始设置自己的API。教程结束时&#xff0c;你将能够从头开始设置你自己的API。我将一步一步指导你&#xff0c;使你能够基于Flask和Python开发你自己的API。 什么是API&a…

Linux 应用层自定义协议与序列化

文章目录 一、应用层1、协议2、序列化 && 反序列化3、通过Json库进行数据的序列化 && 反序列化Json::Value类Json::Reader类Json::Writer类 二、为什么read、write、recv、send和Tcp支持全双工&#xff1f;发数据的本质&#xff1a;tcp支持全双工通信的原因&am…

java计算机毕设课设—超级玛丽游戏(附源码、文章、相关截图、部署视频)

这是什么系统&#xff1f; 资源获取方式在最下方 java计算机毕设课设—超级玛丽游戏(附源码、文章、相关截图、部署视频) 超级玛丽游戏是一款经典的平台游戏&#xff0c;自1985年推出以来&#xff0c;已成为全球玩家心目中的经典之作。玩家操控玛丽奥在多样化的关卡中进行冒…

【综合性渗透利器】- TscanPlus

如果你在寻找一款轻量级、实用且开源的漏洞扫描工具&#xff0c;那么 TscanPlus 绝对值得一试。这款工具由 TideSec 团队打造&#xff0c;以其简洁、高效、易用的特点&#xff0c;广受好评&#xff0c;目前在github上拥有1.5k star。 为什么推荐 TscanPlus&#xff1f; 无论你…

利用ChatGPT实现的生成式人工智能自动化控制系统

一、引言 随着信息化与智能化时代的到来&#xff0c;人工智能&#xff08;AI&#xff09;技术迅猛发展&#xff0c;正在深刻地重塑各行业的运营模式。在这一背景下&#xff0c;生成式人工智能&#xff08;Generative AI&#xff09;以其卓越的创造力和广泛的应用潜力&#xff…

一种多版本、多人并行开发GIT分支管理规范

首发公众号&#xff1a; 赵侠客 引言 作为开发者每天在写代码的同时也在写BUG&#xff0c;所以一方面需要开发新的需求&#xff0c;另一方面还要填自己以前挖的坑。目前主流程序员都在使用GIT来管理自己的代码&#xff0c;当GIT仓库有多人维护或者项目有多个版本同时迭代开发时…

Axios使用cancel token取消请求

在一个vue项目开发的过程中&#xff0c;遇到一个需要中断文件上传的需求&#xff0c;当我利用axios的cancel token实现中断请求的功能之后&#xff0c;想要再次发送post请求&#xff0c;却发现axios直接返回了reject。 问题复现 当我执行upload方法时&#xff0c;文件能够正常…

PC端微信小程序如何调试?

向往常一样运行开微信小程序开发者工具 如果只弹出pc端小程序&#xff0c;没有出现调试的界面&#xff1a;点击胶囊按钮的三个…选择重新进入小程序 即可依次展开相应的功能调试&#xff0c;改完代码没反应再刷新看看&#xff0c;再没反应就再次重新点击编译并自动调试。

中国AIGC最值得关注企业产品榜单揭晓!首份应用全景图谱发布

“你好&#xff0c;新应用&#xff01;” 站在大模型落地元年&#xff0c;是时候喊出这句话了。 从软件APP、智能终端乃至具身智能等等&#xff0c;AIGC开始席卷一切。 大模型玩家、互联网巨头、终端厂商、垂直场景玩家纷纷入场&#xff0c;办公、创作、营销、教育、医疗领域…

电瓶车常见电压数据 48v/60v/72v 说明

常见电压数据 48v/60v/72v的区别 48v 基本属于电动自行车&#xff0c;适合10公里内的骑行&#xff0c;速度慢&#xff0c;25公里/时&#xff0c;适合老年人60v 电轻摩&#xff0c;不能带人&#xff0c;适合远距离出行72v 适合外卖小哥 电压和电机功率 48v 电动车通常配备400…

A: 数数(牛客练习赛129)

题目链接: A-数数_牛客练习赛129 (nowcoder.com) 题目描述: 样例输入: 5 样例输出&#xff1a; 0 思路分析&#xff1a; 直接求偶数是困难的&#xff0c;之前好像听过&#xff1a;任何一个大于1的自然数N&#xff0c;都可以唯一分解成有限个质数的乘积。那么就是间接的去做&a…

STM32F103C8----3-2 LED流水灯(跟着江科大学STM32)

一&#xff0c;电路图&#xff08;接线图&#xff09; 面包板的的使用请参考&#xff1a;《面包板的使用_面包板的详细使用方法-CSDN博客》 二&#xff0c;目的/效果 三&#xff0c;创建Keil项目 详细参考&#xff1a;《STM32F103C8----2-1 Keil5搭建STM32项目模版&#xff…