对SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层等层的理解

这篇具有很好参考价值的文章主要介绍了对SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层等层的理解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

SSM 是 Spring + SpringMVC + Mybatis集成的框架。

MVC即model view controller。(模型,视图,控制器)

一、entity层(model层,domain层)

用于存放我们的实体类,类中定义了多个类属性,并与数据库表的字段保持一致,一张表对应一个类。

主要用于定义与数据库对象应的属性,提供get/set方法,tostring方法,有参无参构造函数。

二、mapper层(dao层)

数据持久层,先设计接口,然后在配置文件中进行配置其实现的关联。对数据库进行数据持久化操作,他的方法语句是直接针对数据库操作的。该层的作用为访问数据库,向数据库发送sql语句,完成数据的增删改查任务。
数据持久化操作就是指,把数据放到持久化的介质中,同时提供增删改查操作。

三、Service层=service接口+servicelmpl实现类

用来存放业务逻辑处理,也是一些关于数据库处理的操作,但不是直接和数据库打交道,和dao层一样都是先设计接口,再创建要实现的类,然后在配置文件中进行配置其实现的关联。然后就可以在Service层调用dao层的接口进行业务逻辑应用的处理。
当项目的业务逻辑简单时,可选择service层=service类;
当项目的业务复杂时,可选择Service层=service接口+serviceImpl实现类;
Service的impl是把mapper和service进行整合的文件
封装Service层的业务逻辑有利于业务逻辑的独立性和重复利用性。

四、controller层

控制器,控制业务逻辑service,控制请求和响应,负责前后端交互。因为service中的方法是我们使用到的,controller通过接收前端传过来的参数进行业务操作,在返回一个指定的路径或者数据表。

项目实例图:

model层(entity层,domain层)

public class TbKeyword extends BaseEntity {
    private static final long serialVersionUID = 1L;

    private Long id;

    @Excel(name = "关键词")
    private String words;


    @Excel(name = "词性")
    private String flag;

    public void setId(Long id)
    {
        this.id = id;
    }
    public Long getId() { return id; }

    public String getWords() {
        return words;
    }

    public void setWords(String words) {
        this.words = words;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }




    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("words", getWords())
                .append("flag", getFlag())
                .toString();
    }
}

对应数据库表字段 

对SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层等层的理解 

mapper层(dao层)

public interface TbKeywordMapper<TbKeyword> {
        /**
         * 关键词信息查询
         * @param id 车主信息查询主键
         * @return 车主信息查询
         */
        public TbKeyword selectTbKeywordById(Long id);
        /**
         * 查询关键词列表
         * @param tbKeyword 关键词查询
         * @return 关键词查询集合
         */
        public List<TbKeyword> selectTbKeywordList(TbKeyword tbKeyword);
        /**
         * 新增关键词查询
         *
         * @param tbKeyword 车主信息查询
         * @return 结果
         */
        public int insertTbKeyword(TbKeyword tbKeyword);
        //修改
        public int updateTbKeyword(TbKeyword tbKeyword);
        //删除
        public int deleteTbKeywordById(Long id);


}

Service层=service接口+servicelmpl实现类

public interface ITbKeywordService
{

    public TbKeyword selectTbKeywordById(Long id);

    public List<TbKeyword> selectTbKeywordList(TbKeyword tbKeyword);

    int insertTbUser(TbKeyword tbKeyword);

    int updateTbUser(TbKeyword tbKeyword);

    public int insertTbKeyword(TbKeyword tbKeyword);

    public int updateTbKeyword(TbKeyword tbKeyword);

    public int deleteTbKeywordById(Long id);

}

 

@Service
public class TbKeywordServiceImpl implements ITbKeywordService
{
    @Autowired
    private TbKeywordMapper tbKeywordMapper;

    /**
     * 查询关键词信息
     *
     * @param id 关键词信息查询主键
     * @return 关键词信息查询
     */
    @Override
    public TbKeyword selectTbKeywordById(Long id)
    {
        return (TbKeyword) tbKeywordMapper.selectTbKeywordById(id);
    }

    /**
     * 查询关键词列表
     *
     * @param tbKeyword 关键词查询
     * @return 关键词查询
     */
    @Override
    public List<TbKeyword> selectTbKeywordList(TbKeyword tbKeyword)
    {
        return tbKeywordMapper.selectTbKeywordList(tbKeyword);
    }

    @Override
    public int insertTbUser(TbKeyword tbKeyword) {
        return 0;
    }

    @Override
    public int updateTbUser(TbKeyword tbKeyword) {
        return 0;
    }

    /**
     * 新增关键词信息查询
     *
     * @param tbKeyword 关键词信息查询
     * @return 结果
     */
    @Override
    public int insertTbKeyword(TbKeyword tbKeyword)
    {
        return tbKeywordMapper.insertTbKeyword(tbKeyword);
    }

    /**
     * 修改关键词查询
     *
     * @param tbKeyword 车主信息查询
     * @return 结果
     */
    @Override
    public int updateTbKeyword(TbKeyword tbKeyword)
    {
        return tbKeywordMapper.updateTbKeyword(tbKeyword);
    }


    /**
     * 删除关键词查询信息
     *
     * @param id 车主信息查询主键
     * @return 结果
     */
    @Override
    public int deleteTbKeywordById(Long id)
    {
        return tbKeywordMapper.deleteTbKeywordById(id);
    }
}

controller层文章来源地址https://www.toymoban.com/news/detail-444683.html

@RestController
@RequestMapping("/mychery/keyword")
public class TbKeywordController extends BaseController
{
    @Autowired
    private ITbKeywordService tbKeywordService;

