springboot

61 阅读2分钟

SpringBoot

1、工程建立方式

1-1、使用开发工具IDEA建立

打开 IDEA -> New Project -> Spring Initializr

img

填写项目信息

img

选择 Spring Boot 版本及 Web 开发所需的依赖

img

保存项目到指定目录

img

 

1-2、使用start.spring.io/网站建立之后下载得到

7

之后,就会弹出一个下载,解压之后我们就可以拿到工程了

8

我们把上述三个删掉,就和1-1方式创建的工程一样了。

2、工程以及依赖pom

2-1、工程目录如下

.gitignore
│  pom.xml
│
│
└─src
    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─funtl
    │  │          └─hello
    │  │              └─spring
    │  │                  └─boot
    │  │                          HelloSpringBootApplication.java
    │  │
    │  └─resources
    │      │  application.properties
    │      │
    │      ├─static
    │      └─templates
    └─test
        └─java
            └─com
                └─funtl
                    └─hello
                        └─spring
                            └─boot
                                    HelloSpringBootApplicationTests.java

 

2-2、pom配置如下

<?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.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot.xu</groupId>
    <artifactId>SpringBootDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootDemo</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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 阿里巴巴连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
    </dependencies>
​
​
​
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin><!--  mybatis的Maven插件  -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.4</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build></project>

 

3、配置mybatis

3-1、使用tkmybatis(对mybatis的封装)

应用application.yml代码如下:

mybatis:
  type-aliases-package: com.springboot.xu.SpringBootDemo.pojo
  mapper-locations: classpath:mapper/*.xml
spring:
  datasource:
    druid:
      url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
      username: root
      password: 123456
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      # MySQL 8.x: com.mysql.cj.jdbc.Driver
      driver-class-name: com.mysql.cj.jdbc.Driver

备注:

  • com.mysql.cj.jdbc.Driver可以是com.mysql.jdbc.Driver,只不过com.mysql.jdbc.Driver已经过时。
  • serverTimezone=UTC在使用新版本的MySQL-JdbcDriver时务必添加这一句,否则会报错。
  • type-aliases-package指向实体类包名。
  • mapper-locations指向mapper.xml文件位置。

4、SpringBoot主方法运行

SpringBoot需要依赖一个主方法运行,我们定义这个类为SpringBootDemoApplication

代码如下:

@SpringBootApplication
@MapperScan(basePackages = "com.springboot.xu.SpringBootDemo.mapper")
public class SpringBootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

备注:

  • SpringBootApplication注解表示,这个是SpringBoot的入口方法。
  • MapperScan表示需要扫描哪个包,即mybatis的mapper接口的位置。

5、直接运行这个main方法

01

上图便是运行成功的截图。

然而就像SpringMVC一样,我们目前并没有写任何一个控制器,以至于我们没有任何一个页面。

6、控制器RestController(View展示层)

@RestController
public class HelloPage {
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String helloWorld() {
        return "Hello Spring Boot.";
    }
}

备注:

  • RestController是Rest风格的Controller,Rest风格是指在http协议中的get,post,put,delete方式来区分操作数据的方式。
  • RequestMapping和SpringMVC用法一致。

运行效果:

02