Spring Boot Framework - Getting Started

  Spring Boot Spring is a very important family bucket a module, you can quickly build a Spring-based Java application through Spring Boot, Spring Boot for popular third-party library provides configuration options, and can be well integrated with Spring, MyBatis, Spring Data JPA, etc., you can set up one-click full-featured Java enterprise applications.

  Spring Boot advantages

    - does not require any XML configuration file.
    - Built-in Web server can be started directly.
    - Default support JSON data, no need for additional configuration.
    - Support for RESTful style
    - using a configuration file (non-XML, propertis, YAML) can configure all of the personalized information

  Spring Boot is a little configuration framework can be used to quickly build Spring applications, and can be automatically integrated mainstream Java technology stack.

  There are two ways to create a Spring Boot

    - Create Project Online

    - Manually Create Project

 

  Here we show you created online

 

1, start the idea, click on the Create New Project

  

 

2、选择Spring Initializr--Default: https://start.spring.io--next

  

 

3. Enter Group, Artifact, etc. - Click next

  

 

4. Select the check web-- click next Spring Web--

  

 

5, select the path - Click to finish

  

 

6, OK, spring boot project will be created, if it is the first project to create a spring boot, then need to wait for a while to download pom-dependent

  

 

7, add pom.xml dependence

<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>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- 数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.14</version>
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>
    <!-- mybaits -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
</dependencies>

 

8, the configuration file application.properties

Note: Spring Boot Support .properties / .yml two configuration file format, if both co-exist, with the first priority. Here I modified for .yml suffix (for writing, it is recommended to use this format)

To illustrate an example, where only the most simple configuration

server:
  port: 7777

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/sunjian2?&useSSL=false&serverTimezone=UTC

 

9, create entity classes

package com.sunjian.demo.entity;

import lombok.Data;

/**
 * @author sunjian
 * @date 2020/3/24 23:20
 */
@Data
public class Person {
    private Integer id;
    private String name;
    private String age;
    private String gender;
    private String email;
    private String city;
}

 

10, create a dao layer interface

package com.sunjian.demo.dao;

import com.sunjian.demo.entity.Person;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * @author sunjian
 * @date 2020/3/24 23:25
 */
@Mapper
public interface UserDao {
    @Select("select * from person where id = #{id}")
    Person findById(Integer id);
}

 

11, to create a service layer interface and implementation class

package com.sunjian.demo.service;

import com.sunjian.demo.entity.Person;

/**
 * @author sunjian
 * @date 2020/3/24 23:29
 */
public interface PersonService {
    public Person findById(Integer id);
}

 

package com.sunjian.demo.service.impl;

import com.sunjian.demo.dao.UserDao;
import com.sunjian.demo.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author sunjian
 * @date 2020/3/24 23:30
 */
@Service
public class PersonServiceImpl {
    @Autowired
    private UserDao userDao;

    public Person findById(Integer id){
        return userDao.findById(id);
    }
}

 

12, the view controller creates the layer class

package com.sunjian.demo.controller;

import com.sunjian.demo.entity.Person;
import com.sunjian.demo.service.PersonService;
import com.sunjian.demo.service.impl.PersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sunjian
 * @date 2020/3/24 23:32
 */
@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonServiceImpl personServiceImpl;

    @GetMapping("/findById/{id}")
    public Person findById(@PathVariable("id") Integer id){
        Person person = personServiceImpl.findById(id);
        System.out.println(person);
        return person;
    }
}

 

13, started the project (Spring Boot built Tomcat web server, run directly DemoApplication startup class file to start the project), access

  

  

  

 

 

 

 

 

 

OK.

 

Guess you like

Origin www.cnblogs.com/zuichao123/p/12563285.html