guyanqing 1 рік тому
батько
коміт
e1d4bc6a99

+ 28 - 0
springBoot/pom.xml

@@ -34,6 +34,34 @@
             <!--带有starter样式-->
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+            <optional>true</optional>
+        </dependency>
+
+<!--        springboot-aop-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid-spring-boot-starter</artifactId>
+            <version>1.1.17</version>
+        </dependency>
+        <dependency>
+            <groupId>com.github.pagehelper</groupId>
+            <artifactId>pagehelper-spring-boot-starter</artifactId>
+            <version>1.3.0</version>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <scope>runtime</scope>
+        </dependency>
     </dependencies>
 
     <build>

+ 21 - 5
springBoot/src/main/java/com/sf/springboot/controller/BookController.java

@@ -2,7 +2,9 @@ package com.sf.springboot.controller;
 
 import com.sf.springboot.dto.Result;
 import com.sf.springboot.entity.Book;
+import com.sf.springboot.service.BookService;
 import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -38,7 +40,8 @@ public class BookController {
      * 添加图书
      * @return
      */
-    @RequestMapping("/addBook2")
+//    @RequestMapping(value = "/addBook2",method = RequestMethod.POST)
+    @PostMapping("/addBook2")
     public Result addBook2(@RequestBody Book book){
         System.out.println(book);
         return new Result();
@@ -59,7 +62,8 @@ public class BookController {
      * 更新图书
      * @return
      */
-    @RequestMapping("/updateBook2")
+//    @RequestMapping(value = "/updateBook2",method = RequestMethod.PUT)
+    @PutMapping("/updateBook2")
     public Result updateBook2(@RequestBody Book book){
         System.out.println(book);
         return new Result();
@@ -71,7 +75,8 @@ public class BookController {
      * @param bookId
      * @return
      */
-    @RequestMapping("/deleteBook")
+//    @RequestMapping(value = "/deleteBook",method = RequestMethod.DELETE)
+    @DeleteMapping("/deleteBook")
     public Result deleteBook(Integer bookId){
         System.out.println(bookId);
         return new Result();
@@ -98,7 +103,8 @@ public class BookController {
      * 根据图书的id去查询图书的详情
      */
 
-    @RequestMapping("/getBookInfo")
+//    @RequestMapping(value = "/getBookInfo",method = RequestMethod.GET)
+    @GetMapping("/getBookInfo")
     public Result getBookInfo(Integer bookId){
         System.out.println(bookId);
         return new Result();
@@ -167,4 +173,14 @@ public class BookController {
         ResponseEntity<byte[]> response = new ResponseEntity<>(body,headers,ok);
         return response;
     }
-}
+
+
+    @Autowired
+    private BookService bookService;
+
+    @PostMapping("/addBook66")
+    public Result addBook66(@RequestBody Book book){
+        bookService.addBook(book);
+        return new Result();
+    }
+ }

+ 28 - 0
springBoot/src/main/java/com/sf/springboot/controller/UserController.java

@@ -1,6 +1,9 @@
 package com.sf.springboot.controller;
 
 import com.sf.springboot.entity.User;
+import com.sf.springboot.entity.User1;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -23,4 +26,29 @@ public class UserController {
         list.add(u3);
         return list;
     }
+
+//
+//    @Value("${adminName}")
+//    private String adminName;
+//    @Value("${age}")
+//    private Integer age;
+//    @Value("${adminId}")
+//    private Integer adminId;
+//
+//    @RequestMapping("/getProperties")
+//    public String getProperties(){
+//        System.out.println("adminName========>"+adminName);
+//        System.out.println("age========>"+age);
+//        System.out.println("adminId========>"+adminId);
+//        return "ok..";
+//    }
+//
+//    @Autowired
+//    private User1 user1;
+//
+//    @RequestMapping("/getPropertiesObject")
+//    public String getPropertiesObject(){
+//        System.out.println("---->"+user1);
+//        return "okok...";
+//    }
 }

+ 35 - 0
springBoot/src/main/java/com/sf/springboot/dto/LogAspect.java

@@ -0,0 +1,35 @@
+package com.sf.springboot.dto;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletRequest;
+
+@Aspect
+@Component
+public class LogAspect {
+//    private final static Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
+    @Pointcut(value = "execution(* com.sf.springboot.controller.UserController.queryUserJson(..))")
+    public void log(){}
+    @Around(value = "log()")
+    public Object methodAroud(ProceedingJoinPoint joinPoint) throws Throwable {
+        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+        HttpServletRequest request = attributes.getRequest();
+        //获取主机IP
+        System.out.println("IP :{}"+request.getRemoteAddr());
+        //获取请求地址
+        System.out.println("URL:{}"+request.getRequestURL().toString());
+        //获取请求方式
+        System.out.println("HTTP Method:{}"+request.getMethod());
+        //获取类名和方法名
+        System.out.println("Class Method:{}.{}"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
+        Object proceed = joinPoint.proceed();
+        return proceed;
+    }
+
+}

+ 56 - 0
springBoot/src/main/java/com/sf/springboot/entity/User1.java

@@ -0,0 +1,56 @@
+package com.sf.springboot.entity;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+
+@Component
+@ConfigurationProperties(prefix = "user1")
+public class User1 {
+    private Integer userId;
+    private String userName;
+    private String hireDate;
+
+    public User1() {
+    }
+
+    public User1(Integer userId, String userName, String hireDate) {
+        this.userId = userId;
+        this.userName = userName;
+        this.hireDate = hireDate;
+    }
+
+    public Integer getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Integer userId) {
+        this.userId = userId;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getHireDate() {
+        return hireDate;
+    }
+
+    public void setHireDate(String hireDate) {
+        this.hireDate = hireDate;
+    }
+
+    @Override
+    public String toString() {
+        return "User1{" +
+                "userId=" + userId +
+                ", userName='" + userName + '\'' +
+                ", hireDate=" + hireDate +
+                '}';
+    }
+}

+ 9 - 0
springBoot/src/main/java/com/sf/springboot/mapper/BookMapper.java

@@ -0,0 +1,9 @@
+package com.sf.springboot.mapper;
+
+import com.sf.springboot.entity.Book;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface BookMapper {
+    void addBook(Book book);
+}

+ 7 - 0
springBoot/src/main/java/com/sf/springboot/service/BookService.java

@@ -0,0 +1,7 @@
+package com.sf.springboot.service;
+
+import com.sf.springboot.entity.Book;
+
+public interface BookService {
+    void addBook(Book book);
+}

+ 17 - 0
springBoot/src/main/java/com/sf/springboot/service/impl/BookServiceImpl.java

@@ -0,0 +1,17 @@
+package com.sf.springboot.service.impl;
+
+import com.sf.springboot.entity.Book;
+import com.sf.springboot.mapper.BookMapper;
+import com.sf.springboot.service.BookService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class BookServiceImpl implements BookService {
+    @Autowired
+    private BookMapper bookMapper;
+    @Override
+    public void addBook(Book book) {
+        bookMapper.addBook(book);
+    }
+}

+ 31 - 7
springBoot/src/main/resources/application.properties

@@ -1,12 +1,36 @@
-# ??????
 server.port=8080
-# ???????
-# server.servlet.context-path=/book
-# ????????
+
 spring.servlet.multipart.max-file-size=10MB
-# ???????
+
 spring.servlet.multipart.max-request-size=100MB
-# ??jackson??
+
 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
-# ????
+
 spring.jackson.time-zone=GMT+8
+
+
+
+adminName=admin
+age=18
+adminId=123
+
+user1.userId=1001
+user1.userName=Tom
+user1.hireDate=2020-12-12 22:22:22
+
+# ?????
+spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&useUnicode=true&serverTimezone=GMT%2B8
+spring.datasource.username=root
+spring.datasource.password=root
+
+# mybatis??
+mybatis.mapper-locations=classpath:mapper/*Mapper.xml
+mybatis.type-aliases-package=com.sf.springboot.entity
+mybatis.config-location=classpath:mybatis-config.xml
+
+# ????(????)
+http.encoding.charset=UTF-8
+http.encoding.force=true
+http.encoding.enabled=true
+server.tomcat.uri-encoding=UTF-8
+

+ 9 - 0
springBoot/src/main/resources/mapper/BookMapper.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sf.springboot.mapper.BookMapper">
+    <insert id="addBook" parameterType="book">
+        insert into book(book_id,book_name,price) values (#{bookId},#{bookName},#{price})
+    </insert>
+</mapper>

+ 11 - 0
springBoot/src/main/resources/mybatis-config.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE configuration
+        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+        "https://mybatis.org/dtd/mybatis-3-config.dtd">
+<configuration>
+
+<!--    配置驼峰命名-->
+    <settings>
+        <setting name="mapUnderscoreToCamelCase" value="true"/>
+    </settings>
+</configuration>