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

SpringBoot后端代码基本逻辑

数据持久化(Dao---Entity---mapper)

配置(application.yml)
server:port: 10086
​
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/wiki?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC&useSSL=trueusername: rootpassword: jia******
​
mybatis:mapper-locations: classpath:/mapper/*.xml
写创建库表语句
drop table if exists `demo`;
create table `demo`
(`id`   bigint not null comment 'id',`name` varchar(50) comment '名称',`other_name` vachar(50) comment '代替名',primary key (`id`)
) engine = innodb default charset utf8mb4 comment ='测试';
​
insert into `demo` (id, name,other_name)VALUES (1, '测试', 'text');
写相应的实体
//使用lombok写实体---我是用的方式,build创建实体很方便
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
​
/*** @author Rui* @description demo实体类* @create 2024/7/5 16:58*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DemoEntity {private Integer id;private String name;private String otherName;
​
}
//使用getter,setter
/*** @author Rui* @description demo实体类* @create 2024/7/5 16:58*/
​
public class DemoEntity {private Integer id;private String name;private String otherName;
​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 String getOtherName() { return otherName; } public void setOtherName(String otherName) {  this.otherName = otherName; }
​public DemoEntity() { } public DemoEntity(Integer id, String name, String otherName) {this.id = id;this.name = name;this.otherName = otherName;}
}
​
写mapper
<?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= "com.jiawa.wiki.dao.DemoDao">
​<resultMap id ="dateMap" type="com.jiawa.wiki.domain.DemoEntity"><id column="id" property="id"/><result column="name" property="name"/><result column="password" property="password"/></resultMap>
​<select id="queryAllDemoDate" resultType="com.jiawa.wiki.domain.DemoEntity">select * from `demo`</select>
</mapper>
写dao接口
import com.jiawa.wiki.domain.DemoEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
​
/*** @author Rui* @description 提供给服务层service的接口* @create 2024/7/5 17:01*/
@Mapper
public interface DemoDao {List<DemoEntity> queryAllDemoDemo();
}

服务层对数据持久层的数据做处理

service
import com.jiawa.wiki.dao.DemoDao;
import com.jiawa.wiki.domain.DemoEntity;
import org.springframework.stereotype.Service;
​
import javax.annotation.Resource;
import java.util.List;
​
/*** @author Rui* @description 为controller层提供服务,对数据层的数据处理* @create 2024/7/5 17:19*/
@Service
public class DemoService {@Resourceprivate DemoDao DemoDao;
​public List<DemoEntity> selectAllDateDemo(){List<DemoEntity> DemoEntities = DemoDao.queryAllDemoDate();return DemoEntities;}
}
​

控制层接收服务层提供的数据,或者向服务层传递前端的数据

controller
import com.jiawa.wiki.domain.DemoEntity;
import com.jiawa.wiki.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
​
import javax.annotation.Resource;
import java.util.List;
​
/*** @author Rui * @description 接收服务层的数据,像服务层传递数据* @description return出去的数据,浏览器就可以接收到了,几乎所有格式* @create 2024/7/5 14:53*/
@Slf4j
@RestController
//注意是rest风格的controller
public class DemoController {
​@Resourceprivate DemoService DemoService;//get方法和post方法效果相同,但是post可以在url中不显示参数@RequestMapping(value = "/hello", method = RequestMethod.GET)public String Hello() {return "Hello world";}
​
//除了post、get还有delete很多方法    @RequestMapping(value = "/hello/post", method = RequestMethod.POST)public String HelloPost(String name) {return "Hello world " +name;}
​@RequestMapping(value = "/hello/queryAll", method = RequestMethod.GET)public List<DemoEntity> queryAllDateDemo() {return DemoService.selectAllDateDemo();}
}

http://www.lryc.cn/news/399398.html

相关文章:

  • Python学生信息管理系统的设计与实现
  • 最优雅的PHP框架 Laravel
  • log4j2的日志框架(详细,springboot和异步日志的实现)
  • taocms 3.0.1 本地文件泄露漏洞(CVE-2021-44983)
  • SpringBoot实战:处理全局异常
  • pdf只要前几页,pdf中只要前几页怎么处理
  • 实变函数精解【4】
  • 【BUG】Python3|COPY 指令合并 ts 文件为 mp4 文件时长不对(含三种可执行源代码和解决方法)
  • AI克隆声音,基于函数计算部署GPT-Sovits语音生成模型
  • DP讨论——建造者模式
  • 【JavaScript】解决 JavaScript 语言报错:Uncaught SyntaxError: Unexpected token
  • oracle数据库的plsql免安装版安装
  • stm32使用通用定时器生成pwm
  • 老物件线上3D回忆展拓宽了艺术作品的展示空间和时间-深圳华锐视点
  • 对于多个表多个字段进行查询、F12查看网页的返回数据帮助开发、数据库的各种查询方式(多对多、多表查询、子查询等)。
  • 护网HW面试常问——组件中间件框架漏洞(包含流量特征)
  • 招投标数据采集:为企业决策提供数据支持
  • 02:项目二:感应开关盖垃圾桶
  • eNsp公司管理的网络NAT策略搭建
  • MUR2060CTR-ASEMI无人机专用MUR2060CTR
  • Manim的代码练习02:在manim中Dot ,Arrow和NumberPlane对象的使用
  • datawhale - 基于术语词典干预的机器翻译挑战赛 (一)
  • 【JavaScript脚本宇宙】提升用户体验:探索 JavaScript 命令行界面开发工具
  • ubuntu18.04安装显卡驱动后无法进入桌面的解决办法
  • javaScript的面试重点--预解析
  • Gitea 仓库事件触发Jenkins远程构建
  • springboot+vue 开发记录(九)后端打包部署运行
  • 昇思25天学习打卡营第20天 | 基于MindNLP+MusicGen生成自己的个性化音乐
  • windows USB 设备驱动开发-USB主控制开发(一)
  • Dubbo 负载均衡(Load Balance)