1227JAVA学习

总结

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 推荐使用

    • 多环境配置

      1. 一个文件里

        spring:
          profiles:
            active: dev
        
        ---
        server:
          port: 80
        spring:
          profiles: dev
        
        ---
        server:
          port: 8081
        spring:
          profiles: pro
        
      2. 多个文件

        <!--主文件-->
        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:不支持,如果当前没有事务,则抛出异常
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值