美文网首页
深入学习java笔记-4.SpringBoot整合Spring、

深入学习java笔记-4.SpringBoot整合Spring、

作者: 笨鸡 | 来源:发表于2019-04-30 17:00 被阅读0次

    1.目录结构

    SpringBoot.png

    2.配置文件

    application.yml
    spring:
      profiles:
        active: dev
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/spring_boot_mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false
        username: root
        password: "password"
        tomcat:
          initialSize: 1
          min-idle: 1
          max-idle: 20
          max-wait: 60000
          timeBetweenEvictionRunsMillis: 60000
          minEvictableIdleTimeMillis: 30000
          validationQuery: SELECT 1
          testWhileIdle: true
          testOnBorrow: false
          testOnReturn: false
    logging:
      file: logs/demo.log
    
    application-dev.yml
    server:
      port: 8081
    
    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 http://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.1.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.ctgu</groupId>
        <artifactId>springstart</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springstart</name>
        <description>Demo project for Spring Boot</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.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>5.1.47</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    java类

    SpringStartApplication.java
    package com.ctgu.springstart;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableAsync;
    
    @SpringBootApplication
    @EnableAsync
    public class SpringStartApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringStartApplication.class, args);
        }
    
    }
    
    User.java
    package com.ctgu.springstart.pojo;
    
    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.*;
    
    import java.util.Date;
    
    @Data
    public class User {
    
        private Integer id;
    
        private String username;
    
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private Integer age;
    
        @JsonProperty("account")
        private String phone;
    
        @JsonIgnore
        private String email;
    
        @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", locale = "zh", timezone = "GMT+8")
        private Date createTime;
    
    }
    
    UserDao.java
    package com.ctgu.springstart.dao;
    
    import com.ctgu.springstart.pojo.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Result;
    import org.apache.ibatis.annotations.Results;
    import org.apache.ibatis.annotations.Select;
    
    @Mapper
    public interface UserDao {
    
        /**
         * 根据用户名称,查询用户信息
         *
         * @param username 用户名
         */
        @Select("SELECT * FROM USER where username = #{username}")
    //    @Select("SELECT * FROM USER")
        @Results({
                @Result(property = "id", column = "id"),
                @Result(property = "username", column = "username"),
                @Result(property = "age", column = "age"),
                @Result(property = "phone", column = "phone"),
                @Result(property = "email", column = "email"),
        })
        User findByName(String username);
    //    User findByName(@Param("username") String username);
    }
    
    UserService.java
    package com.ctgu.springstart.service;
    
    import com.ctgu.springstart.pojo.User;
    
    public interface UserService {
    
        User findUserByName(String username);
    }
    
    UserServiceImpl.java
    package com.ctgu.springstart.service.impl;
    
    import com.ctgu.springstart.dao.UserDao;
    import com.ctgu.springstart.pojo.User;
    import com.ctgu.springstart.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        UserDao userDao;
    
        @Override
        public User findUserByName(String username) {
            return userDao.findByName(username);
        }
    }
    
    UserController.java
    package com.ctgu.springstart.controller;
    
    import com.ctgu.springstart.pojo.User;
    import com.ctgu.springstart.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
    
        @Autowired
        UserService userService;
    
        @GetMapping("/getUserInfo")
        public User getUser(@RequestParam(value = "username", required = true) String username) {
            return userService.findUserByName(username);
        }
    
    }
    
    GetController.java
    package com.ctgu.springstart.controller;
    
    import com.ctgu.springstart.pojo.User;
    import org.springframework.web.bind.annotation.*;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.HashMap;
    import java.util.Map;
    
    
    @RestController
    public class GetController {
    
        private Map<String, Object> params = new HashMap<>();
    
    //    @RequestMapping(path = "/{city_id}/{user_id}",method = RequestMethod.GET)
    //    public Object findUser(@PathVariable("city_id") String cityId,
    //                           @PathVariable("user_id") String userId){
    //        params.clear();
    //        params.put("cityId", cityId);
    //        params.put("userId", userId);
    //        return params;
    //    }
    
        @GetMapping(path = "/{city_id}/{user_id}")
        public Object findUser(@PathVariable("city_id") String cityId,
                               @PathVariable("user_id") String userId){
            params.clear();
            params.put("cityId", cityId);
            params.put("userId", userId);
            return params;
        }
    
        @GetMapping(value="/v1/page_user1")
        public Object pageUser(int from, int size){
            params.clear();
            params.put("from", from);
            params.put("size", size);
            return params;
        }
    
        @GetMapping(value="/v1/page_user2")
        public Object pageUserV2(@RequestParam(defaultValue = "0", name = "page", required = true) int from, int size){
            params.clear();
            params.put("from", from);
            params.put("size", size);
            return params;
        }
    
        @RequestMapping("/v1/save_user")
        public Object saveUser(@RequestBody User user){
            params.clear();
            params.put("user", user);
            return params;
        }
    
        @GetMapping("/v1/get_header")
        public Object getHeader(@RequestHeader("access_token") String accessToken, String id){
            params.clear();
            params.put("access_token", accessToken);
            params.put("id", id);
            return params;
        }
    
        @GetMapping("/v1/test_request")
        public Object testRequest(HttpServletRequest request){
            params.clear();
            params.put("id", request.getParameter("id"));
            return params;
        }
    
    }
    
    OtherHttpController.java
    package com.ctgu.springstart.controller;
    
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.PutMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @RestController
    public class OtherHttpController {
    
        private Map<String, Object> params = new HashMap<>();
    
        @PostMapping("/v1/login")
        public Object login(String id, String pwd){
            params.clear();
            params.put("id", id);
            params.put("pwd", pwd);
            return params;
        }
    
        @PutMapping("/v1/put")
        public Object put(String id){
            params.clear();
            params.put("id", id);
            return params;
        }
    
        @DeleteMapping("/v1/del")
        public Object del(String id){
            params.clear();
            params.put("id", id);
            return params;
        }
    }
    

    相关文章

      网友评论

          本文标题:深入学习java笔记-4.SpringBoot整合Spring、

          本文链接:https://www.haomeiwen.com/subject/cvvvnqtx.html