SpringBoot框架整合SpringMVC、Mybatis框架,对数据库操作的工作原理

Controller层

Controller层是接收用户访问的url信息,再将获取到的内容发送到其他层级进行处理,处理完成后返回新的url,使用户得到想要查询或是其他操作的页面。

@Controller
@RequestMapping("/goods/")
 public class GoodsController{
    @Autowired
    private GoodsService goodsService;
    
    @RequestMapping("doDeleteById")
    public String doDeleteById(Long id) {
        goodsService.deleteById(id);
        return "redirect:doGoodsUI";
    }

Service层

Service层是数据的过渡层,包含接口和实现类,主要作用是接收Controller层传递的参数,进行数据合理性检查等操作,检查通过再将信息传递到Dao层。

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsDao goodsDao;
    @Override
    public int deleteById(Long id) {
        if(id==null || id<1) throw new IllegalArgumentException("id值无效");
        int rows = goodsDao.deleteById(id);
        if(rows == 0) throw new NoSuchElementException("记录可能已经不存在");
        return rows;
    }

Dao层

Dao层的作用就是实现一些增删改查的数据库操作,根据Service层传递的参数进行指定的数据库操作。

@Mapper
public interface GoodsDao {
    @Delete("delete from tb_goods where id = #{id}")
    int deleteById(Long id);//基于id执行商品数据的删除操作返回的:影响行数
    
    int deleteObjects(Integer...ids);//...表示可变参数类型,基于多个id删除商品信息
    //查询所有商品
    @Select("select id,name,remark,createdTime from tb_goods")
    List<Goods> findObject();
    
    @Insert("insert into tb_goods (name,remark,createdTime) valuws(#{name},#{remark}.#now())")
    int insertGoods(Goods entiry);
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GOWE845

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值