This commit is contained in:
qijun202201080136 2024-09-30 19:15:40 +08:00
commit 71b0cfa2d2
20 changed files with 620 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package edu.qijun.mapper;
import edu.qijun.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentMapper {
//查询所有学生全部信息
@Select("SELECT *from student")
public abstract List<Student> list();
//根据ID
@Select("select *from student where id=#{id}")
Student getById(String id);
//根据姓氏查询
@Select("select * from student where name like concat(#{lastNmae},'%')")
List<Student>getByLastName(String lastName);
@Select("select *from student where char_length(student.name)=#{len}")
List<Student> getByNameLen(int len);
}

View File

@ -0,0 +1,21 @@
package edu.qijun.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private String id;
private String name;
private String password;
private Integer gender;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}

97
five/pom.xml Normal file
View File

@ -0,0 +1,97 @@
<?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>3.3.3</version> <!-- 使用您所需的Spring Boot版本 -->
</parent>
<groupId>org.example</groupId>
<artifactId>five</artifactId>
<version>1.0-SNAPSHOT</version>
<name>five</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>22</maven.compiler.target>
<maven.compiler.source>22</maven.compiler.source>
<junit.version>5.11.0-M2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin> </plugins>
</build>
</project>

View File

@ -0,0 +1,31 @@
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}

View File

@ -0,0 +1,18 @@
package edu.qijun.conctroller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class IndexController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
System.out.println("Welcome to Springmvc");
return "Welcome to Springmvc";
}
}

View File

@ -0,0 +1,33 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启包扫描-->
<context:component-scan base-package="edu.qijun.conctroller"/>
<!--开启注解驱动注册一些必要的Bean到SpringMVC容器里面-->
<mvc:annotation-driven>
<!-- 处理乱码 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 视图解析器
作用1.捕获后端控制器的返回值="index"
2.解析: 在返回值的前后 拼接 ==> "/index.jsp"
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvx.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -0,0 +1,12 @@
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %></h1>
<br/>
<a href="hello-servlet">Hello Servlet</a>
</body>
</html>

View File

@ -0,0 +1,64 @@
package edu.qijun.controller;
import edu.qijun.pojo.Student;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@RestController
public class StudentController {
public static final Map<Integer,Student> students=new HashMap<>();
public static final AtomicInteger counter=new AtomicInteger();
//1创建学生/student
@PostMapping("/student")
public Student insertStudnet(@RequestBody Student student){
Integer id=counter.incrementAndGet();
student.setId(id);
students.put(id,student);
System.out.println(student);
return student;
}
//查询所有学生/studentget方法
@GetMapping("/student")
public Map<Integer,Student> getAllStudents(){
System.out.println(students);
return students;
}
//查询单个学生/student/1
@GetMapping("/student/{id}")
public Student getStudentById(@PathVariable Integer id){
System.out.println(students.get(id));
return students.get(id);
}
//根据id删学生
@DeleteMapping("/student/{id}")
public String deletdStudentById(@PathVariable Integer id){
students.remove(id);
System.out.println(students);
return "Remove OK";
}
//根据Id更新学生信息
@PutMapping("/student/{id}")
public Student updateStudentById(@PathVariable Integer id,@RequestBody Student student){
Student oldStudent=students.get(id);
if(oldStudent!=null){
oldStudent.setName(student.getName());
oldStudent.setAge(student.getAge());
}
return oldStudent;
}
}

View File

@ -0,0 +1,17 @@
package edu.qijun.pojo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class Student {
@JsonIgnore
private Integer id;
private String name;
private Integer age;
}

6
six/WEB-INF/web.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>

View File

@ -0,0 +1,27 @@
package edu.qijun.six.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
public class ArrayParamsController {
@RequestMapping(value = "/arrayTest")
public String arrayParams(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "Get ,hobby="+ Arrays.toString(hobby);
}
//集合参数
@RequestMapping("/listTest")
public String listTest(@RequestParam List<String> hobby){
System.out.println(hobby);
return "Get ,hobby="+hobby;
}
}

View File