    /**
     * 查询关键词信息列表
     */
    @PreAuthorize("@ss.hasPermi('mychery:keyword:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbKeyword tbKeyword)
    {
        startPage();
        List<TbKeyword> list = tbKeywordService.selectTbKeywordList(tbKeyword);
        return getDataTable(list);
    }

    /**
     * 导出关键词信息列表
     */
    @PreAuthorize("@ss.hasPermi('mychery:keyword:export')")
    @Log(title = "关键词信息查询", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbKeyword tbKeyword)
    {
        List<TbKeyword> list = tbKeywordService.selectTbKeywordList(tbKeyword);
        ExcelUtil<TbKeyword> util = new ExcelUtil<TbKeyword>(TbKeyword.class);
        return util.exportExcel(list, "关键词信息查询数据");
    }

    /**
     * 获取关键词详细信息
     */
    @PreAuthorize("@ss.hasPermi('mychery:keyword:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbKeywordService.selectTbKeywordById(id));
    }

    /**
     * 新增关键词信息查询
     */
    @PreAuthorize("@ss.hasPermi('mychery:user:add')")
    @Log(title = "车主信息查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbKeyword tbKeyword)
    {
        return toAjax(tbKeywordService.insertTbUser(tbKeyword));
    }

    /**
     * 修改关键词信息查询
     */
    @PreAuthorize("@ss.hasPermi('mychery:keyword:edit')")
    @Log(title = "关键词信息查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbKeyword tbKeyword)
    {
        return toAjax(tbKeywordService.updateTbKeyword(tbKeyword));
    }

    /**
     * 删除关键词信息查询
     */
    @PreAuthorize("@ss.hasPermi('mychery:keyword:remove')")
    @Log(title = "关键词信息查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(tbKeywordService.deleteTbKeywordById(id));
    }
}

到了这里,关于对SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层等层的理解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • spring boot,DAO层、ENTITY层、SERVICE层、CONTROLLER层之间的关系

    主要用于 定义与数据库对象应的属性,提供get/set方法 ,tostring方法,有参无参构造函数。 DAO层 首先会创建Dao接口 , 接着就可以在配置文件中定义该接口的实现类 ;接着就可以在模块中调用Dao的接口进行数据业务的处理,而不用关注此接口的具体实现类是哪一个类,Dao层的数

    2024年04月10日
    浏览(29)
  • Spring Boot项目中的Controller、Service、Mapper和Entity层的作用与联系

    在Spring Boot项目中,常见的四个层次是Controller层、Service层、Mapper层和Entity层。它们各自承担着不同的职责,但彼此之间存在着紧密的联系。本文将详细介绍这四个层次的作用与联系,并提供相关实例来说明它们之间的关系。 Controller层是Spring Boot应用程序的入口点,用于处理

    2024年02月10日
    浏览(34)
  • 剖析Java中的Entity、service、serviceImpl、Mapper以及Controller层之间的关系(代码诠释)

    学习了Java的相关方面知识之后,但对于各层次之间的关系以及部署,可能还会有些陌生感,下面以代码讲解各层之间的关系。 (企业中多数以Springboot为例,下面的代码都是以Springboot为例) 如果还停留在SSM基础或者补充Springboot的基础知识,也可在我的博客搜索。 简单的Sp

    2024年04月26日
    浏览(24)
  • SpringBoot(三层框架Controller,Mapper,Service)中遇到的一些注解整理

    本文主要从Controller层,Service层,Mapper层这三层架构中记录用到的各种注解 还有一些MyBatis用到的注解 持续更新到本人的毕设做完为止,太多了太多了根本学不完哈哈哈 用于建立HTTP请求与处理方法之间的映射关系,其中 XXX Mapping限定了提交http请求的方法 用于获取URL中提交过来的

    2024年01月21日
    浏览(30)
  • SpringBoot框架分层(View层、Controller层、Service层、Mapper层、pojo层)

      SpringBoot框架一般分为View层、Controller层、Service层、Mapper层、pojo层。 View层:视图层,根据接到的数据展示页面给用户 Controller层:响应用户需求,决定用什么视图,需要准备什么数据来显示。Controller层负责前后端交互,接收前端请求,调用Service层,接收Service层返回的数据

    2024年02月07日
    浏览(39)
  • Dao层、Service层、Entity层、Servlet层、Utils层

    这几天在复习高数,还有刷题。 B: 第五周任务 [Cloned] - Virtual Judge (vjudge.net) http://t.csdn.cn/S3imr  G: 第五周任务 [Cloned] - Virtual Judge (vjudge.net) http://t.csdn.cn/UVgfK   Dao层是数据访问层 Service层是业务逻辑层 Entity层是实体层 Servlet层是控制层 Utils层是工具类层 分层架构没有规定自

    2024年02月09日
    浏览(33)
  • 使用mybatisX逆向生成数据表实体类(pojo,dao),mapper,service

    先看使用mybatisX后生成的文件。 1.先在idea安装mybatisX插件,在file-setting-plugins,搜索mybatisX插件,重新启动idea即可。 2.在idea编辑器右侧点击Database,点击“+”链接你的数据库类型,这里我选mysql。     输入root,密码:xxxx 输入url:jdbc:mysql://localhost:3306/emos?useUnicode=truecharacterEnc

    2024年02月03日
    浏览(34)
  • Java——Controller层、Service层和DAO层

    在 Java 的三层架构中,通常会涉及到以下几个层次:Controller层、Service层和DAO层。 Controller层(控制层):Controller层是应用程序的入口点,负责接收用户的请求并处理。它通常处理来自前端或客户端的请求,并将请求转发给相应的Service层进行业务处理。Controller层负责接收和验

    2024年02月04日
    浏览(28)
  • SpringBoot(入门)三层架构Controller、Service、Dao

    SpringB是一个基于Java的开源框架,用于创建微服务。它由Pivotal Team开发,用于构建独立的生产就绪Spring应用。 SpringBoot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件,简化开发。 Controller层:(顾名思义 控制层)控制并处理http请求,将其不

    2024年02月07日
    浏览(31)
  • java中的controller、domain、mapper(persistence)、service 都是做什么用的?

    java中的controller、domain、mapper(persistence)、service代表了服务端接口的 4 层,第一层是控制层(controller),负责接口请求/响应的控制,调用第二层业务逻辑层(service 一般分为接口和实现),完成具体业务功能,它会调用第三层数据持久层 mapper(persistence)的逻辑,作用是访

    2024年02月15日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包