springboot+thymeleaf实现学生信息管理系统
1.https://spring.io/
·
b站视频讲解地址:https://www.bilibili.com/video/BV1Xy4y1a73k
链接:项目百度网盘地址:https://pan.baidu.com/s/1bBI5YwxgW1dKjVEFN4OWmQ
提取码:rjnb
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中实现刚写的查询方法
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
更多推荐
已为社区贡献3条内容
所有评论(0)