当前位置 博文首页 > h295928126的博客:基于springboot+Mybatis+thymeleaf的学生选题

    h295928126的博客:基于springboot+Mybatis+thymeleaf的学生选题

    作者:[db:作者] 时间:2021-07-27 21:05

    一、前言
    1、本篇只提供一个最基础的架构,需要更完美的可以自己去优化。
    2、这篇题目虽然是选题系统,但是可以举一反三地使用,选X系统系列都是适用的。
    看这篇文章的前提是对springboot有一定的基础。没学过springboot的同学可以去看下翟永超大哥的教程,这里我给个网址:

    http://blog.didispace.com/spring-boot-learning-1/

    springboot整合mybatis也可以去上面网站看相关文章

    3、让我们先看下这个系统的实现画面

    a、系统首页:
    这里写图片描述


    b、选题成功画面:
    这里写图片描述


    c、选题失败画面:
    这里写图片描述

    这里写图片描述

    二、首先建好数据库
    这里写图片描述

    三、打开IDAE

    1、新建一个springboot项目,勾上这俩个选项即可。
    这里写图片描述

    2、搭好项目架构
    这里写图片描述

    3、在pom.xml中加入mybatis和mysql的依赖。以及mapper.xml文件的读取路径。

    pom.xml

    <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.21</version>
            </dependency>
    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
    <!-- xml路径-->
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
            </resources>
        </build>

    4、application.properties写好datasource

    #填自己的数据库信息
    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    #mybatis的路径
    mybatis.mapperLocation=classpath*:com/**/*Mapper.xml

    5、控制层(网络层)controller包创建UserController类

    UserController

    @Controller
    @RequestMapping("")
    public class UserController {
        @Autowired
        private TopicService topicService;
    
        //选题系统首页
        @RequestMapping(value = "/topic",method = RequestMethod.GET)
        public String getTopicAll(Model model){
            List<Topic> topics=topicService.findAll();
            model.addAttribute("topics",topics);
            return "topic";
        }
        //检查选题的逻辑
        @RequestMapping(value = "/check",method = RequestMethod.GET)
        public String checkTopic(HttpServletRequest request){
            String id=request.getParameter("id");
            String number=request.getParameter("number");
            Topic topic=new Topic();
            topic.setId(id);
            topic.setNumber(number);
            String result=topicService.check(topic);
            return result;
        }
    
    
    }

    6、实体层model包中创建Topic类

    package com.model;
    
    /**
     * Created by Administrator on 2016/12/9.
     */
    public class Topic {
        private String id ;
        private String  name;
        private String  teacher;
        private String number;
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public String getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        public String getTeacher() {
            return teacher;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setTeacher(String teacher) {
            this.teacher = teacher;
        }
    }
    

    7、数据库层mapper包创建TopicMapper接口

    package com.mapper;
    
    import com.model.Topic;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    /**
     * Created by Administrator on 2016/12/9.
     */
    @Mapper
    public interface TopicMapper {
       List<Topic> findAll();
       Topic findById(String id);
       void update(Topic topic);
    }

    然后在同个包下创建TopicMapper.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.mapper.TopicMapper" >
      <resultMap id="TopicResultMap" type="com.model.Topic">
        <id column="id"  property="id" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="teacher" property="teacher" jdbcType="INTEGER"/>
        <result column="number" property="number" jdbcType="INTEGER"/>
    
      </resultMap>
      <select id="findAll" parameterType="String" resultMap="TopicResultMap">
        select * from topic
      </select>
    
      <select id="findById" parameterType="String" resultMap="TopicResultMap">
        select * from topic WHERE id=#{id}
      </select>
    
      <update id="update" parameterType="com.model.Topic">
        UPDATE topic set number=#{number,jdbcType=VARCHAR}
        where id=#{id,jdbcType=VARCHAR}
      </update>
    </mapper>

    8、业务逻辑层service包创建TopicService接口

    package com.service;
    
    import com.model.Topic;
    
    import java.util.List;
    
    /**
     * Created by Administrator on 2016/12/9.
     */
    public interface TopicService {
        List<Topic> findAll();
        String check(Topic topic);
    }
    

    同个包创建TopicServiceImpl类

    package com.service;
    
    import com.mapper.TopicMapper;
    import com.model.Topic;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * Created by Administrator on 2016/12/9.
     */
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Service("topicService")
    public class TopicServiceImpl implements TopicService {
    
        @Autowired
        private TopicMapper topicMapper;
        @Override
        public List<Topic> findAll() {
            return topicMapper.findAll();
        }
    
        //提交选题时的逻辑
        @Override
        public String check(Topic topic) {
            Topic topic1=topicMapper.findById(topic.getId());
            //如果提交的俩个属性为空
            if (topic1==null||topic1.getNumber().equals("")){
                //返回iderror.html
                return "iderror";
            }
            if ((!topic1.getNumber().equals("0"))||topic.getNumber().equals("0")){
                return "numbererror";
            }
            topicMapper.update(topic);
            return "success";
        }
    }
    

    9、创建html页面,在resources文件下的templates创建以下4个html

    topic.html

    
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
          xmlns:text-align="http://www.w3.org/1999/xhtml">
    <head>
        <title>简单粗暴选题系统!</title>
        <link href="css/main.css" rel="stylesheet"  type="text/css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="X-UA-COMPATIBLE" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    
    </head>
    <body>
    <h1>简单粗暴选题系统!</h1>
    <h2>题目列表</h2>
    
    <table width="400" border="1" cellspacing="0" cellpadding="0">
        <thead>
        <tr>
            <th width="105">序号</th>
            <th width="105">题目</th>
            <th width="105">老师</th>
            <th width="105">已选学号(0为未有人选)</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="topic:${topics}">
            <td width="105"   th:text="${topic.id}">序号</td>
            <td width="105"  th:text="${topic.name}">题目</td>
            <td width="105"  th:text="${topic.teacher}">老师</td>
            <td width="105"  th:text="${topic.number}">已选学号</td>
        </tr>
        </tbody>
    </table>
        <form action="/check" method="get">
        <p>选择你要选的题目的序号: <input type="text" name="id" /></p>
        <p>你的学号(比如二班1号:“2-1”): <input type="text" name="number" /></p>
        <input type="submit" value="确认" />
    </form>
    
    </body>
    </html>
    

    success.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/html">
    <head>
        <meta charset="UTF-8"/>
        <title>Title</title>
    </head>
    <body>
    <h1>恭喜您选题成功!!</h1>
    <a>您选到的题目序号是</a>
    <a th:text="${id}"></a>
    <a></a><br/>
    <a href="/topic">点此返回首页</a>
    </body>
    </html>

    iderror.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>Title</title>
    </head>
    <body>
    <h1>抱歉!你选的序号不存在!或者是你没填学号!</h1><br/>
    <a href="/topic">点此返回首页</a>
    </body>
    </html>

    numbererror.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta