转载

使用SpingBoot快速构建工程入门

一、SpingBoot的概述

Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.

上面是引自官网的一段话,大概是说: Spring Boot 是所有基于 Spring 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。

二、SpringBoot的好处

回顾我们之前的 SSM 项目,搭建过程还是比较繁琐的,需要:

配置 web.xml,加载 spring 和 spring mvc

配置数据库连接、配置日志文件

配置家在配置文件的读取,开启注解

配置mapper文件

.....

而使用 Spring Boot 来开发项目则只需要非常少的几个配置就可以搭建起来一个 Web 项目,并且可以避免依赖之间的各种冲突

三、项目的搭建

1、创建数据库和表

创建一个数据库为mybatis的数据库,在该数据库下面创建一张表,建表语句如下:

[SQL] 纯文本查看 复制代码

?

/*

Navicat Premium Data Transfer

Source Server : local

Source Server Type : MySQL

Source Server Version : 50723

Source Host : localhost:3306

Source Schema : yun6

Target Server Type : MySQL

Target Server Version : 50723

File Encoding : 65001

Date: 08/09/2018 16:33:40

*/

SET NAMES utf8mb4;

SET FOREIGN_KEY_CHECKS = 0;

-- Table structure for tb_user

DROP TABLE IF EXISTS tb_user ;

CREATE TABLE tb_user (

id bigint(20) NOT NULL AUTO_INCREMENT,

user_name varchar(100) DEFAULT NULL COMMENT '用户名',

password varchar(100) DEFAULT NULL COMMENT '密码',

name varchar(100) DEFAULT NULL COMMENT '姓名',

age int(10) DEFAULT NULL COMMENT '年龄',

sex tinyint(1) DEFAULT NULL COMMENT '性别,1男性,2女性',

birthday date DEFAULT NULL COMMENT '出生日期',

note varchar(255) DEFAULT NULL COMMENT '备注',

created datetime DEFAULT NULL COMMENT '创建时间',

updated datetime DEFAULT NULL COMMENT '更新时间',

PRIMARY KEY ( id ),

UNIQUE KEY username ( user_name )

) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

-- Records of tb_user

BEGIN;

INSERT INTO tb_user VALUES (1, 'zhangsan', '123456', '张三', 30, 1, '1964-08-08', '张三同学在学Java', '2014-09-19 16:56:04', '2014-09-21 11:24:59');

INSERT INTO tb_user VALUES (2, 'lisi', '123456', '李四', 21, 2, '1995-01-01', '李四同学在传智学Java', '2014-09-19 16:56:04', '2014-09-19 16:56:04');

INSERT INTO tb_user VALUES (3, 'wangwu', '123456', '王五', 22, 2, '1994-01-01', '王五同学在学php', '2014-09-19 16:56:04', '2014-09-19 16:56:04');

INSERT INTO tb_user VALUES (4, 'zhangwei', '123456', '张伟', 20, 1, '1996-09-01', '张伟同学在传智播客学Java', '2014-09-19 16:56:04', '2014-09-19 16:56:04');

INSERT INTO tb_user VALUES (5, 'lina', '123456', '李娜', 28, 1, '1988-01-01', '李娜同学在传智播客学Java', '2014-09-19 16:56:04', '2014-09-19 16:56:04');

INSERT INTO tb_user VALUES (6, 'lilei', '123456', '李磊', 23, 1, '1993-08-08', '李磊同学在传智播客学Java', '2014-09-20 11:41:15', '2014-09-20 11:41:15');

INSERT INTO tb_user VALUES (7, 'hanmeimei', '123456', '韩梅梅', 24, 2, '1992-08-08', '韩梅梅同学在传智播客学php', '2014-09-20 11:41:15', '2014-09-20 11:41:15');

INSERT INTO tb_user VALUES (8, 'liuyan', '123456', '柳岩', 21, 2, '1995-08-08', '柳岩同学在传智播客学表演', '2014-09-20 11:41:15', '2014-09-20 11:41:15');

INSERT INTO tb_user VALUES (9, 'liuyifei', '123456', '刘亦菲', 18, 2, '1998-08-08', '刘亦菲同学在传智播客学唱歌', '2014-09-20 11:41:15', '2014-09-20 11:41:15');

INSERT INTO tb_user VALUES (10, 'fanbingbing', '123456', '范冰冰', 25, 2, '1991-08-08', '范冰冰同学在传智播客学表演', '2014-09-20 11:41:15', '2014-09-20 11:41:15');

INSERT INTO tb_user VALUES (11, 'zhengshuang', '123456', '郑爽', 23, 2, '1993-08-08', '郑爽同学在传智播客学习如何装纯', '2014-09-20 11:41:15', '2014-09-20 11:41:15');

INSERT INTO tb_user VALUES (12, 'tangyan', '123456', '唐嫣', 26, 2, '1990-08-08', '郑爽同学在传智播客学习如何耍酷', '2014-09-20 11:41:15', '2014-09-20 11:41:15');

COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

2、创建一个maven工程

在pom.xml中导入下面依赖:

[XML] 纯文本查看 复制代码

?

<parent>

<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <mapper.starter.version>2.1.4</mapper.starter.version>
    <mysql.version>5.1.46</mysql.version>
    <pageHelper.starter.version>1.2.5</pageHelper.starter.version>
</properties>


<dependencies>

    <!--web启动器-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 通用Mapper启动器 -->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>${mapper.starter.version}</version>
    </dependency>

    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

3、编写启动类

在src下创建包:com.heima.brand,在com.heima.brand包下创建SpringBoot的启动器:

[Java] 纯文本查看 复制代码

?

@SpringBootApplication

public class BrandApplication {

public static void main(String[] args) {

    SpringApplication.run(BrandApplication.class, args);
}

}

4、创建对应的实体

在创建完实体类之后,在对应的类和属性上加上通用mapper和lombok的注解,用来映射表和字段和生成对应的set和get方法:

[Java] 纯文本查看 复制代码

?

@Data

@Table(name = "tb_brand")

public class Brand {

@Id
@KeySql(useGeneratedKeys =true)//开启主键回填
private Long id;

private String name;

private String firstChar;

}

5、编写配置文件

在resources目录下创建application.yml文件:

[XML] 纯文本查看 复制代码

?

server:

port: 8080

spring:

datasource:

driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybatis
username: root
password: 123456

mybatis:

type-aliases-package: com.heima.brand.bean

logging:

level:

com.heima.brand: debug

5、创建对应的mapper接口

在mapper包下面创建BrandMapper接口,并且继承通用Mapper的父类:

[Java] 纯文本查看 复制代码

?

import com.heima.brand.bean.Brand;

import tk.mybatis.mapper.common.Mapper;

public interface BrandMapper extends Mapper<Brand> {

}

6、在启动类上加入借口扫描包

在SpringBoot的启动器中加入通用mapper的扫描包:

[Java] 纯文本查看 复制代码

?

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication

@MapperScan("com.heima.brand.mapper")

public class BrandApplication {

public static void main(String[] args) {

    SpringApplication.run(BrandApplication.class, args);
}

}

7、编写业务层接口和实现

编写业务层的接口和实现:

BrandService:

[Java] 纯文本查看 复制代码

?

public interface BrandService {

List<Brand> findAllBrand();

}

BrandServiceImpl:

[Java] 纯文本查看 复制代码

?

@Service

public class BrandServiceImpl implements BrandService {

@Autowired
private BrandMapper brandMapper;

@Override
public List<Brand> findAllBrand() {

    return brandMapper.selectByExample(null);
}

}

8、编写控制层

编写控制层:BrandController

[Java] 纯文本查看 复制代码

?

@RestController

@RequestMapping("/brand")

public class BrandController {

@Autowired
private BrandService brandService;

@RequestMapping("/findAllBrand")
public List<Brand> findAllBrand(){

    return brandService.findAllBrand();
}

}

9、启动进行测试

测试结果:

10、小结

注意:

启动类默认的扫描包为:com.heima.brand,因此controler,service,mapper等包必须要是com.heima.brand的子包。

原文  https://segmentfault.com/a/1190000020755449
正文到此结束
Loading...