关于java的三层结构设计

举报
花花叔叔 发表于 2022/08/13 01:14:24 2022/08/13
【摘要】 第一层是dao层 dao包下面有一个impl包,这个包下面存放的是接口的实现类,而接口就是直接在dao包下面。 dao接口: public List<Course> findCourseL...
  1. 第一层是dao层
    在这里插入图片描述
    dao包下面有一个impl包,这个包下面存放的是接口的实现类,而接口就是直接在dao包下面。
    dao接口:
public List<Course> findCourseList();

  
 
  • 1

dao层接口实现类 – 就是写一些关于数据库的东西,这个就是实现接口中的方法。

//    查询课程列表信息
    @Override
    public List<Course> findCourseList() {

        try {
//        1. 创建QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

//        2. 编写sql 判断是否删除 取出is_del = 0 的数据,未删除的数据
            String sql = "SELECT id,course_name,price,sort_num FROM course WHERE is_del = ?";

//        3. 执行查询
            List<Course> query = qr.query(sql, new BeanListHandler<Course>(Course.class), 0);

//        4. 返回数据
            return query;

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  1. 第二层是service层 – service层就是new一个dao层对象,之后打点调用dao层的方法。

接口层

public List<Course> findCourseList();

  
 
  • 1

实现层

//    创建courseDao对象
    CourseDao courseDao = new CourseDaoImpl();

//    查询课程列表信息
    @Override
    public List<Course> findCourseList() {

        List<Course> courseList = courseDao.findCourseList();

        return courseList;
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  1. web层 – servlet层 – 这一层就是new出一个新的service对象,然后打点调用方法。
//    查询课程信息列表
    public void findCourseList(HttpServletRequest request , HttpServletResponse response) {

//        1. 接收参数

//        2. 业务处理
        CourseService cs = new CourseServiceImpl();

        List<Course> courseList = cs.findCourseList();

//        3. 响应结果

//        解决乱码问题 + 只显示需要显示的内容
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,
                "id","course_name","price","sort_num","status");

        String result = JSON.toJSONString(courseList,filter);  // 客户端需要JSON格式的字符串

        try {
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

文章来源: blog.csdn.net,作者:花花叔叔,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_52077949/article/details/122756178

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。