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

SpringBoot框架整合SpringMVC、Mybatis框架,对数据库 *** 作的工作原理,第1张

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 findObject();
    
    @Insert("insert into tb_goods (name,remark,createdTime) valuws(#{name},#{remark}.#now())")
    int insertGoods(Goods entiry);
}
 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5722183.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存