总结
SpringBoot启动web项目配置
-
springboot配置父节点:配置后其他相关引入不需要version配置,springboot会自动去找适合的版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent>
-
配置web依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
springboot运行方式
-
直接idea上main运行
-
java -jar xxx.jar
-
配置打包插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
-
然后maven打包
-
dos窗口java -jar xxx.jar
-
热部署
-
原理
- 代码更改后,重新启动比不同停止再启动快
- 深层原理:就是底层使用了两个ClassLoader,一个会加载没有改变的类(第三方jar包),另一个会加载自己更改的类(舍弃原来的,新创建一个)
-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <!--不加这个,热启动会不起作用--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
-
然后ctrl+f9
配置文件
-
application.properties 传统方式
-
application.yml 推荐使用
-
多环境配置
-
一个文件里
spring: profiles: active: dev --- server: port: 80 spring: profiles: dev --- server: port: 8081 spring: profiles: pro
-
多个文件
<!--主文件--> spring: profiles: active: dev <!-- 另一个文件 以application-dev.yml 还有一个application-xxx.yml --> server: port: 8081
-
-
springboot测试
-
依赖
<!--测试包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--web包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
测试类
@RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class UserTest { @Autowired private User user; @Test public void test(){ System.out.println(user); } }
Thymeleaf模板
-
依赖
<!--thymeleaf依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
-
在classpath:/templates/中放html页面,就能自动渲染
-
html导入命名空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
-
html中的标签属性操作(如:th:text)
-
application.yml设置后缀和前缀
spring: thymeleaf: suffix: .html prefix: classpath:/tem/
整合mybatis
-
依赖
<!-- mysql 数据库驱动. --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--mybatis依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
-
配置四大金刚、别名、映射文件、扫描mapper包
@SpringBootApplication @MapperScan("cn.itsource.mapper") public class App { public static void main(String[] args) { SpringApplication.run(App.class); } } <!--application.yml--> spring: datasource: url: jdbc:mysql:///mybatis driver-class-name: com.mysql.jdbc.Driver username: root password: 666 mybatis: type-aliases-package: cn.itsource.domain mapper-locations: classpath:cn/itsource/mapper/*Mapper.xml
事务
-
控制事务就直接在上面打Transactional
-
在类上打就类中所有一个事务
-
方法上打就一个方法一个事务
-
常用4个事务
-
REQUIRED:支持当前事务,如果没有事务,则新创建一个事务 SUPPORTS:支持当前事务,当前没有事务,就不加事务 REQUIEDS_NEW:新建事务,如果当前有事务,则把事务挂起 NEVER:不支持,如果当前没有事务,则抛出异常
-