spring boot 基础配置
创建初始目录
创建过程选择web,勾选spring web选项。
Rest Controller
//Rest模式
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping
public String getById(){
System.out.println("spring boot is running ...");
return "spring boot is running ...";
}
}
启动服务 http://localhost:8080/books
普通maven工程改造springboot
- 创建maven工程
- pom文件继承spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
- 添加依赖spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 制作引导类Application
@SpringBootApplication
public class BaseOneApplication {
public static void main(String[] args) {
SpringApplication.run(BaseOneApplication.class, args);
}
}
隐藏相关mvn文件夹
- setting->Editor->Code Style-> File Types
starter
- 定义了当前项目使用的所有依赖坐标,已达到减少依赖配置的目的
parent
项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),达到减少冲突的目的
使用maven依赖管理变更起步依赖项(排除再加入)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除tomcat -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入jetty服务器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
配置空文件模板
除pom、src 全部删除
artifactId与新工程的模块名相同
Rest风格
动作行为
java支持8中, 日常使用4中 put、delete、get、post
演示
http://localhost:8080/books get //获取全部
http://localhost:8080/books/1 get //获取指定
http://localhost:8080/books post //添加
http://localhost:8080/books put //修改
http://localhost:8080/books/1 delete //删除
注意事项
- 是约束
- s表示此类资源
@RequestBody @RequestParam @PathVariable注解
区别
- @RequestParam用于接收ur1地址传参或表单传参
- @RequestBody用于接收json数据
- @PathVariable用于接收路径参数,使用{参数名称}描述路径参数
应用
后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广
如果发送非json格式数据,选用@RequestParam接收请求参数
采用RESTful进行开发,当参数数量较少时,例如1个,可以采用@PathVariable接收请求路径变量,通常用于传递id值
配置文件
配置文件基础信息
种类
- properties(最高)
- yml
- yaml(最低)
优先级
properties(最高) > yml > yaml(最低)
指定配置文件
project Structure -> facets-> spring
yaml语法介绍
- 字面量表示方式
- boolean: true
- float: 3.14
- int: 12
- null: ~
- String: hello world
- String: “hello world” 加引号可以识别 \ 进行转义
- data: 2023-03-08
- datatime: 2023-03-08T12:12:12:12+08:00
- 引用类型表示方式
- 数组
- subject:
-
- 1
-
- 2
-
- 3
-
- subject:[‘1’,‘2’,‘3’]
- subject:
- 对象
- enterprice: //对象
- name: march
- age:12
- subject:
-
- 1
-
- 2
-
- enterprice: //对象
- a. 数组对象
- users:
-
- name: tom
- age: 12
-
- name: march
- age: 12
-
- users: [ { name: Tom,age: 4 } ,{ name: jery,age: 12 }]
- users:
- 数组
yaml配置文件数据读取
- 配置文件中可以使用属性名引用方式引用属性 $(变量)
- 出现转义字符 可以用字符串形式识别
- 系统把数据都封装到了Environment对象
- 自定义封装对象 对象用@ConfigurationProperties(prefix = ‘’ ) 注解来指定对象
整合第三方技术
整合JUnit
自带
整合MyBatis
- 创建工程勾选 SQL -> MyBatis Framework和Mysql Driver俩个
- yml文件配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: root
- 创建DaoMapper层定义方法并使用@Select等注解来书写相关方法,或使用xml格式来写sql语句。
- 相关插件 MyBatisX xml文件sql语句和Mapper方法进行对应。
整合MyBatis-Plus
- 创建工程勾选 SQL -> Mysql Driver一个 ,Mybatis Plus 没有这个包,MyBatis Plus 简称MP
- 配置yml文件 同mybatis
- Mapper层继承BaseMapper<实体类>
@Mapper
public interface GoodsDao extends BaseMapper<Goods> {
//Ctrl+O 可以查看继承下来的方法
}
实体类映射
- 实体类名与数据库表名不一致。Goods 会被解析goods表,会与实体类不一致
- @TableName(“tb_brand”) 本实体类映射数据库tb_brand表
- 如果数据库表是 tb_goods 则可以在yml统一配置
mybatis-plus:
global-config:
db-config:
table-prefix: tb_ 全局配置 统一加tb_
id-type: auto //id自生成
- 字段映射
- 使用注解 @TableField(“company_name”)
id生成策略
整合Druid
- 引导Druid包
- yml配置Druid
基于Springboot的SSMP整合案例
SSMP整合案例
实体类开发
- 使用Lombok快速制作实体类
Dao开发
- 整合MyBatisPlus,制作数据层测试类
Service开发
- 基于MyBatisPlus进行增量开发,制作业务层测试类
Controller开发
- 基于Restful开发,使用postMan测试接口功能
页面开发
- 基于Vue+elementUI 列表、增加、删改、修改、分页、查询
基础分页IPage
测试类
IPage page = new Page(4,3);
goodsDao.selectPage(page, null);
System.out.println("当前页:"+page.getCurrent());
System.out.println("当前页码值:"+page.getSize());
System.out.println("数据总量:"+page.getTotal());
System.out.println("最大页码值:"+page.getPages());
System.out.println("当前页数据:"+page.getRecords());
要配置拦截器
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
//定义Mp拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加具体的拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
开启日志才能看到内部怎么执行
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
基础条件查询
QueryWrapper和LambdaQueryWrapper
推荐使用 LambdaQueryWrapper 可减少书写错误
@Test
void goodsListQueryWrapper() {
//QueryWrapper
QueryWrapper qw = new QueryWrapper();
qw.like("brand_name", "松鼠");
goodsDao.selectList(qw);
}
@Test
void goodsListLambdaQueryWrapper() {
String val = null;
//LambdaQueryWrapper
LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<Goods>();
lqw.like(Strings.isNotEmpty(val),Goods::getBrandName, val);
goodsDao.selectList(lqw);
}
MP业务层快速开发
业务层接口 IService
@Service
public interface IGoodService extends IService<Goods> {
}
业务层实现类 ServiceImpl<M,T>
@Service
public class implGoodService extends ServiceImpl<GoodsDao,Goods> implements IGoodService{
}