[secret room escape game theme ranking list] Based on spring MVC + Spring + mybatis

Mingjin 2021-10-14 06:28:28

Download resources :
https://download.csdn.net/download/weixin_44893902/25706959

One 、 Language and environment

1. Implement language :Java Language

2. Environmental requirements :eclipse/myeclipse /idea、maven、mysql

3. Using technology :Spring、SpringMVC、MyBatis、 Connection pool and json The package chooses itself

Two 、 Realization function

The secret room escape game is more and more popular with young people , Now rank the themes of secret room games everywhere , Selection 2021 The favorite secret room theme of the year .

explain : The following interface styles are for reference only , The actual completion effect is beautiful and reasonable .

1、 Display the data

According to the figure 1 Format , Show t_games All data in the table , And in accordance with the 【 Number of votes 】 Sort columns in descending order , Actually 【 Subject category 】 One column in t_games The table stores numbers , Need to be combined t_gamesType Corresponding in the table id The value shows the kind text . There is a voting button behind each row of data to vote on the corresponding topic .
 Data display page

2、 Query data

According to the 【 Subject name 】 and 【 Topic type 】 Data query . if 【 Subject name 】 It's empty , According to 【 Topic type 】 Inquire about , if 【 Subject name 】 Not empty , According to 【 Subject name 】 Make fuzzy queries and also combine 【 Topic type 】 Inquire about .【 Topic type 】 Drop down box required , And the options inside are from the database table t_gamesType Find out . As shown in the figure .
 chart 2 Query function

3、 Voting function

Click on 【 operation 】 The voting button in the column , A secondary confirmation box pops up , Click OK again , You can vote for the corresponding topic , After a successful vote , The number of votes for this data +1, As shown in the figure :
 chart 3 Vote for the topic
After the voting data is refreshed , If there is a change in the number of votes , To sort the new data in descending order , As shown in the figure :
 chart 4 After a successful vote , To update the latest ranking order

4、 New theme

Click the new button , Go to the new page , The options in the topic category need to go to the database t_gamesType Table query , Online time needs date Control , After clicking the Add button , Insert data into the data table , The default number of new topic votes is 0, And return to the main page to display the latest data , As shown in the figure :
 chart 5 New page

3、 ... and 、 Database design

1、 Create database :gamesDB.

2、 Create secret room ranking data table (t_games)

The structure is as follows :

Table name :t_ games
The entity name : Secret room ranking data sheet
Primary key :id

Serial number Field name Field description type digit attribute remarks
1 id Primary key int 11 Non empty id Primary key column , Self increasing 1
2 gamesName Secret room subject name varchar 50 Non empty
3 gamesType Chamber type number int 11 Non empty Foreign keys ,t_gamesType In the table id
4 producers Producer Varchar 50 Non empty
5 uptime On-line time date Non empty
6 votes Number of votes int 11 Non empty The default value is 0

Four 、 Recommended steps

1、 Use MySql Create database , Create the corresponding 2 A watch , Insert data as required .

t_games surface
 Insert picture description here
t_gamesType surface
 Insert picture description here

2、 Use development tools to create projects , Use maven add to spring、springMVC、mybatis、 Connection pool and other dependent coordinates .

3、 complete MyBatis Persistent layer function operation , Respectively for 2 A table completes the corresponding function writing .

4、 Complete the business logic layer interface 、 Realize the function of the class .

5、 Complete the corresponding functions of the controller

6、 Create the interface to interact with the controller to realize the corresponding functions

7 Italics style 、 Deploy the project to Tomcat, Run access test to see if the project is normal

5、 ... and 、 Implementation code

1、MySQL database

gamesdb.sql
 Insert picture description here

