POM.xml
<?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>
<!--继承SpringBoot框架的一个父项目,所有自己开发的Spring Boot都必须的继承-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--当前项目的GAV坐标-->
<groupId>com.XXXXX.springboot</groupId>
<artifactId>002-springboot-springmvc</artifactId>
<version>1.0.0</version>
<!--maven项目名称,可以删除-->
<name>002-springboot-springmvc</name>
<!--maven项目描述,可以删除-->
<description>Demo project for Spring Boot</description>
<!--maven属性配置,可以在其它地方通过${}方式进行引用-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--SpringBoot框架web项目起步依赖,通过该依赖自动关联其它依赖,不需要我们一个一个去添加了-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot框架的测试起步依赖,例如:junit测试,如果不需要的话可以删除-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--SpringBoot提供的打包编译等插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
properties文件(默认采用该文件)
#设置内嵌Tomcat端口号
server.port=9090
#配置项目上下文根
server.servlet.context-path=/003-springboot-port-context-path
#开发环境application-dev.properties
#设置内嵌Tomcat默认端口号
server.port=8080
#设置项目的上下文根
server.servlet.context-path=/006-springboot-multi-environment-dev
#生产环境application-product.properties
#配置内嵌Tomcat默认端口号
server.port=80
#配置项目上下文根
server.servlet.context-path=/006-springboot-multi-environment-product
#测试环境application-test.properties
#配置内嵌Tomcat端口号
server.port=8081
#配置项目的上下文根
server.servlet.context-path=/006-springboot-multi-environment-test
#SpringBoot的总配置文件
#激活开发环境
#spring.profiles.active=dev
#激活测试环境
#spring.profiles.active=test
#激活生产环境
spring.profiles.active=product
#配置内嵌Tomcat端口号
server.port=9090
#配置项目上下文根
server.servlet.context-path=/010-springboot-web-mybatis
#配置数据库的连接信息
#注意这里的驱动类有变化
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.92.134:3306/springboot?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
DAO其它开发方式
1.在运行的主类上添加注解包扫描@MapperScan(“com.bjpowernode.springboot.mapper”)
注释掉StudentMapper接口上的@Mapper注解
2.将接口和映射文件分开
项目名称:011-springboot-web-mybatis
因为SpringBoot不能自动编译接口映射的xml文件,还需要手动在pom文件中指定,所以有的公司直接将映射文件直接放到resources目录下;
在resources目录下新建目录mapper存放映射文件,将StudentMapper.xml文件移到resources/mapper目录下;
# 指定Mybatis映射文件的路径
mybatis.mapper-locations=classpath:mapper/*.xml
Spring MVC的注解,处理http请求
2.@RestController
Spring4后新增注解,是@Controller注解功能的增强,是@Controller与@ResponseBody的组合注解;
如果一个Controller类添加了@RestController,那么该Controller类下的所有方法都相当于添加了@ResponseBody注解;
用于返回字符串或json数据。
案例:
• 创建MyRestController类,演示@RestController替代@Controller + @ResponseBody
@RestController
public class MyRestController {
@Autowired
private StudentService studentService;
@RequestMapping("/boot/stu")
public Object stu(){
return studentService.getStudentById(1);
}
}
3.@RequestMapping(常用)
支持Get请求,也支持Post请求
4.@GetMapping
RequestMapping和Get请求方法的组合只支持Get请求;Get请求主要用于查询操作。
5.@PostMapping
RequestMapping和Post请求方法的组合只支持Post请求;Post请求主要用户新增数据。
6.@PutMapping
RequestMapping和Put请求方法的组合只支持Put请求;Put通常用于修改数据。
7.@DeleteMapping
RequestMapping 和 Delete请求方法的组合只支持Delete请求;通常用于删除数据。