SpringBoot后端代码基本逻辑

2024-07-14 1761阅读

数据持久化(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


​
    
        
        
        
    
​
    
        select * from `demo`
    
写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 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 selectAllDateDemo(){
        List 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 queryAllDateDemo() {
        return DemoService.selectAllDateDemo();
    }
}
SpringBoot后端代码基本逻辑
(图片来源网络,侵删)
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]