SpringBoot详解+集成JSP/FreeMarker/JDBC/Mybatis

springboot概念

  • 目的:简化Spring应用的搭建和开发过程 , 有特定的配置JavaConfig ,无需xml配置
  • 特点:-
    • 快速创建独立的应用
    • 内嵌的tomcat
    • 自动配置Spring
    • 无需xml配置
    • 提供了生产就绪,健康检查等工具包
    • 默认打包jar
    • 简化maven,方便的何三方框架集成

1.搭建jar工程
2.导入依赖
  • 继承spring-boot-starter-parent
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
 </parent>
  • 导入spring-boot-相关的jar
<!--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>
        </dependency>
        
3.编写一个类(controller+配置)
  • @RestController
  • @EnableAutoConfiguration
  • main方法 : SpringApplication.run
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationMybatis {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationMybatis.class,args);
    }
}

HelloWorld分析

问题分析
为什么使用的是jar项目:jar

应用SpringBoot默认的打包方式就是jar,默认把项目部署到内嵌的tomcat中

继承的spring-boot-starter-parent是干嘛的

SpringBoot的父项目,这里项目帮我们管理了很多基本的依赖(用过的没用过的都在里面),我们要用这些依赖需要在我们的项目中吧依赖写错来,版本号不写,交给SpringBoot的parent项目统一管理

引入的spring-boot-starter-web包是干嘛的

SpringBoot和SpringMvc整合的依赖,这个包把SpringMvc相关的包都引入进来了 ,包括tomcat的包,Spring相关的包,日志相关包,json相关的包,自动配置的包等都引入进来了

@EnableAutoConfiguration标签

开启SpringBoot的自动配置功能(spring-boot-starter-autoconfigure.jar)

EnableAutoConfiguration
 -AutoConfigurationImportSelector 

EnableAutoConfiguration标签引入了一个 AutoConfigurationImportSelector一个自动配置的选择器, 它会去找自动配置的jar包(spring-boot-starter-autoconfigure.jar) 里面的 META-INF/spring.factories文件里面的 EnableAutoConfiguration配置项下的一些自动配置的类,找到这些类加载并做自动配置。比如:DispatcherServletAutoConfiguration就帮我们配置 DispatcherServlet , WebMvcAutoConfiguration就是帮我们对SpringMvc做自动配置,比如视图解析器就在里面。

为什么是使用main方法运行程序SpringApplication.run(Example.class, args);
  • 加载程序
  • 解析相关配置
  • 完成自动装配
  • 打包项目到内嵌的tomcat中
  • 启动内墙的tomcat
为什么没有部署tomcat

SpringBoot内墙tomcat,启动的时候自动把项目打包的内嵌的Tomcat中

拓展标签

@Configuration

配置标签,它是Spring的配置标签,只要贴了这个标签的类就可以被识别为Spring的配置类(相当于是SpringContext.xml这样的配置文件就有了)

@Bean

它是Spring 的bean的定义标签,就是打了这个标签的方法 , 返回的对象会自动交给Spring容器管理,相当于是

@ComponentScan

ioc自动扫描,相当于是<context:componentScan package="…"* 默认扫描当前包及其子包

@SpringBootApplication

该标签就包含了如下三个标签

@SpringBootConfiguration

​ - @Configuration :Spring配置标签

@EnableAutoConfiguration :开启自动配置标签

@ComponentScan :组件扫描标签

@ConfigurationProperties

可以根据前缀 prefix 属性过滤出properties文件中的配置 ,自动的把这些配置项绑定到该标签所在的类中,根据名字赋值。

@PropertySource(“clssspath:xxx.properties”)

导入xx.properties配置文件

@ImportResource(“classpath:xxx.xml”)

导入xml配置

SpringBoot热部署

引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>

注意:在idea中需要手动编译一下,ctrl+F9

热部署原理

是在发现代码有更改之后,重新启动应用,但是速度比手动停止后再启动还要更快,更快指的不是节省出来的手工操作的时间。
其深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类(自己写的),称为 restart ClassLoader
,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间(5秒以内)

SpringBoot集成JSP

