
1.什么是springboot
2.使用springboot创建web项目
1.springboot基本配置
2.Springboot整合mybatis
创建maven项目,在pom.xml文件手动添加springboot所需得依赖关系



static:存放静态资源,比如:css,js,图片
templates:模板文件
application.properties:springboot配置文件
properties文件:key-value的格式
yml文件:阶梯状得配置格式,相同级别只能出现一次
properties文件和yml文件中有相同属性时,properties文件得优先级高于yml文件
推荐使用yml文件,格式清晰
server:
port: 8088 # 端口号
connection-timeout: 1000m # 连接超时时间server:
servlet:
context-path: /springboot02 #上下文路径,必须以/开头访问时路径:http://localhost:8090/springboot02/test
application.yml中添加配置
spring:
mvc:
view:
prefix: / # 视图前缀
suffix: .html # 视图后缀
spring:
mvc:
view:
prefix: / # 视图前缀
suffix: .jsp # 视图后缀controller中代码:
@RequestMapping("htmlTest")
public String htmlTest(){
System.out.println("跳转到html页面");
return "success"; // 作为视图名解析
}注意:springboot默认使用html页面,static目录下得资源访问时,不需要添加static路径
http://localhost:8090/springboot02/htmlTest
http://localhost:8090/springboot02/success.html


<!-- 支持跳转jsp页面 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.12</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.1</version>
</dependency>spring:
jackson:
date-format: yyyy-MM-dd HH:mi:ss # 格式化日期
time-zone: GTM+8 # 时区spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/yiibaidb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username: root
password: root参考第一部分
参考第三部分
在pom.xml中引入mybatis依赖关系
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>编写dao层,接口上添加@Mapper注解
@Mapper
public interface EmployeesDao {
// 查询所有数据
@Select("select * from employees")
public List<Employees> findAll();
// 添加
@Insert("INSERT INTO `yiibaidb`.`employees`(`employeeNumber`, `lastName`, `firstName`, `extension`, `email`, `officeCode`, `reportsTo`, `jobTitle`) \n" +
"VALUES (#{employeeNumber}, #{lastName}, #{firstName}, #{extension}, #{email}, #{officeCode}, NULL, #{jobTitle})")
int add(Employees employees);
// 修改
@Update("UPDATE `yiibaidb`.`employees` \n" +
"\tSET `lastName` = #{lastName},\n" +
"\t`firstName` = #{firstName},\n" +
"\t`extension` = #{extension},\n" +
"\t`email` = #{email},\n" +
"\t`officeCode` = #{officeCode},\n" +
"\t`reportsTo` = NULL,\n" +
"\t`jobTitle` = #{jobTitle} \n" +
"\tWHERE\n" +
"\t`employeeNumber` = #{employeeNumber}")
int edit(Employees employees);
// 删除
@Delete("delete from employees where employeeNumber = #{employeeNumber}")
int del(Integer employeeNumber);
}service代码:
@Service
public class EmployeesService {
@Resource
EmployeesDao dao;
public List<Employees> findAll(){
return dao.findAll();
}
public boolean add(Employees employees){
int i = dao.add(employees);
if (i == 1){
return true;
}else{
return false;
}
}
public boolean edit(Employees employees){
int i = dao.edit(employees);
if (i == 1){
return true;
}else{
return false;
}
}
public boolean delete(Integer employeeNumber){
int i = dao.del(employeeNumber);
if (i == 1){
return true;
}else{
return false;
}
}
}controller层:
@Controller
@RequestMapping("emp")
public class EmployeesController {
@Resource
EmployeesService empService;
@RequestMapping("findAll")
public String findAll(Model model){
List<Employees> list = empService.findAll();
model.addAttribute("list",list);
return "empManager";
}
@RequestMapping("delete")
@ResponseBody
public boolean delete(Integer employeeNumber){
return empService.delete(employeeNumber);
}
}jsp页面:
注意:添加<base href="http://localhost:8090/springboot02/"/>根路径
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<base href="http://localhost:8090/springboot02/"/>
<title>Title</title>
<script src="js/jquery-3.1.1.js"></script>
<script>
function del(employeeNumber){
$.post("emp/delete",{"employeeNumber":employeeNumber},function(data){
if(data){
alert("删除成功");
window.location.href="http://localhost:8090/springboot02/emp/findAll";
}else{
alert("删除失败");
}
});
}
</script>
</head>
<body>
<center>
<table border="1">
<thead>
<tr>
<td>employeeNumber</td>
<td>lastName</td>
<td>firstName</td>
<td>extension</td>
<td>email</td>
<td>officeCode</td>
<td>reportsTo</td>
<td>jobTitle</td>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="l">
<tr>
<td>${l.employeeNumber}</td>
<td>${l.lastName}</td>
<td>${l.firstName}</td>
<td>${l.extension}</td>
<td>${l.email}</td>
<td>${l.officeCode}</td>
<td>${l.reportsTo}</td>
<td>${l.jobTitle}</td>
<td><a rel="nofollow" href="javascript:del(${l.employeeNumber})">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</center>
</body>
</html>multipart | |
|---|---|
multipart.enabled | 是否开启文件上传支持,默认为true |
multipart.file-size-threshold | 设定文件写入磁盘的阈值,单位为MB或KB,默认为0 |
multipart.location | 指定文件上传路径. |
multipart.max-file-size | 指定文件大小最大值,默认1MB |
multipart.max-request-size | 指定每次请求的最大值,默认为10MB |
server配置 | |
|---|---|
server.address | 指定server绑定的地址 |
server.compression.enabled | 是否开启压缩,默认为false |
server.compression.excluded-user-agents | 指定不压缩的user-agent,多个以逗号分隔,默认值为:text/html,text/xml,text/plain,text/css |
server.compression.min-response-size | 执行压缩的阈值,默认为2048 |
server.context-parameters.[param name] | 设置servlet context 参数 |
server.servlet.context-path | 设定应用的context-path. |
server.display-name | 设定应用的展示名称,默认: application |
server.jsp-servlet.class-name | 设定编译JSP用的servlet,默认: org.apache.jasper.servlet.JspServlet) |
server.servlet.port | 端口号 |