springboot项目搭建web项目详细总结

1.用官网地址https://start.spring.io/   快速搭建项目

选择需要的包

 

 

生成原始项目代码,解压导入IDE

2.配置

package com.horse.red.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/sayHello")
    public String sayHello(){
        System.out.println("hello spring boot");
        return "hello spring boot";
    }
}
在启动网站前,由于spring boot 是默认自动注册加载数据库相关的类文件的,所以为了不报错,我们需要打开数据库并在resource目录下的application.property中加入数据库配置相关文件,这里以mysql为例子,配置文件如下:
application.properties中配置:
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

3.运行项目:

http://localhost:8080/sayHello

4.传参数

 

/* url传参,访问的路径类似这样:localhost:8080/getParamDemo1/1
        * 方法体中的参数要在前面加注释,@PathVariable,代表url中的参数
 */

@RequestMapping(path = {"/getParamDemo1/{id}"})
public String getParamDemo1(@PathVariable("id") int userId){
    System.out.println("get param " + userId);
    return "success get param";
}

http://localhost:8080/getParamDemo1/1

/**
 * 当然,你也可以通过这种传参方式:localhost:8080/getParamDemo?param1=1或者直接表单提交参数
 * 当然,同时方法中参数声明的注释也要变成@RequestParam,代表请求参数,required属性说明了参数是否是必须的
 */
@RequestMapping(path = {"/getParamDemo2"})
public String getParamDemo2 (@RequestParam(value="param1",required = false) int param){
    System.out.println("get param " + param);
    return "success get param";
}

http://localhost:8080/getParamDemo2?param1=1

5.构建restful编程风格

利用上面说的url中的值(类似这个:path="/member/{userid}")进行资源定位,也非常符合resultful的风格要求,例如这path="/getParamDemo1/{userid}"的配置就是对应的就是对会员级别的某个用户(由userid定位)进行某些操作,如果要删除该用户,则对应http请求的delete请求即可。

@RequestMapping(path = {"/getParamDemo2"},method = RequestMethod.GET)
public String getParamDemo2 (@RequestParam(value="param1",required = false) int param){
    System.out.println("get param " + param);
    return "success get param";
}

6.spring boot中配置数据库框架(以mybatis为例子)

application.properties

#下面这条配置声明了mybatis的配置文件路径,classpath对应的是和这个文件统计的resources mybatis.config-location=classpath:mybatis-config.xml

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <!-- Globally enables or disables any caches configured in any mapper under this configuration -->
        <setting name="cacheEnabled" value="true"/>
        <!-- Sets the number of seconds the driver will wait for a response from the database -->
        <setting name="defaultStatementTimeout" value="3000"/>
        <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- Allows JDBC support for generated keys. A compatible driver is required.
        This setting     forces generated keys to be used if set to true,
         as some drivers deny compatibility but still work -->
        <setting name="useGeneratedKeys" value="true"/>
    </settings>

    <!-- Continue going here -->

</configuration>
package com.horse.red.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface HelloDao {

    @Select({"select name from  user_info "})
    List<String> queryDemo();
}
请求方法
 @Autowired
private HelloDao helloDao;

 @RequestMapping("/sayHello")
 public String sayHello(){
     System.out.println("hello spring boot");
     List<String> names = helloDao.queryDemo();
     System.out.println(names.toString());
     return "hello spring boot";
 }

如果出现报错:

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

解决办法:

url后面增加:

jdbc.url=jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC

试了很多次这个方法都不管用后来直接修改了时区。

修改数据库时区:

 set global time_zone = '+8:00'; ##修改mysql全局时区为北京时间,即我们所在的东8区
  set time_zone = '+8:00'; ##修改当前会话时区
  flush privileges; #立即生效

 

7.springboot跳转html页面

首先在pom文件中引入模板引擎jar包,即:

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

以及web支持
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

在application.properties中配置模板引擎

spring.thymeleaf.prefix=classpath:/templates/

是让controller层到templates文件夹寻找xx.html(src/main/resources/templates)


在templates下建立index.html文件

controller层

package com.horse.red.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

    @RequestMapping("/test")
    public String test(){
        return "index";
    }

}

在浏览器中地址栏输入:http://localhost:8080/test

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值