- 打开 File-Project Structure-Modules
2.选择 Spring Initializr
3.配置自己的包目录
4.选择自己需要的插件
5.等待下载完成,由于下载后是没有webapp目录的,需要手动添加
方式:File-Project Structure-Modules-点击自己的module,
配置自己的webapp目录和WEB-INF目录
配置完的结构:
接下来就是编写项目
6.完整包结构
7. 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.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ygq</groupId>
<artifactId>step</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>step</name>
<description>My first springboot demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!--jsp页面jstl语句-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--是springboot将jsp解析成页面-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.46</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
8.index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<style>
table {
text-align: center;
background: lightgray;
}
#form1{
margin-top: 50px;
}
</style>
</head>
<body>
<div id="form1">
<table width="50%" border="1" cellspacing="0" align="center" text-align="center">
<tr>
<th colspan="5">所有学生信息表</th>
</tr>
<tr>
<th>学生id</th>
<th>学生姓名</th>
<th>学生年龄</th>
<th>学生地址</th>
<th>学生爱好</th>
</tr>
<c:forEach var="student" items="${Students}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.address}</td>
<td>${student.hobby}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
application.properties
server.port=8080
server.tomcat.uri-encoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db01?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
StudentDao.java
package com.ygq.step.dao;
import com.ygq.step.domain.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentDao {
@Select("Select * from student")
public List<Student> findAllStudent();
@Select("SELECT * FROM student where id= #{id}")
public Student findById(String id);
}
StudntController.java (StudentService.java知识一个接口进行了调用,没有什么作用,代码不再贴)
package com.ygq.step.controller;
import com.ygq.step.domain.Student;
import com.ygq.step.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/test")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/hello")
public String sayHello(){
return "Hello My First SpringBoot-Web Application !";
}
@RequestMapping("/findAll")
public void findAll( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Student> student = studentService.findAllStudent();
request.setAttribute("Students",student);
System.out.println(student);
request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request,response);
}
}
实体类 Student.java
package com.ygq.step.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Slf4j
public class Student {
private Integer id;
private String name;
private Integer age;
private String address;
private String hobby;
}
启动类
package com.ygq.step;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration
public class StepApplication {
public static void main(String[] args) {
SpringApplication.run(StepApplication.class, args);
}
}
9.在启动类启动项目
10.在浏览器输入地址:http://localhost:8080/test/findAll
显示结果: