springboot学生表

该博客介绍了如何使用SpringBoot结合Java实现对学生表的操作,包括配置pom.xml和application.yml,创建StudentController、StudentDao、Service等类,以及HTML页面进行CRUD操作。涉及到Ajax交互和MySQL数据库的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom.xml

<?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.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository --><!--从存储库查找父类-->
    </parent>
    <groupId>com.sdbairui</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <kotlin.version>1.3.72</kotlin.version>
    </properties>

    <dependencies><!--存储库_总库-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --><!--下载网址-->
        <dependency><!--存储库-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.2.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <optional>true</optional>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- java 与json 数据转换 -->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>testCompile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

Spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/1901java?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
    #    修改时区serverTimezone=UTC
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
#   限制上传设置
#  servlet:
#    multipart:
#      max-file-size: 50MB
#      max-request-size: 100MB
#      enabled: true
Server:
#  tomcat:
#    port-header: X-ForWarded-Port
#    protocol-header: x-forwarded-proto
#    remote-ip-header: x-forwarded-for
#    max-http-post-size: -1   #tomcat的POST请求不限制大小
#    use-forward-headers: true
  post:8080

StudentController.java

package com.sdbairui.demo.Controller;

import com.sdbairui.demo.Entity.Classes;
import com.sdbairui.demo.Entity.Student;
import com.sdbairui.demo.Service.ClassesService;
import com.sdbairui.demo.Service.StudentService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired      //自动注入学生服务层
    StudentService studentService;
    @Autowired      //自动注入班级服务层
    ClassesService classesService;
    /*
    * 查看所有学生信息
    * @param model
    * @return
    * */
    @RequestMapping("/toIndex")     //表连接查询全部
    public String toIndex(HttpServletRequest request,Model model){
        request.getParameter("sname");
        List<Map<String,Object>> list = studentService.findAllData();
        model.addAttribute("student",list);
        return "index2";
    }
    /*
    * 添加学生信息
    * @param name
    * @param password
    * @param phone
    * @return
    * */
    @RequestMapping("/toAdd")       //跳转添加(附带下拉框查询)
    public String toAdd(Model model){
        List<Classes> list =classesService.findAll();
        model.addAttribute("classes",list);
        return "Student_add";
   }
   /*
   * 显示添加页面
   * @return
   *
   * */
   @RequestMapping("/doAdd")        //执行添加操作
    public String doAdd(HttpServletRequest request)throws Exception{
       request.setCharacterEncoding("UTF-8");
       Student student = new Student();
       student.setSname(request.getParameter("sname"));
       student.setSscore(Integer.parseInt(request.getParameter("sscore")));
       student.setSbirthday(new SimpleDateFormat("yyy-MM-dd").parse(request.getParameter("sbirthday")));
       student.setScid(Integer.parseInt(request.getParameter("scid")));
       student.setSfile(request.getParameter("sfile"));
//2:
//       String sname = request.getParameter("sname");
//       int sscore = Integer.parseInt(request.getParameter("sscore"));
//       Date date = new SimpleDateFormat("yyy-MM-dd").parse(request.getParameter("sbirthday"));
//       int scid = Integer.parseInt(request.getParameter("scid"));
//       String sfile = request.getParameter("sfile");
//       student.setSname(sname);
//       student.setSscore(sscore);
//       student.setSbirthday(date);
//       student.setScid(scid);
//       student.setSfile(sfile);
        studentService.doCreate(student);
        return "redirect:/student/PageList";
   }
   /*
   *删除学生信息
   *
   *
   * */
   @RequestMapping("/doDelete")     //执行删除操作
    public String doDelete(HttpServletRequest request){
       int sid = Integer.parseInt(request.getParameter("sid"));
       Student student = new Student();
       student.setSid(sid);
       studentService.doDelete(student);
       return "redirect:/student/PageList";//redirect重定向
   }
   /*
   * 模糊查询
   *
   *
   * */
   @RequestMapping("/findAllBySnameLike")       //执行模糊查询操作
    public String findAllBySnameLike(HttpServletRequest request,Model model){
       String sname = request.getParameter("sname");
       List<Map<String,Object>> list = studentService.findAllBySnameLike(sname);
       model.addAttribute("student",list);
       return "index";
   }
   /*
   * 修改学生信息
   *
   *
   * */
   @RequestMapping("/toUpdate")     //跳转修改操作
    public String toUpdate(HttpServletRequest request,Model model){
      int sid = Integer.parseInt(request.getParameter("sid"));
       Student student = studentService.findBySid(sid);
       model.addAttribute("student",student);
       List<Classes> list =classesService.findAll();
       model.addAttribute("classes",list);
       return "Student_update";
   }
   @RequestMapping("/doUpdate")     //执行修改操作
    public String doUpdate(HttpServletRequest request) throws ParseException {
       int sid = Integer.parseInt(request.getParameter("sid"));
       String sname = request.getParameter("sname");
       int sscore = Integer.parseInt(request.getParameter("sscore"));
       Date date = new SimpleDateFormat("yyy-MM-dd").parse(request.getParameter("sbirthday"));
       int scid = Integer.parseInt(request.getParameter("scid"));
       studentService.doUpadte(sname,sscore,date,scid,sid);
       return "redirect:/student/PageList";
//       return "redirect:/student/toIndex";//redirect重定向

   }

   @RequestMapping("/PageList")     //分页
    public String findAllDatapage(HttpServletRequest request,Model model,Integer pageNum,String sname){
       if (pageNum==null){
           pageNum=1;
       }
       request.getParameter("sname");
       if (sname==null){
           sname="";
       }
       Pageable pageable = PageRequest.of(pageNum -1,5);
       Page<List<Map>> page = studentService.findAllDataPage(pageable,sname);
       model.addAttribute("Pageinfo",page);
       return "index2";
   }

    public static void uploadFile(byte[] file,String filePath,String fileName) throws Exception{        //上传磁盘文件
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(filePath+"/" + fileName);
        fileOutputStream.write(file);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

   @RequestMapping(value = "/upload",method = RequestMethod.POST)
   @ResponseBody
    public JSONObject uploadImg(@RequestParam("file")MultipartFile file, Model model){
       JSONObject jo = new JSONObject();
       String fileName = System.currentTimeMillis()+file.getOriginalFilename();
       String filePath = "D:/Users/16350/IdeaProjects/SpringBootDemo1/src/main/resources/uploadImg/";
       if (file.isEmpty()){
           jo.put("success",0);
           jo.put("fileName","");
       }
       try{
           uploadFile(file.getBytes(),filePath,fileName);
           jo.put("success",1);
           jo.put("filename",fileName);
           jo.put("xfilename","/imctemp-rainy/"+fileName);
       }catch(Exception e){
           e.printStackTrace();
       }
       model.addAttribute("xfilename",filePath+fileName);
       return jo;
   }
}