@ -0,0 +1,18 @@
package edu.qijun.six.controller;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
public class DataParamsController {
@RequestMapping("/dataTest")
// /dataTest?dataTime=2024=09=18 165000
public String dataTest(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime dateTime){
System.out.println(dateTime);
return dateTime.toString();
}
}

View File

@ -0,0 +1,16 @@
package edu.qijun.six.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControlller {
//URL:协议
//@GetMapping("/hello")
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
return "那咋了";
}
}

View File

@ -0,0 +1,22 @@
package edu.qijun.six.controller;
import edu.qijun.six.pojo.User;
import edu.qijun.six.pojo.Address;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JsonParamsController {
@RequestMapping(value = "/jsonTest1", method = RequestMethod.POST)
public String jsonTest1(@RequestBody User user) {
System.out.println(user);
Address address = user.getAddress();
return "Post 请求name=" + user.getUsername() + ",password=" + user.getPassword() +
",province=" + (address != null ? address.getProvince() : "未提供") +
",city=" + (address != null ? address.getCity() : "未提供");
}
}

View File

@ -0,0 +1,30 @@
package edu.qijun.six.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PathParamsController {
@RequestMapping("/pathTest1/{id}")
public String passTest1(@PathVariable Integer id){
return "Path:id="+id;
}
@RequestMapping("/pathTest2/{name}")
public String passTest2(@PathVariable Integer id,@PathVariable String name){
return "Path:id="+id+",name="+name;
}
@RequestMapping("/pathTest3/**")
public String pathTest3(){
return "通配符请求:**";
}
@RequestMapping("/pathTest4/*")
public String pathTest4(){
return "通配符请求:*";
}
}

View File

@ -0,0 +1,36 @@
package edu.qijun.six.controller;
import edu.qijun.six.pojo.User;
import edu.qijun.six.pojo.Address;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PojoParamsController {
@RequestMapping(value = "/postTest1", method = RequestMethod.POST)
public String pojoTest1(@RequestBody User user) {
System.out.println(user);
return "Post 请求,name=" + user.getUsername() + ",password=" + user.getPassword();
}
@RequestMapping(value = "/postTest2", method = RequestMethod.POST)
public String pojoTest2(@RequestBody User user) {
System.out.println(user);
Address address = user.getAddress();
return "Post 请求name=" + user.getUsername() + ",password=" + user.getPassword() + ",address=" + (address != null ? address.toString() : "未提供");
}
}

View File

@ -0,0 +1,49 @@
package edu.qijun.six.controller;
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.RestController;
@RestController
public class SimpleParamsControoler {
//1
@RequestMapping(value = "/getTest1",method = RequestMethod.GET)
public String getTest1(){
return "Get";
}
//2
//getTest2?nickname=Maxxie&age=18
@RequestMapping(value = "/getTest2",method = RequestMethod.GET)
public String getTest2(String nickname,int age){
System.out.println("Get 请求name="+nickname+",age=+“age");
return "Get ,name="+nickname+",age="+age;
}
//3
//getTest3?nickname=Maxxie&age=18
@RequestMapping(value = "/getTest3",method = RequestMethod.GET)
public String getTest3(@RequestParam(value = "nickname",required = false) String nickname, int age){
System.out.println("Get 请求name="+nickname+",age=+“age");
return "Get ,name="+nickname+",age="+age;
}
//4
@RequestMapping(value = "/postTest1",method = RequestMethod.POST)
public String postTest1(){
return "Post";
}
//5.post有参
@RequestMapping(value = "/postTest2",method = RequestMethod.POST)
public String postTest2(String nickname,int age){
System.out.println("Get 请求name="+nickname+",age=+“age");
return "Get ,name="+nickname+",age="+age;
}
}

View File

@ -0,0 +1,24 @@
package edu.qijun.six.pojo;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Address {
private String username;
private String password;
private String city;
private String province;
private Address address;
// Getters and setters for address
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

View File

@ -0,0 +1,48 @@
package edu.qijun.six.pojo;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
private String username;
private String password;
private Address address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// Getters and setters for address
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", address=" + address +
'}';
}
}