【Spring Boot】service 业务层简单使用

  • 在controller同级目录下创建service

    package com.spring2.service;

    import com.spring2.entity.User;
    import com.spring2.mapper.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    @Service
    public class UserService {

    复制代码
      @Autowired
      private UserMapper userMapper;
    
      public int save(User user){
          if(user.getId() == null){
              //有id 新增
             return userMapper.insert(user);
          }else{
             return userMapper.update(user);
          }
      }

    }

  • 在controller里出入service

相关推荐
flyinmind几秒前
Java环境与Android环境中使用QuickJS
java·开发语言·javascript·quickjs
不吃鱼的羊2 分钟前
DaVinci Developer自动连接
java·开发语言
farerboy8 分钟前
15-Java while 和 do...while循环
java·后端
在繁华处10 分钟前
Java从零到熟练(十二):Java与AI工具整合
java·人工智能·python
凯瑟琳.奥古斯特15 分钟前
力扣1235完整解法详解
java·开发语言·leetcode
quan_泉17 分钟前
DIDCTF 取证初学者
java·服务器·前端
i220818 Faiz Ul20 分钟前
民谣网站|基于Springboot的民谣网站管理系统(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·民谣网站
z落落20 分钟前
C# 继承基础详解(代码实战+权限规则)
java·开发语言
Oneslide22 分钟前
windows cmd输入输出都很卡
后端
摇滚侠30 分钟前
JDBC 基础到高级一套通关!基础篇 00-15
java·开发语言·数据库