栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
}
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/726842.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号