javaWeb 阶段练习 三层架构案例

今天给大家分享一个比较全面的web阶段的一个练习,一个三成架构案例的入门。今天只做了查询的功能,删除,修改,添加后面会补充。希望对初学者有用!

三层架构案例

综合练习–1

一、项目结构

javaWeb 阶段练习 三层架构案例_第1张图片

二、环境搭建

1、导入jar包

javaWeb 阶段练习 三层架构案例_第2张图片

2、引入druid.properties

3、引入JDBCUtils帮助类

4、引入静态页面及相关样式表和js文件

二、domain类设计

User.java

package cn.itcast.domain;

public class User {

    private Integer id;
    private String name;
    private String gender;
    private Integer age;
    private String address;
    private String qq;
    private String email;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

三、dao接口及实现

1、UserDao接口

package cn.itcast.dao;

import cn.itcast.domain.User;

import java.util.List;

public interface UserDao {

    List<User> findAll();
}

2、UserDaoImpl实现类

package cn.itcast.dao.impl;

import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
import cn.itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDaoImpl implements UserDao {

    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<User> findAll() {

        String sql = "select * from user";

        List<User> userList = template.query(sql, new BeanPropertyRowMapper<>(User.class));

        return userList;
    }
}

四、service接口及实现

1、UserService接口

package cn.itcast.service;

import cn.itcast.domain.User;

import java.util.List;

public interface UserService {
    List<User> findAll();
}

2、UserServiceImpl实现类

package cn.itcast.service.impl;

import cn.itcast.dao.UserDao;
import cn.itcast.dao.impl.UserDaoImpl;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {

    UserDao userDao = new UserDaoImpl();

    @Override
    public List<User> findAll() {

        return userDao.findAll();

    }
}

五、web层

package cn.itcast.web.servlet;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/UserListServlet")
public class UserListServlet extends HttpServlet {


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //调用业务层获取所有用户信息
        UserService userService = new UserServiceImpl();
        List<User> userList = userService.findAll();

        //将userList集合传到list.jsp进行显示
        request.setAttribute("list",userList);
        request.getRequestDispatcher("/list.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

六、index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html lang="zh-CN">
<head>
  <meta charset="utf-8"/>
  <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <title>首页title>

  
  <link href="css/bootstrap.min.css" rel="stylesheet">
  
  <script src="js/jquery-2.1.0.min.js">script>
  
  <script src="js/bootstrap.min.js">script>
  <script type="text/javascript">
  script>
head>
<body>
<div align="center">
  <a
          href="${pageContext.request.contextPath}/UserListServlet" style="text-decoration:none;font-size:33px">查询所有用户信息
  a>
div>
body>
html>

在这里插入图片描述

七、list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>





    
    
    
    
    
    
    
    用户信息管理系统

    
    
    
    
    
    
    


用户信息列表

编号 姓名 性别 年龄 籍贯 QQ 邮箱 操作
${temp.id} ${temp.name} ${temp.gender} ${temp.age} ${temp.address} ${temp.qq} ${temp.email} 修改 删除
添加联系人

该图为forEach用法
javaWeb 阶段练习 三层架构案例_第3张图片
以下是代码及相关jar包,百度云提取
链接:https://pan.baidu.com/s/1T8m8DiF3yGGHS_HRCScXKw
提取码:2vpw

你可能感兴趣的:(学习笔记)