2 Incheckningar 0358c328f5 ... 7bd8c18b39

Upphovsman SHA1 Meddelande Datum
  Starlight-0208 7bd8c18b39 2024年4月29日 23点28分 4 månader sedan
  Starlight-0208 f710b010b6 2024年4月27日 20点40分 4 månader sedan

+ 66 - 0
pom.xml

@@ -17,6 +17,17 @@
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-jetty</artifactId>
         </dependency>
 
         <dependency>
@@ -65,6 +76,25 @@
             <version>3.1.0</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>jakarta.platform</groupId>
+            <artifactId>jakarta.jakartaee-api</artifactId>
+            <version>10.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter</artifactId>
+            <version>2.2.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+            <version>3.4.2</version>
+        </dependency>
+
     </dependencies>
     <dependencyManagement>
         <dependencies>
@@ -82,6 +112,15 @@
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-resources-plugin</artifactId>
+                <version>3.2.0</version>
+                <configuration>
+                    <encoding>UTF-8</encoding>
+                    <useDefaultDelimiters>true</useDefaultDelimiters>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.8.1</version>
                 <configuration>
@@ -110,4 +149,31 @@
         </plugins>
     </build>
 
+    <profiles>
+        <!-- 开发环境 -->
+        <profile>
+            <id>dev</id>
+            <properties>
+                <profile.active>dev</profile.active>
+            </properties>
+        </profile>
+        <!-- 生产环境 -->
+        <profile>
+            <id>prod</id>
+            <properties>
+                <profile.active>prod</profile.active>
+            </properties>
+            <activation>
+                <activeByDefault>true</activeByDefault>
+            </activation>
+        </profile>
+        <!-- 测试环境 -->
+        <profile>
+            <id>test</id>
+            <properties>
+                <profile.active>test</profile.active>
+            </properties>
+        </profile>
+    </profiles>
+
 </project>

+ 18 - 1
src/main/java/org/starter/learning/config/SpringMvcConfig.java

@@ -1,11 +1,28 @@
 package org.starter.learning.config;
 
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.starter.learning.controller.interceptor.UserInterceptor;
 
 @Configuration
 @EnableWebMvc
 @ComponentScan("org.starter.learning.controller")
-public class SpringMvcConfig {
+public class SpringMvcConfig implements WebMvcConfigurer {
+    @Autowired
+    private UserInterceptor userInterceptor;
+
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        registry.addResourceHandler("/paths/**").addResourceLocations("/pages/");
+    }
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        registry.addInterceptor(userInterceptor).addPathPatterns("/user");
+    }
 }

+ 37 - 0
src/main/java/org/starter/learning/controller/UserController.java

@@ -0,0 +1,37 @@
+package org.starter.learning.controller;
+
+import org.springframework.web.bind.annotation.*;
+import org.starter.learning.pojo.User;
+import org.starter.learning.pojo.Result;
+
+@RestController
+@RequestMapping("/user")
+public class UserController {
+    @GetMapping
+    public Result getAllUser() {
+        System.out.println("Execute getAll method OK!");
+        return new Result(200, "execute success", "getAll");
+    }
+
+    @GetMapping("/{id}")
+    public Result getById(@PathVariable Integer id){
+        System.out.println("Execute getById method OK! ID = " + id);
+        return new Result(200, "execute success", "getById");
+    }
+    
+    @PostMapping
+    public Result save(@RequestBody User user){
+        System.out.println("Execute save method OK! user = " + user.toString());
+        return new Result(200, "execute success", "save");
+    }
+    @PutMapping
+    public Result modify(@RequestBody User user){
+        System.out.println("Execute modify method OK! user = " + user.toString());
+        return new Result(200, "execute success", "modify");
+    }
+    @DeleteMapping("/{id}")
+    public Result deleteById(@PathVariable Integer id){
+        System.out.println("Execute getAllUser method OK! id = " + id);
+        return new Result(200, "execute success", "deleteById");
+    }
+}

+ 26 - 0
src/main/java/org/starter/learning/controller/interceptor/UserInterceptor.java

@@ -0,0 +1,26 @@
+package org.starter.learning.controller.interceptor;
+
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.ModelAndView;
+
+@Component
+public class UserInterceptor implements HandlerInterceptor {
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        System.out.println("PreHandler...");
+        return true;
+    }
+
+    @Override
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
+        System.out.println("PostHandler...");
+    }
+
+    @Override
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+        System.out.println("afterCompletion...");
+    }
+}

+ 8 - 0
src/main/java/org/starter/learning/demos/web/User.java

@@ -40,4 +40,12 @@ public class User {
     public void setAge(Integer age) {
         this.age = age;
     }
+
+    @Override
+    public String toString() {
+        return "User{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                '}';
+    }
 }

+ 48 - 0
src/main/java/org/starter/learning/pojo/User.java

@@ -0,0 +1,48 @@
+/*
+ * Copyright 2013-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.starter.learning.pojo;
+
+public class User {
+
+    private String name;
+
+    private Integer age;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    @Override
+    public String toString() {
+        return "User{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                '}';
+    }
+}

+ 0 - 0
src/main/resources/application.properties → src/main/resources/application.properties.bak


+ 9 - 0
src/main/resources/application.yml

@@ -0,0 +1,9 @@
+spring:
+  datasource:
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    url: jdbc:mysql://localhost:3306/test?useSSL=false
+    username: root
+    password: 123456
+
+server:
+  port: 8080