ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot+thymeleaf实现学生信息管理系统

2021-03-07 10:29:17  阅读:116  来源: 互联网

标签:springboot student import springframework thymeleaf Student org 信息管理系统 public


1.https://spring.io/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.
在这里插入图片描述
3.idea中打开项目
在这里插入图片描述
4.配置application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml  
# 注意:对应实体类的路径
mybatis.type-aliases-package=com.rj.stu.stuManagement.entity

在这里插入图片描述
5.在pom文件末尾新增mybatis generator的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.rj.stu</groupId>
	<artifactId>stuManagement</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>stuManagement</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.4</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.1</version>
				<configuration>
					<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
					<verbose>true</verbose>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>${mysql.version}</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

</project>

在这里插入图片描述
5.按照刚刚上一张图中紫色框住的文件路径去新建一个generator文件夹,并在generator文件夹下新建generatorConfig.xml文件。(千万注意不要拼写错误)
在这里插入图片描述
6.generator.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="C:\Program Files (x86)\MySQL\Connector.J 5.1\mysql-connector-java-5.1.40-bin.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
        <!--数据库连接驱动类,URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/demo" userId="root" password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成(实体)模型的包名和位置-->
        <javaModelGenerator targetPackage="com.rj.stu.stuManagement.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成XML映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="resources.mapper" targetProject="src/main">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO接口的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.rj.stu.stuManagement.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="stu" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

红框位置是你需要修改的
在这里插入图片描述
在这里插入图片描述
7.配置generator maven来生成mybatis
在这里插入图片描述
在这里插入图片描述
加上这一行mybatis-generator:generate -e
在这里插入图片描述
8.选择刚刚新建的maven generator来运行
在这里插入图片描述
9.如果生成了以下红框部分就说明成功了
在这里插入图片描述
10.在StudentMapper中新增一个查询方法

package com.rj.stu.stuManagement.mapper;

import com.rj.stu.stuManagement.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
@Mapper
public interface StudentMapper {
//   删除
    int deleteByPrimaryKey(Integer sno);
//  新增
    int insert(Student record);
//
    int insertSelective(Student record);
//按照学号查询
    Student selectByPrimaryKey(Integer sno);
//更新
    int updateByPrimaryKeySelective(Student record);
//
    int updateByPrimaryKey(Student record);

//    查询
    List<Student> QueryAll();
}

11.然后在StudentMapper.xml中实现刚写的查询方法
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210307101131386.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzcxOTYxNg==,size_16,color_FFFFFF,t_70
12.新建service包来实现mapper
在这里插入图片描述
13.StuInterface

package com.rj.stu.stuManagement.service;

import com.rj.stu.stuManagement.entity.Student;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public interface StuInterface {
//    查询
    List<Student> QuertAll();
//    按照学号来查询学生信息
    Student selectByPrimaryKey(Integer sno);
//    新增
    boolean Add(Student student);
//    删除
    boolean Del(int sno);
//    修改
    boolean Update(Student student);
}

14.StuImpl

package com.rj.stu.stuManagement.service;

import com.rj.stu.stuManagement.entity.Student;
import com.rj.stu.stuManagement.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StuImpl implements StuInterface{
    @Autowired
    private StudentMapper studentMapper;
//    查询
    public List<Student> QuertAll() {
        List<Student> students = studentMapper.QueryAll();
        return students;
    }

    //    按照学号来查询学生信息
    public Student selectByPrimaryKey(Integer sno) {
        Student student = studentMapper.selectByPrimaryKey(sno);
        return student;
    }

    //新增
    public boolean Add(Student student) {
        int i = studentMapper.insert(student);
        if(i>0)
            return true;
        else
          return false;
    }
//删除
    public boolean Del(int sno) {
        int i = studentMapper.deleteByPrimaryKey(sno);
        if(i>0)
            return true;
        else
            return false;
    }
// 更新
    public boolean Update(Student student) {
        int i = studentMapper.updateByPrimaryKey(student);
        if(i>0)
            return true;
        else
            return false;
    }
}

