SpringBoot后端代码基本逻辑

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

配置(application.yml)
server:
 port: 10086
​
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/wiki?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC&useSSL=true
    username: root
    password: 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 {
    @Resource
    private 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 {
​
    @Resource
    private 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();
    }
}

相关推荐

  1. SpringBoot代码基本逻辑

    2024-07-13 23:38:04       18 阅读
  2. springboot项目

    2024-07-13 23:38:04       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 23:38:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 23:38:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 23:38:04       57 阅读
  4. Python语言-面向对象

    2024-07-13 23:38:04       68 阅读

热门阅读

  1. 响应式编程-数据劫持

    2024-07-13 23:38:04       20 阅读
  2. Vue-生命周期勾子函数

    2024-07-13 23:38:04       16 阅读
  3. 计算机如何学习

    2024-07-13 23:38:04       15 阅读
  4. 要修改已经推送到远程仓库的提交信息

    2024-07-13 23:38:04       17 阅读
  5. linux 设置nginx开机自启

    2024-07-13 23:38:04       21 阅读
  6. c++贪心算法

    2024-07-13 23:38:04       18 阅读
  7. ArcGIS Pro SDK (八)地理数据库 4 查询

    2024-07-13 23:38:04       16 阅读
  8. 文本语言的上升沿写法

    2024-07-13 23:38:04       15 阅读
  9. Aop实现后端数据重复提交

    2024-07-13 23:38:04       23 阅读
  10. Android C++系列:Linux进程间关系

    2024-07-13 23:38:04       20 阅读
  11. thinkphp5多层with关联查询错误问题

    2024-07-13 23:38:04       25 阅读
  12. Understanding EtherCAT Device Serial Number Checking

    2024-07-13 23:38:04       19 阅读
  13. 1.1 Android启动概览

    2024-07-13 23:38:04       22 阅读
  14. HttpUtils工具类

    2024-07-13 23:38:04       18 阅读
  15. 风景区服务热线系统:智能化时代的旅游新选择

    2024-07-13 23:38:04       21 阅读