- 访问 https://start.spring.io/ , 选择好之后, 点击 generate, 会生成对应的压缩包。
- idea 导入对应的文件, 设置 maven仓库和jdk(1.8)
- resources目录下添加 banner.txt,
banner生成网站: http://patorjk.com/software/taag/
- 配置application.properties文件
# 端口 server.port=8080 # 文根 server.servlet.context-path=/itboat008
- 配置 Controller:
package com.itboat008.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller// (value="/user") 这里做配置请求对应url会报404
@RequestMapping(value="/user")
public class UserController {
@RequestMapping("getUser")
@ResponseBody
public String getUser(){return "itboat008";}
}
- 启动类
package com.itboat008.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
项目结构:
- 启动项目后, 访问 : http://localhost:8080/itboat008/user/getUser
package com.itboat008.springboot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController// (value="/order") 这里做配置请求对应url会报404
@RequestMapping("/order")
public class OrderController {
@GetMapping("/getOrder")
public String getOrder(){return "asd";}
}
注意:
@RestController 和 @Controller 配置 value属性不会作用到 请求路径中去, 需采用 @RequestMapping里面的
属性去拼接请求url。
使用thymeleaf动态模板 引用pomController 中配置页面动态传参org.springframework.boot spring-boot-starter-thymeleaf
package com.itboat008.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@Controller// (value="/user") 这里做配置请求对应url会报404
@RequestMapping(value="/user")
public class UserController {
@RequestMapping("sayHello")
public String sayHello(Map params){
String hello = "hello , my name is itboat008";
params.put("hello",hello);
return "User";
}
}
classpath下的templates中添加User.html
Title
application.properties中添加thymeleaf配置
server.port=8080 server.servlet.context-path=/itboat008 #thymelea模板配置 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html #HTML5已经弃用 spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 #页面不缓存 spring.thymeleaf.cache=false注意:
因为spring.thymeleaf.prefix配置的是classpath路径下, 修改html或者添加html需要重启或者maven重新clean install,才能生效。
springboot 整合 h2 + MyBatis 注意:当maven打包出现failure时候, 需要看具体的异常:
#maven 打包命令, 打印异常堆栈, 忽略测试用例 mvn clean install -e -X -DskipTests
报错内容:
org.apache.maven.plugin.MojoExecutionException: Input length = 1
原因是缺失 maven插件, 在pom.xml中添加 如下配置,重新clean install之后,问题解决
maven 中添加依赖:org.apache.maven.plugins maven-resources-plugin 2.7 org.apache.maven.shared maven-filtering 1.3
com.h2database h2 runtime org.mybatis.spring.boot mybatis-spring-boot-starter org.springframework.boot spring-boot-starter-jdbc org.mybatis mybatis-spring 1.3.2 org.mybatis mybatis 3.5.0 com.github.pagehelper pagehelper-spring-boot-starter 1.2.5 com.github.pagehelper pagehelper 5.1.4
application.properties中添加配置
#####################mybatis###########################
mybatis.mapper-locations=classpath:mappings
private int pageNum;
private int pageSize;
private long totalSize;
private int totalPages;
private List> content;
}
PageUtils:
package com.itboat008.springboot.utils;
import com.github.pagehelper.PageInfo;
public class PageUtils {
public static PageResult getPageResult(PageRequest pageRequest, PageInfo> pageInfo) {
PageResult pageResult = new PageResult();
pageResult.setPageNum(pageInfo.getPageNum());
pageResult.setPageSize(pageInfo.getPageSize());
pageResult.setTotalSize(pageInfo.getTotal());
pageResult.setTotalPages(pageInfo.getPages());
pageResult.setContent(pageInfo.getList());
return pageResult;
}
}
测试:
-
访问 http://localhost:8080/itboat008/user/findById?id=1
-
访问 http://localhost:8080/itboat008/user/findUsersByCompanyId/1
-
访问: http://localhost:8080/itboat008/user/findUserByPage
返回:
{
"pageNum": 3,
"pageSize": 2,
"totalSize": 6,
"totalPages": 3,
"content": [
{
"id": 5,
"userName": "欧阳锋",
"userCode": "ouyangfeng",
"companyId": 2
},
{
"id": 6,
"userName": "一灯",
"userCode": "yideng",
"companyId": 3
}
]
}
- 访问 http://localhost:8080/itboat008/user/deleteById?id=1
查询数据库:
-
访问: http://localhost:8080/itboat008/user/addUser
-
访问 http://localhost:8080/itboat008/user/updateUser