/* Navicat MySQL Data Transfer Date: 2021-07-27 20:29:20 */
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `t_games`
-- ----------------------------
DROP TABLE IF EXISTS `t_games`;
CREATE TABLE `t_games` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`games_name` varchar(255) DEFAULT NULL,
`games_type` int(11) DEFAULT NULL,
`producers` varchar(255) DEFAULT NULL,
`uptime` varchar(255) DEFAULT NULL,
`votes` int(11) unsigned zerofill DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_games
-- ----------------------------
INSERT INTO `t_games` VALUES ('1', ' The darkness has invaded ', '1', '911 Dungeons ', '2019-07-01', '00000005400');
INSERT INTO `t_games` VALUES ('2', ' The land of silence ', '1', '911 Dungeons ', '2021-04-10', '00000003421');
INSERT INTO `t_games` VALUES ('3', ' latent ', '2', ' Changsha Zhiyan Culture Co., Ltd ', '2020-06-20', '00000002339');
INSERT INTO `t_games` VALUES ('4', ' Resurrection stone ', '3', ' Diagon Alley ', '2020-08-30', '00000002340');
INSERT INTO `t_games` VALUES ('5', ' Journey to the west ', '1', ' Cultural media ', '2021-07-27', '00000000007');
-- ----------------------------
-- Table structure for `t_gamestype`
-- ----------------------------
DROP TABLE IF EXISTS `t_gamestype`;
CREATE TABLE `t_gamestype` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type_name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_gamestype
-- ----------------------------
INSERT INTO `t_gamestype` VALUES ('1', ' Terror ');
INSERT INTO `t_gamestype` VALUES ('2', ' Spy war ');
INSERT INTO `t_gamestype` VALUES ('3', ' Solve the mystery ');

2、JAVA Code

gamesDB
 Insert picture description here

(1) com.cst.controller【 Control layer 】

① TGamesController,java

package com.cst.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cst.entity.TGames;
import com.cst.service.TGamesService;
@Controller
public class TGamesController {

@Resource
TGamesService tGamesService;
// Query all the data , Fuzzy query 
@RequestMapping("/tGamesList")
public String accountList(Model model,String keyword,String type){

List<TGames> selectAll = tGamesService.selectAll(keyword,type);
model.addAttribute("selectAll",selectAll);
return "games";
}
// Enter the add method 
@RequestMapping("/addGames")
public String addAccount() {

return "addGames";
}
// Perform the add operation 
@RequestMapping("/addGamesDo")
public String addAccountDo(TGames games) {

games.setVotes(0);
int users = tGamesService.insert(games);
if (users>0) {

return "redirect:/tGamesList.do";
}else {

return "forward:/addGames.do";
}
}
@RequestMapping("/updateGamesDo")
public String updateGamesDo(Integer id,Integer votes) {

TGames games=new TGames();
int votes1=votes+1;
games.setId(id);
games.setVotes(votes1);
tGamesService.updateByPrimaryKey(games);
return "redirect:/tGamesList.do";
}
}

(2) com.cst.dao【 Database access layer 】

① TGamesMapper.java

package com.cst.dao;
import com.cst.entity.TGames;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface TGamesMapper {

int deleteByPrimaryKey(Integer id);
int insert(TGames record);
TGames selectByPrimaryKey(Integer id);
List<TGames> selectAll(@Param("keyword")String keyword,@Param("type")String type);
int updateByPrimaryKey(TGames record);
}

② TGamestypeMapper.java

package com.cst.dao;
import com.cst.entity.TGamestype;
import java.util.List;
public interface TGamestypeMapper {

int deleteByPrimaryKey(Integer id);
int insert(TGamestype record);
TGamestype selectByPrimaryKey(Integer id);
List<TGamestype> selectAll();
int updateByPrimaryKey(TGamestype record);
}

