通过parent标签指定pom项目的父工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ali</groupId>
<artifactId>springBoot_test_01</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
创建controller及启动类
controller层:
package com.zi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class FirstController {
@ResponseBody
@RequestMapping("/first")
public String firstController(){
return "hello SpringBoot...";
}
}
package com.zi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
启动类需要与controller层、service层等包同层级【SpringBoot默认扫描启动类所在包及其子包】
在工作中我们有可能遇到我们的maven项目已经要继承一个parent项目了,此时应该使用dependencyManagement来创建SpringBoot项目
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ali</groupId>
<artifactId>springBoot_test_01</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<!--<parent>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-parent</artifactId>-->
<!-- <version>2.5.4</version>-->
<!--</parent>-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
</project>
SpringBoot中的配置文件只能是以application开头【application*】
# 设置端口
server.port=8081
# 设置上下文路径
server.servlet.context-path=/springboot02
# yml格式的文件,区分大小写,且层级之间要有两个空格;
# 属性与值之间要留一个空格
server:
port: 8888
servlet:
context-path: /springboot02
注意:
①yml也可以配置数组或者json格式的数据;
②如果同时存在properties与yml文件,如果存在冲突,以properties为准
访问路径上不需要加/static或者/public【并且二者之间可以互相访问】
a.html代码:
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<img src="logo.jpg"/>
body>
html>
类似于JavaWeb中的WEB-INF里面的内容,属于私密内容,外界无法直接访问,只能通过内部跳转访问
【FreeMarker thymeleaf 页面所在目录 】
SpringBoot的项目结构中不同于以前的JavaWeb项目,没有webapp目录【只有需要用到jsp技术,才会有该目录】
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
对应包结构:
package com.zi.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
private String email;
}
UserMapper接口:
package com.zi.mapper;
import com.zi.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
/**
* 查询所有用户
* @return
*/
List<User> selectAll();
}
UserMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zi.mapper.UserMapper">
<select id="selectAll" resultType="user">
select * from t_user
select>
mapper>
UserService:
package com.zi.service;
import com.zi.pojo.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
UserServiceImpl:
package com.zi.service.impl;
import com.zi.mapper.UserMapper;
import com.zi.pojo.User;
import com.zi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.selectAll();
}
}
package com.zi.controller;
import com.zi.pojo.User;
import com.zi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
}
地址栏内容:
最终数据:
logback的依赖,我们不需要亲自引入,spring-boot-starter或者spring-boot-starter-web中已经包含了Logback的依赖。
在resources目录下创建logback.xml:
<configuration>
<property name="LOG_HOME" value="${catalina.base}/logs/"/>
<appender name="Stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
layout>
appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/server.%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>30MaxHistory>
rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}
[%thread] %-5level %logger{50} - %msg%n
pattern>
layout>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MBMaxFileSize>
triggeringPolicy>
appender>
<root level="info">
<appender-ref ref="Stdout"/>
<appender-ref ref="RollingFile"/>
root>
<logger name="com.zi.mapper" level="DEBUG"/>
configuration>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpService empService;
@ResponseBody
@RequestMapping("/findByPage/{pageNum}/{pageSize}")
public List<Emp> findAll(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
return empService.findByPage(pageNum, pageSize);
}
}
EmpMapper:
@Mapper
public interface EmpMapper {
List<Emp> findAll();
}
EmpMapper.xml:
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zi.mapper.EmpMapper">
<select id="findAll" resultType="emp">
select * from emp
select>
mapper>
EmpService:
public interface EmpService {
List<Emp> findAll();
List<Emp> findByPage(Integer pageNum, Integer pageSize);
}
EmpServiceImpl:
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public List<Emp> findAll() {
return empMapper.findAll();
}
@Override
public List<Emp> findByPage(Integer pageNum, Integer pageSize) {
Page<Emp> page = PageHelper.startPage(pageNum, pageSize);
List<Emp> emps = empMapper.findAll();
PageInfo<Emp> pi =new PageInfo<>(emps);
System.out.println("当前页"+pi.getPageNum());
System.out.println("总页数"+pi.getPages());
System.out.println("页大小"+pi.getSize());
System.out.println("总记录数"+pi.getTotal());
System.out.println("当前页数据"+pi.getList());
return emps;
}
}
①地址栏中输入:
②响应结果: