SpringBoot 搭建第一个小项目---足球俱乐部管理系统


写在前面

前文已经讲了用SSM框架来实现这个项目,但配置过程过于繁琐。使用springboot可以不用配置太多。

搭建项目框架

新建Spring Initiallizr

在这里插入图片描述

勾选所需依赖即可

在这里插入图片描述

项目结构

在这里插入图片描述

配置

在application.properties文件里配置数据源和mybatis

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT

#mybayis 配置

#别名
mybatis.type-aliases-package=com.example.demo.entity 
#映射文件
mybatis.mapper-locations=classpath:mapper/*.xml
#下划线转驼峰
mybatis.configuration.map-underscore-to-camel-case=true

前期准备

建表

在这里插入图片描述

建立entity,dao层(接口),service层(接口+实现类).

entity层:

package com.example.demo.entity;

public class Club {
    private Integer id;
    private String name;
    private String loc;
    private String stadium;
    private Integer rank;
    private Integer score;

    @Override
    public String toString() {
        return "Club{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", loc='" + loc + '\'' +
                ", stadium='" + stadium + '\'' +
                ", rank=" + rank +
                ", score=" + score +
                '}';
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public String getStadium() {
        return stadium;
    }

    public void setStadium(String stadium) {
        this.stadium = stadium;
    }

    public Integer getRank() {
        return rank;
    }

    public void setRank(Integer rank) {
        this.rank = rank;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }
}

Dao层:(纯接口)采用mapper代理

package com.example.demo.dao;

import com.example.demo.entity.Club;
import org.springframework.stereotype.Repository;

import java.util.List;

public interface ClubDao {
    //数据库基本操作

    /**
     * 添加球队记录
     * @param club
     * @throws Exception
     */
    void addClub(Club club) throws Exception;

    /**
     * 修改一条球队
     * @param club
     * @throws Exception
     */
    void updateClub(Club club) throws Exception;

    /**
     * 删除一条球队记录
     * @param id
     * @throws Exception
     */
    void deleteClub(int id) throws Exception;

    /**
     * 根据主键查询用户信息
     * @param id
     * @return
     * @throws Exception
     */
    Club getClubById(int id) throws Exception;

    /**
     * 查询所有用户信息
     * @return
     */
    List<Club> getClubs();
}

Service层:

ClubService.java(接口):

package com.xkp.ssm.service;

import com.github.pagehelper.PageInfo;
import com.xkp.ssm.entity.Club;
public interface ClubService {
    void addClub(Club club) throws Exception;
    void updateClub(Club club) throws Exception;
    void deleteUser(int id) throws Exception;
    Club getClubById(int id) throws Exception;
    /**
     * 分页查询
     */
    PageInfo<Club> getClubs(int page, int pageSize) throws  Exception;

}

ClubServiceImpl.java(接口的实现类):

package com.xkp.ssm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xkp.ssm.dao.ClubDao;
import com.xkp.ssm.entity.Club;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ClubServiceImpl implements ClubService {
    @Autowired
    private ClubDao clubDao;

    @Override
    public void addClub(Club club) throws Exception {
        clubDao.addClub(club);
    }

    @Override
    public void updateClub(Club club) throws Exception {
        clubDao.updateClub(club);
    }

    @Override
    public void deleteUser(int id) throws Exception {
        clubDao.deleteClub(id);
    }

    @Override
    public Club getClubById(int id) throws Exception {
        return clubDao.getClubById(id);
    }

    @Override
    public PageInfo<Club> getClubs(int page, int pageSize) throws Exception {
        PageHelper.startPage(page,pageSize);
        List<Club> clubList = clubDao.getClubs();
        PageInfo<Club> pageInfo = new PageInfo<>(clubList);
        return pageInfo;
    }
}

Mybatis映射文件

ClubMapper.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.xkp.ssm.dao.ClubDao">

    <insert id="addClub" parameterType="club">
        insert into tb_club(tb_club.name,tb_club.loc,tb_club.stadium,tb_club.rank,tb_club.score) values(#{name},#{loc},#{stadium},#{rank},#{score})
    </insert>

    <delete id="deleteClub" parameterType="int">
        delete from tb_club where id=#{id}
    </delete>

    <update id="updateClub" parameterType="club">
        update tb_club set tb_club.name=#{name},tb_club.loc=#{loc},tb_club.stadium=#{stadium},tb_club.rank=#{rank},tb_club.score=#{score}
        where tb_club.id=#{id}
    </update>

    <select id="getClubById" parameterType="int" resultType="club">
        SELECT
        tb_club.id,
        tb_club.name,
        tb_club.loc,
        tb_club.stadium,
        tb_club.rank,
        tb_club.score
        FROM
        tb_club
        where
        tb_club.id= #{id}
    </select>

    <select id="getClubs" resultType="club">
        SELECT
        tb_club.id,
        tb_club.name,
        tb_club.loc,
        tb_club.stadium,
        tb_club.rank,
        tb_club.score
        FROM
        tb_club
        order by tb_club.rank asc
   </select>
</mapper>

前端页面

基本和之前的ssm框架的jsp页面一样,不同之处在于把el表达式换位thmyleaf的表达形式。这里展出一个文件

src/main/resources/templates/ClubList.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>列表</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>

<div class="row">
    <div class="col-md-3">

    </div>
    <div class="col-md-6">
        <h1>俱乐部信息</h1>
        <a href="toAdd"><span class="btn-info">新增俱乐部</span></a>
        <table class="table table-condensed table-bordered table-hover">
            <tr>
                <th>排名</th>
                <th>球队</th>
                <th>城市</th>
                <th>主场</th>
                <th>积分</th>
                <th>操作</th>
            </tr>
                <tr th:each="club : ${pageInfo.list}">
                    <td th:text="${club.rank}"></td>
                    <td th:text="${club.name}"></td>
                    <td th:text="${club.loc}"></td>
                    <td th:text="${club.stadium}"></td>
                    <td th:text="${club.score}"></td>
                    <td>
                        <a href="#"><span class="btn btn-danger btn-small">删除</span></a>
                        <a href="#"><span class="btn btn-success btn-small">修改</span></a>
                    </td>
                </tr>

        </table>
<!--        分页-->
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <li>
                    <a href="#" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>

                <li class="active"><a href="#">1</a></li>

                <li>
                    <a href="#" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </ul>
        </nav>

    </div>
    <div class="col-md-3"></div>
</div>
</body>
</html>

编写Controller类

src/main/java/com/example/demo/controller/ClubController.java

ClubController.java

package com.example.demo.controller;

import com.example.demo.entity.Club;
import com.example.demo.service.ClubService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/club")
public class ClubController {

    @Autowired
    private ClubService clubService;
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int pageSize) throws Exception {
        PageInfo<Club> pageInfo = clubService.getClubs(1,10);
        model.addAttribute("pageInfo",pageInfo);
        return "ClubList";
    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "ClubAdd";
    }
    @RequestMapping("/addclub")
    public String addClub(Club club) throws Exception {
        clubService.addClub(club);
        return "redirect:list";
    }
}

效果展示

同SSM框架的结果

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值