③ TGamesMapper.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.cst.dao.TGamesMapper" >
<resultMap id="BaseResultMap" type="com.cst.entity.TGames" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="games_name" property="gamesName" jdbcType="VARCHAR" />
<result column="games_type" property="gamesType" jdbcType="INTEGER" />
<result column="producers" property="producers" jdbcType="VARCHAR" />
<result column="uptime" property="uptime" jdbcType="VARCHAR" />
<result column="votes" property="votes" jdbcType="INTEGER" />
<result column="type_name" property="typeName" jdbcType="VARCHAR" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_games
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.cst.entity.TGames" >
insert into t_games (id, games_name, games_type,
producers, uptime, votes
)
values (#{id,jdbcType=INTEGER}, #{gamesName,jdbcType=VARCHAR}, #{gamesType,jdbcType=INTEGER},
#{producers,jdbcType=VARCHAR}, #{uptime,jdbcType=VARCHAR}, #{votes,jdbcType=INTEGER}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.cst.entity.TGames" >
update t_games
set votes = #{votes,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select id, games_name, games_type, producers, uptime, votes
from t_games
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select g.id, games_name, type_name, producers, uptime, votes
from t_games g,t_gamestype t where g.games_type=t.id
<if test="keyword!=null and keyword!=''">
and games_name like concat('%', #{keyword}, '%')
</if>
<if test="type!=null and type!=''">
and type_name=#{type}
</if>
order by votes desc
</select>
</mapper>

④ TGamestypeMapper.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.cst.dao.TGamestypeMapper" >
<resultMap id="BaseResultMap" type="com.cst.entity.TGamestype" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="type_name" property="typeName" jdbcType="VARCHAR" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_gamestype
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.cst.entity.TGamestype" >
insert into t_gamestype (id, type_name)
values (#{id,jdbcType=INTEGER}, #{typeName,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com.cst.entity.TGamestype" >
update t_gamestype
set type_name = #{typeName,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select id, type_name
from t_gamestype
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select id, type_name
from t_gamestype
</select>
</mapper>

(3) com.cst.entity 【 Package for storing entities 】

① TGames.java

package com.cst.entity;
public class TGames {

private Integer id;
private String gamesName;
private Integer gamesType;
private String producers;
private String uptime;
private Integer votes;
private String typeName;
public String getTypeName() {

return typeName;
}
public void setTypeName(String typeName) {

this.typeName = typeName == null ? null : typeName.trim();
}
public Integer getId() {

return id;
}
public void setId(Integer id) {

this.id = id;
}
public String getGamesName() {

return gamesName;
}
public void setGamesName(String gamesName) {

this.gamesName = gamesName == null ? null : gamesName.trim();
}
public Integer getGamesType() {

return gamesType;
}
public void setGamesType(Integer gamesType) {

this.gamesType = gamesType;
}
public String getProducers() {

return producers;
}
public void setProducers(String producers) {

this.producers = producers == null ? null : producers.trim();
}
public String getUptime() {

return uptime;
}
public void setUptime(String uptime) {

this.uptime = uptime == null ? null : uptime.trim();
}
public Integer getVotes() {

return votes;
}
public void setVotes(Integer votes) {

this.votes = votes;
}
}

② TGamestype.java

package com.cst.entity;
public class TGamestype {

private Integer id;
private String typeName;
public Integer getId() {

return id;
}
public void setId(Integer id) {

this.id = id;
}
public String getTypeName() {

return typeName;
}
public void setTypeName(String typeName) {

this.typeName = typeName == null ? null : typeName.trim();
}
}

(4) com.cst.generator【 Entity classes automatically generate packages 】

① Generator.java

package genter;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Generator {

/* * targetRuntime="MyBatis3Simple", No generation Example */
public void generateMyBatis() {

//MBG Warning messages during execution 
List<String> warnings = new ArrayList<String>();
// When the generated code repeats , Covering the original code 
boolean overwrite = true ;
String generatorFile = "/generatorConfig.xml";
//String generatorFile = "/generator/generatorConfigExample.xml";
// Read MBG The configuration file 
InputStream is = Generator.class.getResourceAsStream(generatorFile);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config;
try {

config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
// establish MBG
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
// Execute generate code 
myBatisGenerator.generate(null);
} catch (IOException e) {

e.printStackTrace();
} catch (XMLParserException e) {

e.printStackTrace();
} catch (InvalidConfigurationException e) {

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

e.printStackTrace();
} catch (InterruptedException e) {

e.printStackTrace();
}
for (String warning : warnings) {

System.out.println(warning);
}
}
public static void main(String[] args) {

Generator generator = new Generator();
generator.generateMyBatis();
}
}

(5) com.cst.service【 Interact with the page 】

① TGamesService.java

package com.cst.service;
import java.util.List;
import com.cst.entity.TGames;
import com.cst.entity.TGamestype;
public interface TGamesService {

int insert(TGames record);
List<TGames> selectAll(String keyword,String type);
int updateByPrimaryKey(TGames record);
}

(6) com.cst.service.imp【service Implementation class of 】

① TGServiceImpl.java

package com.cst.service.imp;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cst.dao.TGamesMapper;
import com.cst.entity.TGames;
import com.cst.entity.TGamestype;
import com.cst.service.TGamesService;
@Service
public class TGServiceImpl implements TGamesService{

@Resource
TGamesMapper tgamesMapper;
@Override
public List<TGames> selectAll(String keyword,String type) {

// TODO Auto-generated method stub
List<TGames> tGamesList=tgamesMapper.selectAll(keyword,type);
return tGamesList;
}
@Override
public int insert(TGames record) {

// TODO Auto-generated method stub
int add=tgamesMapper.insert(record);
return add;
}
@Override
public int updateByPrimaryKey(TGames record) {

// TODO Auto-generated method stub
int updateByPrimaryKey = tgamesMapper.updateByPrimaryKey(record);
return updateByPrimaryKey;
}
}

3、JSP page

(1) Index.jsp【 Set the default open page 】

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String path=request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<script type="text/javascript"> window.location.href="<%=basePath%>/tGamesList.do"; </script>
</body>
</html>

(2) games.jsp【 The main page 】

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<style> .wrap{
 width: 80%; margin: 0 auto; } h1 {
 width:40%; margin:0 auto; } a{
 text-decoration: none; } p{
 text-align: right; } h2{
 position:relative; left:40%; } tr:hover{
 background: orange; } img{
 width:100%; height:100%; } </style>
</head>
<body>
<div style="width: 70%;margin: 0 auto;">
<h2>2021 Annual secret room escape theme ranking </h2>
<fieldset>
<legend> Search for </legend>
<div style="margin:10px">
<form action="tGamesList.do" >
Subject name :<input type="text" name="keyword" id="keyword" value="" />
type :<select name="type">
<option selected="selected" value=""> Please select </option>
<option value=" Terror "> Terror </option>
<option value=" Spy war "> Spy war </option>
<option value=" Solve the mystery "> Solve the mystery </option>
</select>
<input type="submit" value=" Search for "/>
<a href="addGames.do"><input type="button" value=" newly added "/></a>
</form>
</div>
</fieldset>
<hr/>
<table width="100%" border="1px" cellpadding="5" cellspacing="0">
<tr style="background-color: gray;">
<td width="10%" align="center" > Number </td>
<td width="15%" align="center"> Subject name </td>
<td width="10%" align="center"> Subject category </td>
<td width="20%" align="center"> Producer </td>
<td width="15%" align="center"> On-line time </td>
<td width="10%" align="center"> Number of votes </td>
<td align="center" width="15%"> operation </td>
</tr>
<c:forEach items="${selectAll}" var="games">
<tr align="center">
<td>
${games.id}
</td>
<td>
${games.gamesName}
</td>
<td>
${games.typeName}
</td>
<td>
${games.producers}
</td>
<td>
${games.uptime}
</td>
<td>
${games.votes}
</td>
<td>
<a href="#" onclick="delAccount(${games.id},${games.votes})"> vote </a>&nbsp;&nbsp;&nbsp;&nbsp;
</td>
</tr>
</c:forEach>
<tr style="text-align: center;background-color: white;">
<td colspan="6"></td>
<td>
total ${selectAll.size()} Data
</td>
</tr>
</table>
</div>
</body>
<script src="<%=request.getContextPath()%>/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript"> function delAccount(id,votes) {
 if(confirm(" Are you sure to vote ?")){
 location.href="updateGamesDo.do?id="+id+"&votes="+votes; } } </script>
</html>

(3) addGames.jsp【 Add a page 】

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css"> body{
 width: 100%; } .wrap_table{
 width: 40%; margin: 0 auto; text-align: center; } table{
 text-align: center; margin: 0 auto; } </style>
</head>
<body>
<div class="wrap_table">
<h2> New secret room escape theme </h2>
<form action="addGamesDo.do" method="post">
<table border="1" cellspacing="" cellpadding="">
<tr>
<td> name :</td>
<td><input type="text" name="gamesName" id="name" value="" /></td>
<tr>
<td> type </td>
<td>
<select name="gamesType">
<option selected="selected" value=""> Please select </option>
<option value="1"> Terror </option>
<option value="2"> Spy war </option>
<option value="3"> Solve the mystery </option>
</select>
</td>
</tr>
<tr>
<td> Producer :</td>
<td><input type="text" name="producers" id="producers" value="" /></td>
</tr>
<tr>
<td> On-line time :</td>
<td><input type="date" name="uptime" id="number" value="" /></td>
</tr>
<tr>
<td colspan="2">
<input type="button" name="" id="button" onclick="addAccount()" value=" determine " />
<input type="reset" name="" id="" value=" Reset " />
</td>
</tr>
</table>
</form>
</div>
<script type="text/javascript"> function addAccount() {
 var name = document.getElementById("name").value; var number = document.getElementById("number").value; var money = document.getElementById("producers").value; var button = document.getElementById("button"); console.log(button) if(name==""){
 alert(" Name cannot be empty !"); return false; }else if(number==""){
 alert(" Date cannot be empty !"); return false; }else if(money==""){
 alert(" The producer cannot be empty !"); return false; }else{
 button.setAttribute("type","submit"); } } </script>
</body>
</html>

(4) web.xml【xml To configure 】

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>gamesDB</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--spring Containers -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- Monitor , load spring To configure -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Front controller -->
<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/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Set up post Requested character encoding filter -->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Please bring the original link to reprint ,thank
Similar articles

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14