SpringBoot学习笔记1 - 20181127

一.引用

  1. 作用
  • 用来简化Spring应用的初始化搭建以及开发过程。
  • 快速项目构建,简化配置
  • Springboot(微框架)=Spring(项目管理框架)+SpringMVC(控制器)
  1. 规范(约定俗成)
  • 在底层子包外,有一个Application.java
    入口类,一个springboot项目,有且只有一个
  • springboot的约定(就是配置,也称”约定大于配置“)
    applicaton.yml 或 application.properties,且必须在src/main/resources根目录
  1. springboot的特点
    ①创建独立的spring应用
    ②嵌入Tomcat,无需部署war文件
    ③简化Maven配置
    ④自动配置Spring
    ⑤没有xml的配置

  2. 开发环境要求
    ①Maven 3以上
    ②Spring FrameWork 4以上
    ③Jdk1.7以上 boot2在1.8以上
    ④springboot 1.5以上

二.Springboot第一个搭建环境

  1. pom.xml依赖
	<!-- springboot的父级项目依赖 -->
	<parent>
   	 	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-parent</artifactId>
    	<version>1.5.7.RELEASE</version>
	</parent>

	<!-- springboot的web依赖 -->
    <dependency>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 测试 -->
    <dependency>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-test</artifactId>
      	<!-- 只在test测试里面运行 -->
      	<scope>test</scope>
    </dependency>
  1. 书写springboot的入口类 Application.java
@SpringBootApplication
public class Application {
	//启动springboot应用
    public static void main(String[] args) {
		//参数1:springboot入口类对象、参数2:main函数的参数
        SpringApplication.run(Application.class,args);
    }
}

@SpringBootApplication
修饰范围:只能用在入口类上 唯一
作用:
①.代表当前应用是一个springboot构建的应用
②.代表这个类是springboot的入口应用

  1. 书写springboot的约定 application.yml
server:
  port: 9090
  context-path: /springboot

三.SpringBootApplication注解拆解

入口类上注解,是以下三个注解的组合

@SpringBootApplication		//自动配置spring相关内容
@EnableAutoConfiguration	//第三方jar与springboot自动配置
@ComponentScan				//手动指定包扫描,默认扫描入口类当前包及其包下子包

在这里插入图片描述

注意:
@SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,
并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。

其他注解:
spring4中

@RestController		//基于restful风格控制器
			//使用在controller类上,相当于@Controller + @ResponseBody
			//作用:将类中所有方法的返回值以json格式响应

在这里插入图片描述

四.更换启动banner图

在resources包下,创建banner.txt,并在其中书写内容,我的示例如下:

   ______          _____                                 ______          __   __   __
  / ____/ ____    / ___/  ____   __  __  _____  _____   / ____/ ____    / /  / /  / /
 / / __  / __ \   \__ \  / __ \ / / / / / ___/ / ___/  / / __  / __ \  / /  / /  / /
/ /_/ / / /_/ /  ___/ / / /_/ // /_/ / / /    (__  )  / /_/ / / /_/ / /_/  /_/  /_/
\____/  \____/  /____/ / .___/ \__,_/ /_/    /____/   \____/  \____/ (_)  (_)  (_)
                      /_/

五.Springboot的视图解析 与 中文乱码问题

springboot中默认的视图解析器是:thymeleaf(view层框架)
springboot集成jsp:

  1. 引入springboot对jsp页面支持依赖
	<!--jstl-->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    
    <!--引入springboot 对jsp页面的支持-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
  1. 通过约定,明确视图解析器
    注意:使用main函数方式启动默认内嵌的tomcat,不会解析jsp页面
spring:
  mvc:
    view:	#试图解析器
      prefix: /
      suffix: .jsp
  http:
    encoding:
      charset: utf-8	#中文乱码问题
      force: true
  1. 项目中引入支持jsp启动插件
<plugins>

      <!--springboot 支持jsp启动插件-->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      
</plugins>
  1. 使用插件启动,才能支持jsp页面,右侧maven中
    Pligins — springboot-boot — springboot:run
    【Lifecycle — clean 清除冗余编译】
    【springboot 1.5.x之后,使用内置tomcat8,编码为utf-8 / 之前的编码为iso-8859-1】
    【选中 single instance only 单例执行run,最方便】

