当年的Java考试:JAVA&移动应用&大数据-大三-社区疫苗接种管理系统(全部源码·保姆式呵护)

举报
红目香薰 发表于 2022/05/31 18:41:02 2022/05/31
【摘要】 ​最终效果:(包含需要的所有样式都写了)​编辑​编辑添加页面​编辑删除提示:​编辑目录1、数据库环境MySQL脚本2、创建项目3、基础配置文件位置pom.xmlmapper.xmljdbc.propertiesapplicationContext.xmlspring-mvc.xmlweb.xml4、各层级包创建5、完成各层编码【com.item.model/TbCommunity.java】...

最终效果:(包含需要的所有样式都写了)

编辑

编辑

添加页面

编辑

删除提示:

编辑

目录

1、数据库环境MySQL脚本

2、创建项目

3、基础配置文件位置

pom.xml

mapper.xml

jdbc.properties

applicationContext.xml

spring-mvc.xml

web.xml

4、各层级包创建

5、完成各层编码

【com.item.model/TbCommunity.java】

【com.item.dao/TbCommunityMapper.java】

【com.item.service/TbCommunityService.java】

【com.item.serviceimpl/TbCommunityServiceImpl.java】

【com.item.controller/TbCommunityController.java】

【webapp/views/GetInfo.jsp】

【webapp/views/AddInfoPage.jsp】

tomcat配置(略)



1、数据库环境MySQL脚本

/*
 Navicat Premium Data Transfer

 Source Server         : test
 Source Server Type    : MySQL
 Source Server Version : 50640
 Source Host           : localhost:3306
 Source Schema         : community_db

 Target Server Type    : MySQL
 Target Server Version : 50640
 File Encoding         : 65001

 Date: 31/05/2022 16:49:17
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tb_community
-- ----------------------------
DROP TABLE IF EXISTS `tb_community`;
CREATE TABLE `tb_community`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `existing` int(11) NOT NULL,
  `completed` int(11) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of tb_community
-- ----------------------------
INSERT INTO `tb_community` VALUES (1, '平壤们社区', 12000, 32345);
INSERT INTO `tb_community` VALUES (2, '红光社区', 22000, 42121);
INSERT INTO `tb_community` VALUES (3, '鱼梁洲社区', 42000, 21215);
INSERT INTO `tb_community` VALUES (4, '桥口社区', 32125, 13212);
INSERT INTO `tb_community` VALUES (5, '汉阳社区', 7000, 129832);

SET FOREIGN_KEY_CHECKS = 1;

2、创建项目

编辑

编辑编辑

编辑整个替换【dependencies】内所有内容

编辑

3、基础配置文件位置

编辑

pom.xml

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.9</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.18.RELEASE</version>
<!--      <optional>true</optional>-->
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
 
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.6</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
 
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
 
 
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>2.0.4</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
 
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.2.9</version>
    </dependency>

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.item.dao.TbCommunityMapper">
    <select id="GetInfo" resultType="TbCommunity">
        select * from tb_community;
    </select>
    <select id="SelectByName" resultType="TbCommunity">
        select * from tb_community where name like "%${name}%";
    </select>
    <insert id="AddInfo">
        insert into tb_community values(0,"${name}",#{existing},#{completed})
    </insert>
    <delete id="DeleteById">
        delete from tb_community where id=#{id}
    </delete>
</mapper>

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/community_db?characterEncoding=utf-8
username=root
password=12345678

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.item.model" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.item.dao" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:component-scan base-package="com.item" />

    <mvc:annotation-driven />

    <mvc:default-servlet-handler/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

4、各层级包创建

创建【com.item】下的【controller】【dao】【model】【service】【serviceimpl】五层

创建【webapp】下【views】视图层

编辑

5、完成各层编码

【com.item.model/TbCommunity.java】

package com.item.model;

public class TbCommunity {
    private int id;
    private String name;
    private int existing;
    private int completed;

    @Override
    public String toString() {
        return "TbCommunity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", existing=" + existing +
                ", completed=" + completed +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getExisting() {
        return existing;
    }

    public void setExisting(int existing) {
        this.existing = existing;
    }

    public int getCompleted() {
        return completed;
    }

    public void setCompleted(int completed) {
        this.completed = completed;
    }
}

【com.item.dao/TbCommunityMapper.java】

package com.item.dao;

import java.util.*;
import com.item.model.TbCommunity;
import org.apache.ibatis.annotations.Param;

public interface TbCommunityMapper {
    List<TbCommunity> GetInfo();
    List<TbCommunity> SelectByName(@Param("name") String name);
    int AddInfo(
            @Param("name") String name,
            @Param("existing") int existing,
            @Param("completed") int completed
    );
    int DeleteById(@Param("id") int id);

}

【com.item.service/TbCommunityService.java】

package com.item.service;

import com.item.model.TbCommunity;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface TbCommunityService {
    /**
     * 查询所有
     * @return
     */
    List<TbCommunity> GetInfo();
    List<TbCommunity> SelectByName(String name);
    int AddInfo(
            String name,
            int existing,
            int completed
    );
    int DeleteById(int id);

}

