按住Ctrl键,然后点击pom.xml中的spring-boot-starter-parent,跳转到了spring-boot-starter-parent的pom.xml,xml配置如下(只摘抄了部分重点配置):
org.springframework.boot
spring-boot-dependencies
2.3.3.RELEASE
../../spring-boot-dependencies
按住Ctrll键,然后点击pom.xml中的spring-boot-starter-dependencies,跳转到了spring-boot-starter-dependencies的pom.xml,xml配置如下(只摘抄了部分重点配置):
5.15.3
2.7.7
1.9.63
2.4.0
1.8.13
3.9.1
4.0.6
2.1.4
3.0.0
1.7.11
... ... ...
org.springframework.boot
spring-boot
2.3.1.RELEASE
org.springframework.boot
spring-boot-test
2.3.3.RELEASE
... ... ...
org.jetbrains.kotlin
kotlin-maven-plugin
${kotlin.version}
org.jooq
jooq-codegen-maven
${jooq.version}
org.springframework.boot
spring-boot-maven-plugin
2.3.3.RELEASE
... ... ...
从上面的spring-boot-starter-dependencies的pom.xml中我们可以发现,一部分坐标的版本、> > 依赖管理、插件管理已经定义好,所以我们的SpringBoot工程继承spring-boot-starter-parent后> > 已经具备版本锁定等配置了。所以起步依赖的作用就是进行依赖的传递。
按住Ctrll键,然后点击pom.xml中的spring-boot-starter-web,跳转到了spring-boot-starter-web的pom.xml,xml配置如下(只摘抄了部分重点配置):
4.0.0
org.springframework.boot
spring-boot-starters
2.3.3.RELEASE
org.springframework.boot
spring-boot-starter-web
2.3.3.RELEASE
Spring Boot Web Starter
org.springframework.boot
spring-boot-starter
2.3.3.RELEASE
compile
org.springframework.boot
spring-boot-starter-json
2.3.3.RELEASE
compile
org.springframework.boot
spring-boot-starter-tomcat
2.3.3.RELEASE
compile
org.hibernate.validator
hibernate-validator
6.0.9.Final
compile
org.springframework
spring-web
5.2.8.RELEASE
compile
org.springframework
spring-webmvc
5.2.8.RELEASE
compile
从上面的spring-boot-starter-web的pom.xml中我们可以发现,spring-boot-starter-web就是将> web开发要使用的spring-web、spring-webmvc等坐标进行了“打包”,这样我们的工程只要引入spring-boot-starter-web起步依赖的坐标就可以进行web开发了,同样体现了依赖传递的作用。
按住Ctrll键,然后点击查看启动类DemoApplication上的注解@SpringBootApplication
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注解@SpringBootApplication的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication { //... }
@SpringBootConfiguration:等同与@Configuration,既标注该类是Spring的一个配置类@EnableAutoConfiguration:SpringBoot自动配置功能开启 @ComponentScan:定义扫描路径,从中找出标识了需要装配的类,并自动装配到spring容器中
按住Ctrll键,然后点击查看处理器类 UserController上的注解@RestController
@RestController
public class UserController { }
注解@RestController的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController { //... }
可以看到:@RestController注解就相当于:@Controller+@ResponseBody
SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用SpringBoot配置文件进行配置。
SpringBoot配置文件有两种:properties文件形式、yml文件形式。SpringBoot默认会从Resources目录下加载application.properties或application.yml文件。
下面是一个 application.properties 配置文件实例:
## 修改服务器启动端口
server.port=8080
## 设置应用程序访问上下文路径
server.servlet.context-path=/elm
## 设置SpringBoot日志输出级别(error、warn、info、debug)
logging.level.org.springframework=debug
YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML文件是以数据为核心的,比传统的xml方式更加简洁。
YML文件的扩展名可以使用.yml或者.yaml。
下面是一个 application.yml 配置文件实例:
server:
port: 8080
servlet:
context-path: /elm
logging:
level:
org.springframework: debug
yml文件基本语法:
上面提及过,SpringBoot的配置文件,主要的目的就是对配置信息进行修改的,但在配置时的key从哪里去查询呢?我们可以查阅SpringBoot的官方文档文档URL: Spring Boot Reference Guide 常用的配置摘抄如下:
# QUARTZ SCHEDULER (QuartzProperties)
spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
spring.quartz.job-store-type=memory # Quartz job store type.
spring.quartz.properties.*= # Additional Quartz Scheduler properties.
# ----------------------------------------
# WEB PROPERTIES
# ----------------------------------------
# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=8080 # Server HTTP port.
server.servlet.context-path= # Context path of the application.
server.servlet.path=/ # Path of the main dispatcher servlet.
# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly.
# JACKSON (JacksonProperties)
spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, `yyyy-MM-dd HH:mm:ss`.
# SPRING MVC (WebMvcProperties)
spring.mvc.servlet.load-on-startup=-1 # Load on startup priority of the dispatcher servlet.
spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
spring.datasource.password= # Login password of the database.
spring.datasource.url= # JDBC URL of the database.
spring.datasource.username= # Login username of the database.
我们可以通过@Value注解,将配置文件中的值映射到一个Spring管理的Bean的属性上。
@RestController
public class DeptController {
//server.port 就是SpringBoot配置文件中的一个值
@Value("${server.port}")
private int port;
@Autowired
private DeptService deptService;
@RequestMapping("/listDept")
public List listDept(){
System.out.println("端口:"+port);
return deptService.listDept();
}
}
将@Value注解放置在一个属性上,就可以使用它获取SpringBoot配置文件中的值。
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
mysql
mysql-connector-java
5.1.6
runtime
在application.properties配置文件中配置MyBatis相关信息
server.port=8080
server.servlet.context-path=/elm
## logging.level.org.springframework=debug
## 配置mapper输出日志级别
logging.level.com.neusoft.demo.mapper=debug
## 配置数据源信息
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.url=jdbc:mysql://localhost:3306/elm?characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## 配置mapper映射文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
## 配置扫描实体包,给实体类设置别名
mybatis.type-aliases-package=com.neusoft.demo.po
也可以使用application.yml形式配置文件进行MyBatis相关配置:
server:
port: 8080
servlet:
context-path: /elm
logging:
level:
org.springframework: debug
com.neusoft.demo.mapper: debug
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/elm?characterEncoding=utf-8
username: root
password: 123
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.neusoft.demo.po
@Mapper
public interface DeptMapper {
@Select("select * from dept order by deptno")
public List listDept();
}
注意:必须要使用 @Mapper 标识此mapper接口
public interface DeptService {
public List listDept();
}
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptMapper deptMapper;
@Override
@Transactional //注意:需要在主启动类上添加@EnableTransactionManagement注解
public List listDept() {
return deptMapper.listDept();
}
}
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping("/listDept")
public List listDept(){
return deptService.listDept();
}
}