源码(免积分)下载地址:
Java_SSM_tb_community对照编码(全部源码)-Java文档类资源-CSDN下载
最终效果:(包含需要的所有样式都写了)
添加页面
删除提示:
目录
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脚本
代码语言:javascript复制/*
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
代码语言:javascript复制 <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
代码语言:javascript复制<?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
代码语言:javascript复制driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/community_db?characterEncoding=utf-8
username=root
password=12345678
applicationContext.xml
代码语言:javascript复制<?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
代码语言:javascript复制<?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
代码语言:javascript复制<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】
代码语言:javascript复制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】
代码语言:javascript复制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】
代码语言:javascript复制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】
代码语言:javascript复制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】
代码语言:javascript复制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】
代码语言:javascript复制<%@ 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> 共计<%=list.size()%>条数据
</td>
</tr>
</table>
</body>
</html>
【webapp/views/AddInfoPage.jsp】
代码语言:javascript复制<%--
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="提交"/>
<input type="reset"/>
</td>
</tr>
</table>
</form>
</body>
</html>
tomcat配置(略)
祝大家考试顺利。
访问路径:端口号根据自己的配置些啊:
【http://localhost:8088/GetInfo】