创建webapp工程 - war
导入依赖
<!--添加spring-boot-starter-web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <!-- servlet 依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- tomcat 的支持. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
编写jsp页面在这个路径下面/WEB-INF/views/
配置jsp

application.properties

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
  • 前缀
  • 后缀
创建controller
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApplicationConfig {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class,args);
    }
}
创建配置类

注意:启动 - 在run 下面的edit Config要选择工作空间为 MODULE_WORKING_DIR

SpringBoot集成Freemarker

创建java项目 - jar
导入依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
编写模板文件

resources/templates/xxx.ftl

在这里插入图片描述

编写controller
创建配置类
@SpringBootApplication
public class ApplicationConfigFree {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfigFree.class,args);
    }
}
Freemarker的配置

application.properties

# FreeeMarker 模板引擎配置
#       设定ftl文件路径
spring.freemarker.tempalte-loader-path=classpath:/templates
#        关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

YAML配置

YAML是YAML Ain’t Markup Language递归缩写,是YAML不是标记语言的意思,读音“yamel”(或者“雅梅尔”)。YAML是便于人阅读基于unicode编码的各种语言的序列号标准。它的用途广泛,用于配置文件,日志文件,跨语言数据共享,对象持久化,复杂的数据结构。
原则:
1、大小写敏感
2、使用缩进表示层级关系
4、缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级。
5、使用#表示注释
6、字符串可以不用引号标注

打包运行

引入插件
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>utf-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                    <mainClass>cn.itsource.freemark.FreemarkTest</mainClass> <!--主类 包含main-->
                    <layout>JAR</layout>
            </configuration>
        </plugin>
    </plugins>
</build>
使用package命令打包

使用idea中的maven工具的package命令打包 或 mvn clean package spring-boot:repackage

运行jar:java -jar xxx.jar

Profiles多环境

方式一:创建多个配置文件
创建配置dev

application-dev.properties

创建配置test

application-test.properties

创建主配置

application.properties 中 使用spring.profiles.active=test 指定使用哪个配置

方式二:创建一个配置文件
创建主配置

创建三个环境配置 ,使用 — 分开

spring:
	profiles:
		ative: dev  #指定使用dev
---
#开发
spring:
	profiles: dev
...

---
#测试
spring:
	profiles: test
...

SpringBoot测试

导入依赖
测试类贴标签
  • RunWith(SpringRunner.class)
  • SpringBootTest(classes=主程序配置类.class)

整合JDBC

导入依赖
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
配置datasource

application.proerties 配置spring.datasource开头的在这里插入代码片

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql:///crm
    driver-class-name: com.mysql.jdbc.Driver
创建DAO,service
JdbcTemplate

使用 JdbcTemplate做持久操作

@Repository
public class EmployeeDaoImpl implements IEmployeeDao {
    @Autowired
    private JdbcTemplate jdbcTemplate ;
    @Override
    public List<Employee> selectAll() {
        String sql = "select id, user_name,real_name from employee" ;
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class));
    }
}
测试

Mybatis整合

导入依赖
- 导入mybatis相关包

	<!--mybatis相关包-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
		
   		<!--数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.9</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>3.4.2</version>
    </dependency>
创建基本组件

domain,mapper,mapper.xml,service,controller

创建主配置类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan("mapper映射接口的全路径名")
@ComponentScan("需要扫描的包路径")
public class ApplicationMybatis {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationMybatis.class,args);
    }
}
配置datasource

如果要配置自定义的dataSource类型 使用 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 指定

集成mybatis
  • 配置mybatis.mapper-locations
  • 配置别名mybatis.type-aliases-package (非必须)
  • 配置mybatis配置文件路径mybatis.config-location (非必须)
  • 配置类贴@MapperScan
spring.datasource.url = jdbc:mysql://localhost:3306/crm
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.mapper-locations=classpath:cn/itsource/springboot/mapper/*.xml
测试

配置事务

注解配置
  • @EnableTranstractionManager - 贴启动类上
  • @Transcational - 贴service上
xml配置
  • 创建xml 配置事务
  • 配置类导入xml配置 @ImportResource
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值