SpringBoot后端代码基本逻辑

07-14 1758阅读

数据持久化(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购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]