六.springboot中约定的拆分(配置文件的拆分)

spring.profiles.active:xxx #激活哪个文件生效(写文件-后边单词即可)
图三

七.springboot与MyBatis集成

  1. 引入mybatis的依赖
	<!--引入springboot和mybatis整合jar-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>

    <!--引入mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

    <!--druid-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
  1. 配置application.yml
    ①数据源
spring:    
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #指定数据源
    driver-class-name: com.mysql.jdbc.Driver #指定驱动
    url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8 #指定url
    username: root
    password: root

②mapper配置文件的位置 和 实体起别名

mybatis:
  mapper-locations: classpath:com/abc/mapper/*Mapper.xml #指定mapper配置文件位置
  type-aliases-package: com.abc.entity #指定起别名的包
  1. 入口类上 加接口位置扫描
@SpringBootApplication
@MapperScan("com.abc.dao")
public class Application {
}

八.springboot本地测试JUnit

  1. 引入两个测试依赖 junit4.12 和 spring-boot-start-test
	<dependency>
      	<groupId>junit</groupId>
      	<artifactId>junit</artifactId>
     	<version>4.12</version>
     	<scope>test</scope>
    </dependency>
    
    <!-- 测试 -->
    <dependency>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-test</artifactId>
      	<!-- 只在test测试里面运行 -->
      	<scope>test</scope>
    </dependency>
  1. 创建测试类示例
//在当前类实例化启动springboot应用
//当测试类与入口类不平级时,需手动指定入口类;要求junit4.12
@SpringBootTest(classes = Application.class)
@RunWith(SpringRunner.class)
public class TestUser {
    @Autowired
    private UserDAO userDAO;

    @Test
    public void test1(){
        User user = userDAO.selectByPrimaryKey("1");
        System.err.println(user);
    }
}

ps:解决注入dao红色下划线问题!
在DAO接口加如下注解:

@Mapper
@Component(value = "userDAO")
public interface UserDAO {

@Mapper
@Component:定义Spring管理Bean,通过@Component将切面定义为Spring管理Bean,像@Service、@Controller、@Repository都是其扩展

  1. 其他
  • JUnit是一款优秀的开源Java单元测试框架,也是目前使用率最高最流行的测试框架,开发工具Eclipse和IDEA对JUnit都有很好的支持,JUnit主要用于白盒测试和回归测试。
    ~ 白盒测试:把测试对象看作一个打开的盒子,程序内部的逻辑结构和其他信息对测试人 员是公开的;
    ~ 回归测试:软件或环境修复或更正后的再测试;
    ~ 单元测试:最小粒度的测试,以测试某个功能或代码块。一般由程序员来做,因为它需要知道内部程序设计和编码的细节;
  • 在测试数据操作的时候,我们不想让测试污染数据库,也是可以实现的,只需要添加给测试类上添加“@Transactional” 即可,这样既可以测试数据操作方法,又不会污染数据库了。

九.springboot中jsp页面的热部署

  1. 引入热部署依赖(注意,先下载依赖,再配成插件)
 	  <!--springboot 支持jsp启动插件-->
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>

        <dependencies>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.7.RELEASE</version>
          </dependency>
        </dependencies>

     </plugin>
  1. 开启idea自动编译功能
    settings — Compiler — Build project automatically或Make~
  2. 配置文件中
server:
  jsp-servlet:
    init-parameters:
      development: true #开启jsp的热部署配置

十.springboot 集成 FastJSON

  1. 引入依赖
	<dependency>
     	 <groupId>com.alibaba</groupId>
     	 <artifactId>fastjson</artifactId>
     	 <version>1.2.49</version>
    </dependency>
  1. 在入口类中加入配置
	//FastJson配置
   	 @Bean   //@Bean 将配置的东西作为bean对象,交给工厂处理
   	 public HttpMessageConverters fastjsonHttpMessageConverter(){
        //定义一个转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //添加fastjson的配置信息 比如 :是否要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        //在转换器中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
  1. FastJson也可以利用 WebMvcConfigurerAdapter(非入口类方法)

猜你喜欢

转载自blog.csdn.net/weixin_42838993/article/details/84575781