15.写controller类。创建controller包并新建StuController文件

package com.rj.stu.stuManagement.controller;

import com.rj.stu.stuManagement.entity.Student;
import com.rj.stu.stuManagement.service.StuImpl;
import com.rj.stu.stuManagement.service.StuInterface;
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 org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class StuController {
    @Autowired
    private StuInterface stuInterface;
//    查询
    @RequestMapping("/Display")
    public ModelAndView Display(){
        ModelAndView modelAndView = new ModelAndView();
        List<Student> studentList = stuInterface.QuertAll();
        modelAndView.addObject("students",studentList);
        modelAndView.setViewName("Display");
        return modelAndView;
    }
//    删除
    @RequestMapping("/del")
    public ModelAndView del(HttpServletRequest request){
        int sno = Integer.parseInt(request.getParameter("sno"));
        stuInterface.Del(sno);
        return new ModelAndView("redirect:/Display");
    }
//    新增
    @GetMapping("/addPage")
    public ModelAndView addPage(Model model){
        model.addAttribute("student",new Student());
        return new ModelAndView("add","stumodel",model);
    }
    @PostMapping("/add")
    public ModelAndView add(Student student){
        stuInterface.Add(student);
        return new ModelAndView("redirect:/Display");
    }
//    更新
    @GetMapping("/sendsno")
    public ModelAndView sendsno(HttpServletRequest request,Model model){
        int sno = Integer.parseInt(request.getParameter("sno"));
        Student student = stuInterface.selectByPrimaryKey(sno);
        model.addAttribute("student",student);
        return new ModelAndView("Edit","stumodel",model);
    }
    @PostMapping("/update")
    public ModelAndView update(Student student){
        System.out.println(student);
        stuInterface.Update(student);
        return new ModelAndView("redirect:/Display");
    }
}

16.在template下新建这几个文件
在这里插入图片描述
Display.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>学生信息展示</title>
</head>
<body>
    <table border="1px">
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>地址</th>
            <th>密码</th>
            <th>操作</th>
        </tr>
        <tr th:each="students:${students}">
            <td th:text="${students.sno}"></td>
            <td th:text="${students.sname}"></td>
            <td th:text="${students.sage}"></td>
            <td th:text="${students.saddress}"></td>
            <td th:text="${students.spwd}"></td>
            <td>
                <a th:href="@{sendsno(sno=${students.sno})}">更新</a>
                <a th:href="@{del(sno=${students.sno})}">删除</a>
            </td>
        </tr>
        <a href="/addPage">新增</a>
    </table>

</body>
</html>

add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>新增</title>
</head>
<body>
    <form action="/add" method="post" th:object="${stumodel.student}">
        学号:<input type="text" name="sno" th:value="*{sno}">
        姓名:<input type="text" name="sname" th:value="*{sname}">
        年龄:<input type="text" name="sage" th:value="*{sage}">
        地址:<input type="text" name="saddress" th:value="*{saddress}">
        密码:<input type="text" name="spwd" th:value="*{spwd}">
        <input type="submit" value="添加">
    </form>
</body>
</html>

Edit.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>编辑</title>
</head>
<body>
<form action="/update" method="post" th:object="${stumodel.student}">
    学号:<input type="text" name="sno" th:value="*{sno}" readonly>
    姓名:<input type="text" name="sname" th:value="*{sname}">
    年龄:<input type="text" name="sage" th:value="*{sage}">
    地址:<input type="text" name="saddress" th:value="*{saddress}">
    密码:<input type="text" name="spwd" th:value="*{spwd}">
    <input type="submit" value="更新">
</form>
</body>
</html>

over

标签:springboot,student,import,springframework,thymeleaf,Student,org,信息管理系统,public
来源: https://blog.csdn.net/weixin_43719616/article/details/114477359

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有