• SpringBoot后端代码基本逻辑


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

    配置(application.yml)
    1. server:
    2. port: 10086
    3. spring:
    4. datasource:
    5.   driver-class-name: com.mysql.cj.jdbc.Driver
    6.   url: jdbc:mysql://127.0.0.1:3306/wiki?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC&useSSL=true
    7.   username: root
    8.   password: jia******
    9. mybatis:
    10. mapper-locations: classpath:/mapper/*.xml
    写创建库表语句
    1. drop table if exists `demo`;
    2. create table `demo`
    3. (
    4.    `id`   bigint not null comment 'id',
    5.    `name` varchar(50) comment '名称',
    6.    `other_name` vachar(50) comment '代替名',
    7.    primary key (`id`)
    8. ) engine = innodb default charset utf8mb4 comment ='测试';
    9. insert into `demo` (id, name,other_name)VALUES (1, '测试', 'text');
    写相应的实体
    1. //使用lombok写实体---我是用的方式,build创建实体很方便
    2. import lombok.AllArgsConstructor;
    3. import lombok.Builder;
    4. import lombok.Data;
    5. import lombok.NoArgsConstructor;
    6. /**
    7. * @author Rui
    8. * @description demo实体类
    9. * @create 2024/7/5 16:58
    10. */
    11. @Data
    12. @Builder
    13. @AllArgsConstructor
    14. @NoArgsConstructor
    15. public class DemoEntity {
    16.    private Integer id;
    17.    private String name;
    18.    private String otherName;
    19. }
    1. //使用getter,setter
    2. /**
    3. * @author Rui
    4. * @description demo实体类
    5. * @create 2024/7/5 16:58
    6. */
    7. public class DemoEntity {
    8.    private Integer id;
    9.    private String name;
    10.    private String otherName;
    11.    public Integer getId() { return id; }
    12.    public void setId(Integer id) { this.id = id; }
    13.    
    14.    public String getName() { return name; }
    15.    public void setName(String name) { this.name = name; }
    16.    public String getOtherName() { return otherName; }
    17.    public void setOtherName(String otherName) {  this.otherName = otherName; }
    18.    public DemoEntity() { }
    19.    public DemoEntity(Integer id, String name, String otherName) {
    20.        this.id = id;
    21.        this.name = name;
    22.        this.otherName = otherName;
    23.   }
    24. }
    写mapper
    1. "1.0" encoding="UTF-8"?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace= "com.jiawa.wiki.dao.DemoDao">
    4.    <resultMap id ="dateMap" type="com.jiawa.wiki.domain.DemoEntity">
    5.        <id column="id" property="id"/>
    6.        <result column="name" property="name"/>
    7.        <result column="password" property="password"/>
    8.    resultMap>
    9.    <select id="queryAllDemoDate" resultType="com.jiawa.wiki.domain.DemoEntity">
    10.       select * from `demo`
    11.    select>
    12. mapper>
    写dao接口
    1. import com.jiawa.wiki.domain.DemoEntity;
    2. import org.apache.ibatis.annotations.Mapper;
    3. import java.util.List;
    4. /**
    5. * @author Rui
    6. * @description 提供给服务层service的接口
    7. * @create 2024/7/5 17:01
    8. */
    9. @Mapper
    10. public interface DemoDao {
    11.    List queryAllDemoDemo();
    12. }

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

    service
    1. import com.jiawa.wiki.dao.DemoDao;
    2. import com.jiawa.wiki.domain.DemoEntity;
    3. import org.springframework.stereotype.Service;
    4. import javax.annotation.Resource;
    5. import java.util.List;
    6. /**
    7. * @author Rui
    8. * @description 为controller层提供服务,对数据层的数据处理
    9. * @create 2024/7/5 17:19
    10. */
    11. @Service
    12. public class DemoService {
    13.    @Resource
    14.    private DemoDao DemoDao;
    15.    public List selectAllDateDemo(){
    16.        List DemoEntities = DemoDao.queryAllDemoDate();
    17.        return DemoEntities;
    18.   }
    19. }

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

    controller
    1. import com.jiawa.wiki.domain.DemoEntity;
    2. import com.jiawa.wiki.service.DemoService;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RequestMethod;
    6. import org.springframework.web.bind.annotation.RestController;
    7. import javax.annotation.Resource;
    8. import java.util.List;
    9. /**
    10. * @author Rui
    11. * @description 接收服务层的数据,像服务层传递数据
    12. * @description return出去的数据,浏览器就可以接收到了,几乎所有格式
    13. * @create 2024/7/5 14:53
    14. */
    15. @Slf4j
    16. @RestController
    17. //注意是rest风格的controller
    18. public class DemoController {
    19.    @Resource
    20.    private DemoService DemoService;
    21.    
    22. //get方法和post方法效果相同,但是post可以在url中不显示参数
    23.    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    24.    public String Hello() {
    25.        return "Hello world";
    26.   }
    27. //除了post、get还有delete很多方法    
    28.    @RequestMapping(value = "/hello/post", method = RequestMethod.POST)
    29.    public String HelloPost(String name) {
    30.        return "Hello world " +name;
    31.   }
    32.    @RequestMapping(value = "/hello/queryAll", method = RequestMethod.GET)
    33.    public List queryAllDateDemo() {
    34.        return DemoService.selectAllDateDemo();
    35.   }
    36. }

  • 相关阅读:
    Win10配置Maven环境
    设计模式-1.概述/UML类图/软件设计原则
    什么品牌洗地机性价比高?四大出色的王牌机型力荐
    上海亚商投顾:沪指冲高回落 纺织服装股午后集体走强
    UE4 C++:TMap容器
    嵌入式学习之Linux驱动(第九期_设备模型_教程更新了)_基于RK3568
    配置Kubelet的垃圾回收(K8S镜像回收)
    【无标题】
    特征工程完整指南 - 第二部分
    视频答题猜歌闯关娱乐微信小程序源码支持看视频答题闯关听歌猜歌答题流量主模式(团队奖励等)
  • 原文地址:https://blog.csdn.net/m0_66691386/article/details/140408346