StudentDao.java

package com.sdbairui.demo.Dao;


import com.sdbairui.demo.Entity.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;



import java.util.Date;
import java.util.List;
import java.util.Map;

public interface StudentDao extends JpaRepository<Student,Integer> {

    @Query("from student")
    List<Student> findAll();

    @Query(value = "select * from student s inner join classes c on s.scid = c.cid",nativeQuery = true)      //定义sql语句
    List<Map<String,Object>> findAllData();     //表连接显示

    @Query(value="select sid,sname,sscore,sbirthday,scid from student where sid = :sid",nativeQuery = true)
    Student findAllBySid(@Param("sid")int sid);

    @Query(value = "select * from student s inner join classes c on s.scid = c.cid where sname like CONCAT('%',:sname,'%')",nativeQuery = true)
    List<Map<String,Object>> findAllBySnameLike(String sname);      //模糊查询

    @Query(value = "select * from student s inner join classes c on s.scid = c.cid",nativeQuery = true)
    Page<List<Map>> findAllDataPage(Pageable pageable,String sname);        //分页表连接

    @Query(value = "select sid,sname,sscore,sbirthday,scid,cname from student s inner join classes c on s.scid = c.cid where sid = :sid",nativeQuery = true)
    Student findBySid(@Param("sid")int sid);        //根据编号查询

