spring boot mvc入门与配置(二)

接上一博客:spring boot mvc入门与配置(一),本文内容实现基本的mvc功能

导入后的结构如下图所示

 DemoApplication类是启动类,里面有main函数,并且运行的时候要按照java application来运行

编写配置文件

#Common Application properties
#https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-properties
spring.
   thymeleaf:
    prefix: "classpath:/templates/"
    suffix: ".html"
    
#数据源基本配置
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 下面是数据库连接池配置
# Hikari will use the above plus the following to setup connection pooling
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=25
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1

 :键值对和=键值对可以混合使用

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


//@SpringBootApplication相当于下面三个配置:
//@EnableAutoConfiguration
//@ComponentScan({"com.xx","com.xx2"})
//@Configuration: 

//@ComponentScan默认扫描启动类的所在的包和子包下面所有的注解。
//https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-using-springbootapplication-annotation
//一篇比较好的关于@configuration博客文章:
//https://www.cnblogs.com/duanxz/p/7493276.html
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

所有的包和类需要在com.example.demo 下面创建

直接在应用程序包下面创建一个controller类:

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
	@GetMapping("/hello")
    public String hello() {
	 
        return "hello world";
    }
}

上述代码@RestController表示返回的是一个json字符串类型的controller

右击DemoApplication类运行项目,如下入所示

输入结果如下图所示 :

稍微深入一点的spring boot mvc开发:thymeleaf和mybatis实现网站的入门案例,请参考我的博文sping boot2整合Mybatis以及常犯错误

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值