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

springboot基础-demo

1.创建学生信息表

create table stu(id int unsigned primary key auto_increment comment 'ID',name varchar(100) comment '姓名',age tinyint unsigned comment '年龄',gender tinyint unsigned comment '性别, 1:男, 2:女',score double(5,2) comment '成绩',phone varchar(11) comment '手机号'
)comment '学生表';
ALTER TABLE stu CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
insert into stu(id, name, age, gender,score, phone) VALUES (null,'张三',18,'1',560,'18800000000');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'李四',19,'1',572,'18800000001');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'王五',20,'1',480,'18800000002');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'赵六',18,'2',621,'18800000003');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'黄七',17,'2',211,'18800000004');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'孙八',18,'1',492,'18800000005');

2.创建项目工程

在这里插入图片描述
在这里插入图片描述

2.appliction.yml

spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisusername: rootpassword: 1234

3.StuController

package com.itdanb.springboottest.controller;import com.itdanb.springboottest.pojo.Stu;
import com.itdanb.springboottest.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class StuController {@Autowiredprivate StuService stuService;@RequestMapping("/findById")public Stu findById(Integer id){return stuService.findById(id);}
}

Stumapper

package com.itdanb.springboottest.mapper;import com.itdanb.springboottest.pojo.Stu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;@Mapper
public interface StuMapper {@Select("select * from stu where id = #{id}")public Stu findById(Integer id);}

Stu

package com.itdanb.springboottest.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Stu {private Integer id;private String name;private Integer age;private Short gender;private String score;private String 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 Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Short getGender() {return gender;}public void setGender(Short gender) {this.gender = gender;}public String getScore() {return score;}public void setScore(String score) {this.score = score;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return "Stu{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender=" + gender +", score='" + score + '\'' +", phone='" + phone + '\'' +'}';}
}

StuService

package com.itdanb.springboottest.service;import com.itdanb.springboottest.pojo.Stu;public interface StuService {public Stu findById(Integer id);
}

StuServiceImpl

package com.itdanb.springboottest.service.impl;import com.itdanb.springboottest.pojo.Stu;
import com.itdanb.springboottest.mapper.StuMapper;
import com.itdanb.springboottest.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class StuServiceImpl implements StuService {@Autowiredprivate StuMapper stuMapper;@Overridepublic Stu findById(Integer id) {return  stuMapper.findById(id);}
}

在这里插入图片描述

http://www.dtcms.com/a/287355.html

相关文章:

  • Java中缓存的使用浅讲
  • Netty集群方案详解与实战(Zookeeper + Redis + RabbitMQ)
  • 深入理解设计模式:策略模式的艺术与实践
  • 云端成本治理利器:亚马逊云科技智能仪表盘(AWS Cost Intelligence Dashboard)深度解析
  • Android14 SystemUI 启动流程(2)
  • Spring MVC @RequestParam注解全解析
  • Spring MVC源码分析 DispatcherServlet#getHandlerAdapter方法
  • C# 中的强大运算符
  • 掌握配置文件(一):精通`properties`与`yml`的语法及选择
  • 【iOS】ZARA仿写
  • MySQL详解二
  • ros2高级篇之高可用启动文件及配置编写
  • 深入解析HDFS写入流程:管道机制与数据可靠性保障
  • (Python)类和类的方法(基础教程介绍)(Python基础教程)
  • 7月19日日记
  • SpringAI_Chat模型_DeepSeek模型--基础对话
  • Word快速文本对齐程序开发经验:从需求分析到实现部署
  • 嵌入式单片机开发 - Keil MDK 复制工程
  • Python day18
  • MySQL事务管理(上)(12)
  • xss-labs靶场1-8
  • DAMA数据管理具像化解读|第一章数据管理|业务驱动因素
  • 暑期训练8
  • 13.4 Meta LLaMA开源模型家族全面解析:从Alpaca到Vicuna的技术内幕
  • 外观设计模式
  • 删除debian xdm自启动ibus的配置项
  • 2021 RoboCom 世界机器人开发者大赛-本科组(初赛)解题报告 | 珂学家
  • c语言-数据结构-如何用分冶法求得二叉树的节点数与高度?
  • CSS篇——第一章 六十五项关键技能(上篇)
  • Node.js特训专栏-实战进阶:17.会话管理与安全存储