maven配置
在maven conf sttings.xml中增加阿里云镜像和jdk1.8
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
2.SpringBoot开发流程
2.1、创建maven工程
2.2、引入依赖
在pom.xml中增加依赖:
使用Springboot必要的父类
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
开发web需要的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Spring Tools 4 for Eclipse 还为我们提供了更加便捷的项目创建方式,在 File->New 选项中有 Spring Starter Project,可以直接选择 Spring Boot 的版本以及需要依赖的第三方包,直接生成 Spring Boot 项目,不用再去手动配置 Maven 依赖。
2.3、创建主程序
/**
* 主程序类
* @SpringBootApplication:这是一个SpringBoot应用
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
springBoot启动程序的目录一定要在controller等目录的至少上一级,或者在SpringBootApplication 注解中添加属性:
@SpringBootApplication(scanBasePackages="controller")
(默认的包结构
○主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
○无需以前的包扫描配置
○想要改变扫描路径,@SpringBootApplication(scanBasePackages="com.atguigu")
或者@ComponentScan 指定扫描路径,@ComponentScan注解包含在@SpringBootApplication注解中,因此这两个注解不能同时使用在一个类上。但@SpringBootApplication等同于@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
)
2.4、编写业务
在主程序的同级目录下,新建一个controller包,一定要在 同级或者下级目录下,否则识别不到
例如:
@RestController
//=@Controller+@ResponseBody
//这是一个控制器,处理请求
public class HelloController {
@RequestMapping("/hello") //映射请求
public String handle01(){
return "Hello, Spring Boot 2!";
}
}
2.5、测试
直接运行main方法
2.6、简化配置
在resourses中创建配置文件application.properties或者用.yml
所有配置都可以写在这里
例如:
server.port=8888
2.7、简化部署
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
把项目打成jar包,直接在目标服务器执行即可。
注意点:
取消掉cmd的快速编辑模式
自动配置
SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面
依赖管理
父项目做依赖管理
依赖管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
他的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制。
无需关注版本号,自动版本仲裁
1、引入依赖默认都可以不写版本
2、引入非版本仲裁的jar,要写版本号。
可以修改默认版本号
1、查看spring-boot-dependencies里面规定当前依赖的版本用的 key。
2、在当前项目里面重写配置
例如:
<properties>
<mysql.version>5.1.43</mysql.version>
</properties>
开发导入starter场景启动器
https://www.yuque.com/atguigu/springboot/qb7hy2
Spring注解:
@Autowired :
Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。
@PreAuthorize:
进入方法前的权限验证,@PreAuthorize 声明这个方法所需要的权限表达式,例如:@PreAuthorize(“hasAuthority(‘sys:dept:delete’)”),根据这个注解所需要的权限,再和当前登录的用户角色所拥有的权限对比,如果用户的角色权限集Set中有这个权限,则放行;没有,拒绝.@PreAuthorize(“@ac.hasPermi(‘dept:list’)”)是调用别名为ac类的hasPermi方法。
@GetMapping
是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写.该注解将HTTP Get 映射到 特定的处理方法上.
@Data
主要作用是提高代码的简洁,使用这个注解可以省去实体类中大量的get()、 set()、 toString()等方法。
@TableField(exist = false)
注解加在bean属性上,表示当前属性不是数据库的字段,但在项目中必须使用。