    @Transactional
    @Modifying
    @Query(value = "update student set sname = ?1,sscore = ?2,sbirthday = ?3,scid = ?4 where sid = ?5",nativeQuery = true)
    int doUpdate(String sname,int sscore,Date sbirthday,int scid,int sid);      //修改方法

}
        //@Transactional
        //一、使用范围
        //@Transactional注解 可以作用于接口、接口方法、类以及类方法上。
        //
        // 当标于类前时, 标示类中所有方法都进行事物处理
        //当用作类上时,该类上的所有 public 方法将都具有该类型的属性。
        //当用作方法上是,该方法所在类上的注解将失效,该注解只能应用在 public 方法上。
        //不建议用在接口或接口方法上,因为这只有在使用基于接口的动态代理是才会生效
        //!注意:只有来自外部的方法调用才会引起事务行为,类内部方法调用本类内部的其他方法并不会引起事务行为。
        //
        //二、注解属性
        //value属性:当配置多个事务管理器时,可以使用该属性指定选择哪个事务管理器。
        //propagation属性:用于指定事务的传播行为、默认值为 REQUIRED。

        //@Modifying
        //(1)可百以通过自定义的 JPQL 完成 UPDATE 和 DELETE 操作。
        //注意: JPQL 不支持使用 INSERT;度
        //(2)回在 @Query 注解中编写 JPQL 语句, 但必须使用 @Modifying 进行修饰. 以通知   SpringData, 这是一个 UPDATE 或 DELETE 操作
        //(3)UPDATE 或 DELETE 操作需要使用事务,此时需要定义 Service 层,在 Service 层的方法上添加事务操作;
        //(4)默认情况下, SpringData 的每个方法上有事务, 但都是一个只读事务。
        //他们答不能完成修改操作。

ClassesDao.java

package com.sdbairui.demo.Dao;

import com.sdbairui.demo.Entity.Classes;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ClassesDao extends JpaRepository<Classes,Integer> {

}

Classes

package com.sdbairui.demo.Entity;

import javax.persistence.*;

@Table
@Entity(name = "classes")
public class Classes {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int cid;
    private String cname;

    public int getCid(){
        return cid;
    }
    public String getCname(){
        return cname;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
}

Student.java

package com.sdbairui.demo.Entity;


import javax.persistence.*;
import java.util.Date;

@Table      //表
@Entity(name = "student")       // 表名

public class Student {
    @Id     //主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)     //自增
    private int sid;
    private String sname;
    private int sscore;
    private Date sbirthday;
    private int scid;
    private String sfile;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getSscore() {
        return sscore;
    }

    public void setSscore(int sscore) {
        this.sscore = sscore;
    }

    public Date getSbirthday() {
        return sbirthday;
    }

    public void setSbirthday(Date sbirthday) {
        this.sbirthday = sbirthday;
    }

    public int getScid() {
        return scid;
    }

    public void setScid(int scid) {
        this.scid = scid;
    }

    public String getSfile() {
        return sfile;
    }

    public void setSfile(String sfile) {
        this.sfile = sfile;
    }
}

ClassesService.java

package com.sdbairui.demo.Service;

import com.sdbairui.demo.Dao.ClassesDao;
import com.sdbairui.demo.Entity.Classes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service        //业务层
public class ClassesService {
    @Resource       //自动注入
    ClassesDao classesDao;

    public List<Classes> findAll(){
        return classesDao.findAll();
    }
}

StudentService.java

package com.sdbairui.demo.Service;

import com.sdbairui.demo.Dao.StudentDao;
import com.sdbairui.demo.Entity.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service        //业务层
public class StudentService {
    @Resource       //自动注入
    StudentDao studentDao;

    public List<Student> findAll(){
        return studentDao.findAll();
    }
    public List<Map<String,Object>> findAllData(){
        return studentDao.findAllData();
    }
    public Student doCreate(Student student){
        return studentDao.save(student);
    }
//    public void doCreate(Student student){
//        studentDao.save(student);
//    }同上

//    public Student doUpdate(Student student){
//        return student.save(student);
//    }
    public List<Map<String,Object>>  findAllBySnameLike(String sname){
        return studentDao.findAllBySnameLike(sname);
    }

    public void doDelete(Student student){
        studentDao.delete(student);
    }
    public Student findBySid(int sid){
//        Student student =new Student();
//        student = studentDao.findAllBySid(sid);
//        return student;
        return studentDao.findBySid(sid);
    }

    public void doUpadte(String sname, int sscore, Date sbirthday, int scid, int sid){
        studentDao.doUpdate(sname,sscore,sbirthday,scid,sid);
    }

    public  Page<List<Map>> findAllDataPage(Pageable pageable,String sname){
        return studentDao.findAllDataPage(pageable,sname);
    }
}

DemoApplication.java

package com.sdbairui.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication      //包含@Configuration、@EnableAutoConfiguration、@ComponentScan通常用在主类上;
public class DemoApplication implements WebMvcConfigurer {
/*    为什么继承WebMvcConfigurer接口呢?
   原因是这个接口中有addResourceHandlers方法,
   此方法是为了自定义静态资源映射路径,
   也就是说:图片将图片上传到内部磁盘当中,
   addResourceHandler这个方法通俗一点就是说将内部路径重命名在外部显示。
*/

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    //自定义静态资源映射目录
    @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*
            addResoureHandler:指的是对外暴露的访问路径
            addResourceLocations:指的是内部文件放置的目录
        */
        registry.addResourceHandler("/imctemp-rainy/**").addResourceLocations("file:D:/Users/16350/IdeaProjects/SpringBootDemo1/src/main/resources/uploadImg/");
     }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <center>
        <table border="1" width="80%">
            <tr>
                <th colspan="7">学生信息展示</th>
            </tr>
            <tr>
                <th colspan="7"><a href="/student/toAdd">添加</a></th>
            </tr>
            <tr>
                <th colspan="7">
                    <form action="/student/findAllBySnameLike">
                        <input type="text" name="sname">
                        <input type="submit" value="查询">
                    </form>
                </th>
            </tr>
            <tr>
                <td align="center">编号</td>
                <td align="center">姓名</td>
                <td align="center">成绩</td>
                <td align="center">生日</td>
                <td align="center">班级</td>
                <td colspan="2" align="center">操作</td>
            </tr>
            <tr th:each="student:${student}">
                <td th:text="${student.sid}"></td>
                <td th:text="${student.sname}"></td>
                <td th:text="${student.sscore}"></td>
                <td th:text="${student.sbirthday}"></td>
                <td th:text="${student.cname}"></td>
                <td><a th:href="@{/student/doDelete(sid=${student.sid})}">删除</a></td>
                <td><a th:href="@{/student/toUpdate(sid=${student.sid})}">修改</a></td>
            </tr>

        </table>
    </center>
</body>
</html>

index2.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<center>
    <table border="1" width="80%">
        <tr>
            <th colspan="8">学生信息展示</th>
        </tr>
        <tr>
            <th colspan="8"><a href="/student/toAdd">添加</a></th>
        </tr>
        <tr>
            <th colspan="8">
                <form action="/student/findAllBySnameLike">
                    <input type="text" name="sname">
                    <input type="submit" value="查询">
                </form>
            </th>
        </tr>
        <tr>
            <td align="center">编号</td>
            <td align="center">姓名</td>
            <td align="center">成绩</td>
            <td align="center">生日</td>
            <td align="center">班级</td>
            <td align="center">学生照片</td>
            <td colspan="2" align="center">操作</td>
        </tr>
        <tr th:each="student:${Pageinfo.content}">
            <td th:text="${student.sid}"></td>
            <td th:text="${student.sname}"></td>
            <td th:text="${student.sscore}"></td>
            <td th:text="${student.sbirthday}"></td>
            <td th:text="${student.cname}"></td>
            <td><img th:src="@{${student.sfile}}" alt="" width="100" height="100"></td>
            <td><a th:href="@{/student/doDelete(sid=${student.sid})}">删除</a></td>
            <td><a th:href="@{/student/toUpdate(sid=${student.sid})}">修改</a></td>
        </tr>
    </table>
    <ul>
        <p><span th:text="${Pageinfo.number}+1"></span>页
            总<span th:text="${Pageinfo.totalPages}"></span>页
            共<span th:text="${Pageinfo.totalElements}"></span>条数据
            <a th:href="@{/student/PageList(pageNum=1)}">首页</a>
            <a th:href="@{/student/PageList(pageNum= ${Pageinfo.hasPrevious()} ? ${Pageinfo.getNumber()} : 1)}">上一页</a>
            <a th:href="@{/student/PageList(pageNum= ${Pageinfo.hasNext()} ? ${Pageinfo.getNumber()} + 2 : ${Pageinfo.totalPages})}">下一页</a>
            <a th:href="@{/student/PageList(pageNum=${Pageinfo.totalPages})}">尾页</a>
        </p>
    </ul>
</center>
</body>
</html>

Student_add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
    <center>
        <form action="/student/doAdd" method="post" enctype="multipart/form-data">
            <table border="1">
                <tr>
                    <td>学生姓名:</td>
                    <td><input type="text" name="sname"></td>
                </tr>
                <tr>
                    <td>学生成绩:</td>
                    <td><input type="text" name="sscore"></td>
                </tr>
                <tr>
                    <td>学生生日:</td>
                    <td><input type="date" name="sbirthday"></td>
                </tr>
                <tr>
                    <td>学生班级:</td>
                    <td><select name="scid">
                        <option th:each="classes:${classes}" th:text="${classes.cname}" th:value="${classes.cid}"></option>
                    </select></td>
                </tr>
                <tr>
                    <td>学生照片:</td>
                    <td>
                        <input type="file" name="file" id="file"> <br>
                        <input type="text" name="sfile" id="sfile">
                        <p id="url"><img th:src="@{${xfilename}}" alt="" width="200" height="200"></p>
                        <input type="button" value="上传" id="upload">
                        <input type="button" value="取消" id="back">
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" value="添加">
                    </td>
                </tr>
            </table>
        </form>
    </center>
<script type="text/javascript">
    $(function (){
      $("#upload").click(function (){       //上传图片
          var form = new FormData();        //获取表单对象
          form.append("file",document.getElementById("file").files[0]);
          $.ajax({
              url: "/student/upload",       //后台url
              type: "POST",     //类型,POST或GET
              async: false,
              cache: false,
              data: form,
              dataType: 'json',     //数据返回类型,可以是xml,json等
              contentType: false,
              processData: false,
              success:function (data){
                  alert(JSON.stringify(data));
                  //成功,回调函数
                  if(data){
                      var pic = data.xfilename;
                      $("#url img").attr("src",pic);
                      $("#sfile").val(pic);
                  }else{
                      alert("失败");
                  }
              },
              error: function(er){      //失败,回调函数
                  alert(JSON.stringify(data));
              }
          })
      })
    })
</script>
</body>
</html>

Stduent_update.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <center>
    <form action="/student/doUpdate">
        学生姓名:<input type="text" name="sname" th:value="${student.sname}"><br>
        学生成绩:<input type="text" name="sscore" th:value="${student.sscore}"><br>
        学生生日:<input type="date" name="sbirthday" th:value="${student.sbirthday == null }? '' : ${ #dates.format( student.sbirthday, 'yyyy-MM-dd' ) }"><br>
        学生班级:<select name="scid" >
        <option th:each="classes:${classes}" th:selected="(${student.scid} == ${classes.cid})" th:text="${classes.cname}" th:value="${classes.cid}"></option>
    </select><br>
        <input type="hidden" name="sid" th:value="${student.sid}">
        <input type="submit" value="修改">
    </form>
    </center>
</body>
</html>

application.properties

# 场景描述:
#在写一个含有date类型字段返回给前端的接口中,postman测试发现返回的日期都比controller中的日期少了一天,datetime的日期又没有问题,纳闷了很久,查相关网友的案例,发现是springboot问题,解决方法如下,本人已解决:
#
#原因如下:
#spring-boot中对于@RestController或者@Controller+@ResponseBody注解的接口方法的返回值默认是Json格式,
#所以当对于date类型的数据,在返回浏览器端是会被spring-boot默认的Jackson框架转换,而Jackson框架默认的时区GMT(相对于中国是少了8小时)。
#
#处理方式:
#在application.properties添加配置
#
#spring.jackson.time-zone=GMT+8
#1
#如果是yml增加:
#
#spring:
#  jackson:
#    time-zone: GMT+8
#
#1
#2
#3
#4
#设置jackson的时区为东八区即可!!!
#返回给前台的时间即正确

banner.txt

${AnsiColor.BRIGHT_GREEN}
          _____     _____                        _____                    _____                    _____
         /\    \   /\    \                      /\    \                  /\    \                  /\    \         
        /::\____\ /::\    \                    /::\____\                /::\    \                /::\    \        
       /:::/    //::::\    \                  /:::/    /                \:::\    \              /::::\    \       
      /:::/    //::::::\    \                /:::/    /                  \:::\    \            /::::::\    \      
     /:::/    //:::/\:::\    \              /:::/    /                    \:::\    \          /:::/\:::\    \     
    /:::/    //:::/__\:::\    \            /:::/____/                      \:::\    \        /:::/__\:::\    \    
   /:::/    //::::\   \:::\    \           |::|    |                       /::::\    \      /::::\   \:::\    \   
  /:::/    //::::::\   \:::\    \          |::|    |     _____    ____    /::::::\    \    /::::::\   \:::\    \  
 /:::/    //:::/\:::\   \:::\    \         |::|    |    /\    \  /\   \  /:::/\:::\    \  /:::/\:::\   \:::\    \ 
/:::/____//:::/  \:::\   \:::\____\        |::|    |   /::\____\/::\   \/:::/  \:::\____\/:::/__\:::\   \:::\____\
\:::\    \\::/    \:::\  /:::/    /        |::|    |  /:::/    /\:::\  /:::/    \::/    /\:::\   \:::\   \::/    /
 \:::\    \\/____/ \:::\/:::/    /         |::|    | /:::/    /  \:::\/:::/    / \/____/  \:::\   \:::\   \/____/ 
  \:::\    \        \::::::/    /          |::|____|/:::/    /    \::::::/    /            \:::\   \:::\    \     
   \:::\    \        \::::/    /           |:::::::::::/    /      \::::/____/              \:::\   \:::\____\    
    \:::\    \       /:::/    /            \::::::::::/____/        \:::\    \               \:::\   \::/    /    
     \:::\    \     /:::/    /              ~~~~~~~~~~               \:::\    \               \:::\   \/____/     
      \:::\    \   /:::/    /                                         \:::\    \               \:::\    \         
       \:::\____\ /:::/    /                                           \:::\____\               \:::\____\        
        \::/    / \::/    /                                             \::/    /                \::/    /        
         \/____/   \/____/                                               \/____/                  \/____/         
                                                                                                                  

路径
路径
数据库内容:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值