springboot基本使用总结

Spring Boot基本使用

0. 依赖

<!--继承springboot父项目-->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.7.RELEASE</version>
</parent>

<dependencies>
    <!--引入springboot的web支持-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--引入jsp-->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <!--引入jstl-->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!--引入springboot-mybatis依赖-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.3</version>
    </dependency>
    <!--引入druid-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.12</version>
    </dependency>
    <!--引入mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>
    <!--引入lombok-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.8</version>
      <scope>provided</scope>
    </dependency>
    <!--引入测试依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

1. 入口类

package ace.gjh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

注意: 入口类的名字可以随意起,但入口类的位置决定了入口类能扫描组件的范围。

2. 配置文件

application.yml 应放在resource目录下,应用名必须为application.yml 或者application.yaml 或者application.properties

下面以application.yml为例:

server:
  port: 8989 #端口
  context-path: /dangdang #指定应用名
  jsp-servlet: 
    init-parameters:
      development: true #开启jsp热部署

注意:如果要访问Jsp文件,需要通过下面的插件启动spring boot,启动时通过双击该模块Maven的Plugins下面的spring-boot下面的spring-boot:run来启动spring boot

<build>
  <finalName>dangdang</finalName>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <!--使用热部署出现中文乱码解决方案-->
      <configuration>
        <fork>true</fork>
        <!--增加jvm参数-->
        <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
        <!--指定入口类-->
        <mainClass>ace.gjh.Application</mainClass>
      </configuration>
    </plugin>
  </plugins>
</build>

3. 其它

在resource文件下加入banner.txt文件可以改变启动时的logo,下面是博主最喜欢的一个banner。

                             .::::.                        
                           .::::::::.                      
                           :::::::::::                     
                           ':::::::::::..                  
                            :::::::::::::::'               
                             ':::::::::::.                 
                               .::::::::::::::'            
                             .:::::::::::...               
                            ::::::::::::::''               
                .:::.       '::::::::''::::                
              .::::::::.      ':::::'  '::::               
             .::::':::::::.    :::::    '::::.             
           .:::::' ':::::::::. :::::      ':::.            
         .:::::'     ':::::::::.:::::       '::.           
       .::::''         '::::::::::::::       '::.          
      .::''              '::::::::::::         :::...      
   ..::::                  ':::::::::'        .:' ''''     
..''''':'                    ':::::.'                      
美女保佑         永无Bug         永不宕机         永不修改

Spring Boot 整合 Mybatis

1. 配置文件

server:
  port: 8989
  context-path: /dangdang
  jsp-servlet:
    init-parameters:
      development: true #开启jsp热部署

spring:
  mvc:
    view:
      prefix: /
      suffix: .jsp
  profiles:
    active: dev  #开发环境需要加载resource下的`application-dev.yml`,生产环境加载`application-prud.yml`
  datasource: #数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
mybatis:
  mapper-locations: classpath:ace/gjh/mapper/*.xml #加载mapper文件
  type-aliases-package: ace.gjh.entity #实体类的简称

2. mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="ace.gjh.dao.TestDao">

    <select id="selectAll" resultType="User">
        select id, name, age from t_user
    </select>

</mapper>

3. 主类文件

在主类上添加dao接口文件的包名

package ace.gjh;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("ace.gjh.dao")
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4. 对事务的支持

package ace.gjh.service;

import ace.gjh.dao.TestDao;
import ace.gjh.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TestDao testDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<User> queryAll() {
        return testDao.selectAll();
    }
}

Spring Boot测试类

在测试类上添加下面的注解

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)

测试base类

package ace.gjh.test;

import ace.gjh.Application;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TestSpringBootBase {

}

Spring Boot-Mybatis测试类

package ace.gjh.test;

import ace.gjh.entity.User;
import ace.gjh.service.TestService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class TestServiceTest extends TestSpringBootBase{

    @Autowired
    private TestService testService;

    @Test
    public void testQueryAll(){
        List<User> users = testService.queryAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

}

Spring Boot热部署

Jsp热部署

1. 配置文件
server:
  port: 8989 #端口
  context-path: /dangdang #指定应用名
  jsp-servlet: 
    init-parameters:
      development: true #开启jsp热部署

devtools热部署

  1. 依赖

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-devtools</artifactId>
       <optional>true</optional>
    </dependency>
    
  2. 设置IDEA中的支持自动编译

    # 1.开启自动编译
    
    	Preferences | Build, Execution, Deployment | Compiler -> 勾选上 Build project automatically 这个选项
    
    # 2.开启允许在运行过程中修改文件
    	ctrl + alt + shift + / ---->选择1.Registry ---> 勾选 compiler.automake.allow.when.app.running 这个选项
    
  3. 检测热部署是否生效

   2019-07-17 21:23:17.566  INFO 4496 --- [  restartedMain] com.baizhi.InitApplication               : Starting InitApplication on chenyannandeMacBook-Pro.local with PID 4496 (/Users/chenyannan/IdeaProjects/ideacode/springboot_day1/target/classes started by chenyannan in /Users/chenyannan/IdeaProjects/ideacode/springboot_day1)
   2019-07-17 21:23:17.567  INFO 4496 --- [  restartedMain] com.baizhi.InitApplication               : The following profiles are active: dev
   2019-07-17 21:23:17.612  INFO 4496 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@66d799c5: startup date [Wed Jul 17 21:23:17 CST 2019]; root of context hierarchy
   2019-07-17 21:23:18.782  INFO 4496 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8989 (http)
   2019-07-17 21:23:18.796  INFO 4496 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
   2019-07-17 21:23:18.797  INFO 4496 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.20
Container : Tomcat initialized with port(s): 8989 (http)
   2019-07-17 21:23:18.796  INFO 4496 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
   2019-07-17 21:23:18.797  INFO 4496 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.20

注意:日志出现restartedMain代表已经生效,在使用热部署时如果遇到修改之后不能生效,请重试重启项目再试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭建華

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值