Spring Boot框架——快速入门

  Spring Boot是Spring 全家桶很是重要的一个模块,经过 Spring Boot 能够快速搭建一个基于 Spring 的 Java 应用程序,Spring Boot 对经常使用的第三方库提供了配置方案,能够很好地和 Spring 进行整合,MyBatis、Spring Data JPA 等,能够一键式搭建功能完备的 Java 企业级应用。java

  Spring Boot 的优点mysql

    - 不须要任何 XML 配置文件。
    - 内嵌 Web 服务器,能够直接启动。
    - 默认支持 JSON 数据,不须要作额外配置。
    - 支持 RESTful 风格
    - 使用一个配置文件(非 XML、propertis、YAML)能够配置全部的个性化信息web

  Spring Boot 就是一个能够用不多的配置快速搭建 Spring 应用的框架,而且能够自动集成主流的 Java 技术栈。spring

  Spring Boot有两种建立方式sql

    - 在线建立工程数据库

    - 手动建立工程apache

 

  这里演示一下在线建立服务器

 

一、启动idea,点击Create New Projectmybatis

  

 

二、选择Spring Initializr--Default: https://start.spring.io--nextapp

  

 

三、输入Group、Artifact等--点击next

  

 

四、选择web--勾选Spring Web--点击next

  

 

五、选择路径--点击finish

  

 

六、OK,spring boot项目建立成功了,若是是第一次建立spring boot项目的话,须要等待一会,下载pom依赖

  

 

七、添加pom.xml依赖

<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>

 

八、配置application.properties文件

注意:Spring Boot支持 .properties / .yml两种格式的配置文件,若是两种都存在时,以第一种优先。这里我修改成了.yml后缀的(便于书写,建议使用此格式的)

为了演示例子,这里只作最简单配置

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

 

九、建立实体类

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;
}

 

十、建立dao层接口

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);
}

 

十一、建立service层接口及实现类

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);
    }
}

 

十二、建立controller视图层类

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;
    }
}

 

1三、启动项目(Spring Boot内置了Tomcat web服务器,直接运行DemoApplication启动类文件便可启动项目),访问

  

  

  

 

 

 

 

 

 

OK.

相关文章
相关标签/搜索