SpringBoot学习笔记

作者: Mr_欢先生 | 来源:发表于2018-03-06 20:31 被阅读42次

1.在intellij idea中创建一个spring boot项目

步骤如下:




完成傻瓜式操作后就可以创建一个springboot项目结构如下图


2.创建一个controller

@RestController
@RequestMapping("")
public class UserController {

    @RequestMapping("")
    public String index(){
        return "Hello World!";
    }
}

项目结构如下图所示



启动main函数控制台出现如下所示说明启动成功



浏览器访问:默认端口8080

注解解释:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。


3.整合Mybatis到springboot

在pom中添加以下依赖

<!--===============添加mybatis依赖================-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

application.properties中添加配置

spring.datasource.url=jdbc:mysql://localhost:3306/web?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

创建数据表

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

浏览器输入访问接口:http://localhost:8080/query结果如图所示

此时项目结构如下

注解:
@ComponentScan注解开启后会扫描到项目中所有注解
@MapperScan 可以替代dao层接口的@mapper注解,作用是:使用此注解可以注册 Mybatis 接口类


4.设置静态资源前后缀

#配置静态资源前后缀
spring.mvc.view.prefix=/web/
spring.mvc.view.suffix=.html

设置PageController页面接口

@Controller
@RequestMapping
public class PageController {
    @RequestMapping("index")
    public String index(){
        return "index";
    }
}

访问接口:http://localhost:8080/index结果如下所示


此时项目结构如下所示:

5.切换application.properties 为application.yml

yml和properties文件是一样的原理,yml属性结构是将配置文件中的信息树形展示出来,更便于查看,但语法要求比较严格。

  • application.properties
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/web?useUnicode=true&characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = root

#=========Mybatis 相关配置==================
#mapper文件
mybatis.mapperLocations=classpath:mapper/*.xml
#可省略写mybatis的xml中的resultType的全路径
mybatis.type-aliases-package=com.example.systemspringboot.entity

#配置静态资源前后缀
spring.mvc.view.prefix=/web/
spring.mvc.view.suffix=.html
  • application.yml
spring:
  datasource:
    password: root
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/web?useUnicode=true&characterEncoding=utf8
    username: root
  #配置静态 资源前后缀
  mvc:
    view:
      prefix: /web/
      suffix: .html
#=========Mybatis 相关配置==================
#1.mapper文件
#2.可省略写mybatis的xml中的resultType的全路径
mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.example.systemspringboot.entity
  • 推荐一个将application.properties 转换为application.yml的网站:地址

所属文集:SpringBoot学习

项目地址:GitHub

相关文章

网友评论

    本文标题:SpringBoot学习笔记

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