【com.item.serviceimpl/TbCommunityServiceImpl.java】

package com.item.serviceimpl;

import com.item.dao.TbCommunityMapper;
import com.item.model.TbCommunity;
import com.item.service.TbCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TbCommunityServiceImpl implements TbCommunityService {
    @Autowired
    private TbCommunityMapper tbCommunityMapper;
    @Override
    public List<TbCommunity> GetInfo() {
        return tbCommunityMapper.GetInfo();
    }

    @Override
    public List<TbCommunity> SelectByName(String name) {
        return tbCommunityMapper.SelectByName(name);
    }

    @Override
    public int AddInfo(String name, int existing, int completed) {
        return tbCommunityMapper.AddInfo(name,existing,completed);
    }

    @Override
    public int DeleteById(int id) {
        return tbCommunityMapper.DeleteById(id);
    }
}

【com.item.controller/TbCommunityController.java】

package com.item.controller;

import com.item.model.TbCommunity;
import com.item.service.TbCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TbCommunityController {

    @Autowired
    private TbCommunityService db;
    @GetMapping("/GetInfo")
    public String GetInfo(HttpServletRequest request, Model model){
        List<TbCommunity> list = db.GetInfo();
        model.addAttribute("lists",list);
        return "GetInfo";
    }

    @GetMapping("/SelectByName")
    public String SelectByName(HttpServletRequest request, Model model){
        String name = request.getParameter("name");
        List<TbCommunity> list = db.SelectByName(name);
        model.addAttribute("lists",list);
        return "GetInfo";
    }
    /**
     * 添加
     * @return
     */
    @GetMapping("/AddInfoPage")
    public String AddInfoPage(){
        return "AddInfoPage";
    }
    @PostMapping("/AddInfo")
    public String AddInfo(HttpServletRequest request){
        String name = request.getParameter("name");
        String existing = request.getParameter("existing");
        String completed = request.getParameter("completed");
        db.AddInfo(name, Integer.parseInt(existing), Integer.parseInt(completed));
        return "redirect:/GetInfo";
    }
    @GetMapping("/DeleteById")
    public String DeleteById(HttpServletRequest request){
        String id = request.getParameter("id");
        db.DeleteById(Integer.parseInt(id));
        return "redirect:/GetInfo";
    }
}

【webapp/views/GetInfo.jsp】

<%@ page import="java.util.*" %>
<%@ page import="com.item.model.TbCommunity" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/5/31 0031
  Time: 10:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>显示</title>
</head>
<body>

<fieldset style="width: 100%;border-radius: 9px">
    <legend>
        搜索
    </legend>
    <form action="/SelectByName" method="get">
        名称:<input type="text" name="name">
        <input type="submit">
    </form>
</fieldset>

<% List<TbCommunity> list = (List<TbCommunity>) request.getAttribute("lists");%>
<style>
    table tr:hover{
        background-color: black;
        color:white;
    }
</style>
<table style="width: 100%" border="1" cellpadding="1" cellspacing="1">
    <tr style="text-align: center;background-color: gray;color:white;">
        <th>编号</th>
        <th>用户名</th>
        <th>接种疫苗</th>
        <th>已接种疫苗</th>
        <th>操作</th>
    </tr>
    <%
        for (TbCommunity t : list) {
    %>
    <tr>
        <td><%=t.getId()%></td>
        <td><%=t.getName()%></td>
        <td><%=t.getExisting()%></td>
        <td><%=t.getCompleted()%></td>
        <td>
            <a href="/DeleteById?id=<%=t.getId()%>" onclick="return confirm('是否删除此行?')">删除</a>
        </td>
    </tr>
    <%
        }
    %>
    <tr>
        <td colspan="10" align="right">
            <a href="/AddInfoPage">添加</a>&nbsp;&nbsp;&nbsp;&nbsp;共计<%=list.size()%>条数据
        </td>
    </tr>
</table>
</body>
</html>

【webapp/views/AddInfoPage.jsp】

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/5/31 0031
  Time: 10:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加</title>
</head>
<body>
<h1 align="center">添加信息</h1>
    <form action="/AddInfo" method="post">
        <table align="center">
            <tr>
                <td>社区名称</td>
                <td><input type="text" name="name"/></td>
            </tr>
            <tr>
                <td>现存疫苗</td>
                <td><input type="text" name="existing"/></td>
            </tr>
            <tr>
                <td>已接种人数</td>
                <td><input type="text" name="completed"/></td>
            </tr>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="提交"/>
                    &nbsp;&nbsp;
                    <input type="reset"/>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

tomcat配置(略)

编辑

祝大家考试顺利。 

访问路径:端口号根据自己的配置些啊:

http://localhost:8088/GetInfo

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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