1. 程式人生 > >基於springboot+Mybatis+thymeleaf的學生選題系統超級簡易版

基於springboot+Mybatis+thymeleaf的學生選題系統超級簡易版

一、前言
1、本篇只提供一個最基礎的架構,需要更完美的可以自己去優化。
2、這篇題目雖然是選題系統,但是可以舉一反三地使用,選X系統系列都是適用的。
看這篇文章的前提是對springboot有一定的基礎。沒學過springboot的同學可以去看下翟永超大哥的教程,這裡我給個網址:

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 charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<h1>抱歉!此題已有人選了!或者是不能填0</h1><br/>
<a href="/topic">點此返回首頁</a>
</body>
</html>

四、跑起來就顯示我開頭展示的畫面了,以後有機會看能不能把這個系統完善到功能跟展示頁面都是棒棒的。