SpringBoot

SpringBoot

SpringBoot是简化spring集成第三方框架的过程,不需要编写大量的配置文件,可以帮助开发人员快速搭建环境

SpringBoot的特点

内置tomcat服务器
提供starter起步依赖,简化应用的配置
自动配置spring和第三方库(就是把对象创建好了放在容器)
提供了健康检查,统计,外部化配置
不用使用xml配置

如果创建springboot项目失败,解决办法

方式1:将spring初始化器的server URL的start.spring.io改成https://start.aliyun.com/
方式2:用start.springboot.io镜像地址
方式3:打开File | Settings | Appearance & Behavior | System Settings | HTTP Proxy,找到Auto…settings勾上Auto…URL,点击下方Check connection,复制start.spring.io或者https://start.aliyun.com/检查连接,如果还不行,在start.spring.io的http上加个s,或者在最后加个斜杠!

注解@Configation和@Bean的使用

@Configation:位置声明在类上.表示这是一个配置类,替代spring配置文件的功能
@Bean:位置声明在方法上,表示这个方法返回的对象交给spring容器管理,默认对象名为方法名,也可以通过name属性自定义对象名

@ImporResource的使用

@ImporResource:声明在类上,作用是导入其他的xml配置文件,使用方式是value属性=classpath:xxx.xml

@PropertyResource的使用

1.在resources目录下创建以properties结尾的文件,提供key=value数据
2.在配置类上使用@PropertyResource指定文件位置,value属性=classpath:xxx.properties
3.在属性上使用@Value,属性value=“${key}”,通过配置文件的key对应的value值赋值给属性

创建springboot项目结构
在这里插入图片描述@SpringBootApplication注解的使用说明

该注解有多个注解组成,对应着有多种功能,位置在主类上
1.可以扫描到程序中所有注解
2.自动配置功能,将spring框架和第三方框架自动创建好
3.当成配置文件使用

SpringBoot配置文件创建有两种方式

1.application.properties,key=value形式
2.application.yml,key:value形式

配置文件的相关配置

server.port : 设置端口号
server.servlet.context-path : 设置项目名(上下文路径),访问路径需要加上项目名

多环境配置文件

在不同环境下使用不同的配置文件,如果propertise格式的和yml格式的配置文件共存,会优先用propertise的
多个配置文件命名规则是application-自定义名.propertise(yml),比如开发时用的是application-dev.properties(yml),而测试用的是application-test.properties(yml)

在原生配置文件指定需要运行的配置文件:

spring.profiles.active=test,在原生的配置文件设置才会有效,其他配置文件无效,如果没有原生的配置文件,启动程序会默认使用原生的,使用的是没有任何配置的原生文件
就会运行application-test.properties(yml)

@Value注解的使用

位置在属性上,属性value=“${配置文件的key}”,获取配置文件key所对应的值,然后赋值给属性

@ConfigurationProperties注解的使用:
当@Value注解赋值的属性有很多个,可以使用一个普通的Java对象来赋值,之后注入对象属性即可,步骤如下

1.创建Java对象,类上加注解Component和@ConfigurationProperties,属性prefix的值是配置文件的前缀,该注解的作用是获取到配置文件的key,然后匹配key,如果key相同则直接赋值
2.在需要的类上以对象属性的方式注入即可

如果idea上方出现红色提示,加入以下依赖,重启idea即可解决!

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
</dependency>

-----------------------------------------------------------
SpringBoot不推荐使用JSP,如果非要用,则遵循以下步骤:

1.创建springboot项目,添加依赖tomcat-embed-jasper,负责编译jsp文件
2.在main目录下创建webapp目录,作为web项目的根,需要改变文件夹的性质
3.在webapp目录下新建index.jsp文件
4.在配置文件配置视图解析器
5.在pom文件指定配置,用来告诉springboot编译jsp后存放的目录
6.启动服务器,测试结果

<!--加入处理JSP文件的依赖-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

main目录下的webapp目录
在这里插入图片描述
jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP文件</title>
</head>
<body>
获取的数据:${data}
</body>
</html>

yml配置文件

server:
  port: 8081
  servlet:
    context-path: /jsp
spring:
  mvc:
    view:
      prefix: /
      suffix: .jsp

pom依赖文件

 <build>
        <!--指定JSP编译后存放的目录-->
        <resources>
            <resource>
                <!--jsp原来的目录-->
                <directory>src/main/webapp</directory>
                <!--指定编译后存放的目录-->
                <targetPath>META-INF/resources</targetPath>
                <!--指定处理的目录和文件-->
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
</build>

在这里插入图片描述
----------------------------------------------------------
springboot配置字符集过滤器,解决post中文乱码问题

#true表示使用系统的过滤器,false表示使用自定义的过滤器
server.servlet.encoding.enabled=true
#指定系统过滤器的字符集
server.servlet.encoding.charset=utf-8
#强制让request,response都使用charset属性的值
server.servlet.encoding.force=true

server.servlet.encoding.enabled=true,系统默认的就是true,可以不配置!

-------------------------------------------------------------
使用SpringBoot集成mybatis

1.添加依赖

  <!--mybatis的起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

2.pojo层

@Data
public class User {
    private Integer id;
    private String loginName;
    private String loginPwd;
    private String realName;
}

3.dao层

@Mapper//创建代理对象,注入到容器
public interface UserDao {

    User selectUser(@Param("id") Integer id);
}
<?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">
<!--namespace写全接口名-->
<mapper namespace="com.alibaba.springboot.dao.UserDao">
    <!--SQL语句的id写方法名,查询需要resultType属性,写实体类全类名-->
    <resultMap id="user" type="com.alibaba.springboot.pojo.User">
        <id column="id" property="id"></id>
        <result column="loginName" property="loginName"></result>
        <result column="loginPwd" property="loginPwd"></result>
        <result column="realName" property="realName"></result>
    </resultMap>
    <select id="selectUser" resultMap="user">
        select id, loginName, loginPwd, realName
        from t_user
        where id = #{id};
    </select>
</mapper>

4.controller层

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/userSelect")
    @ResponseBody
    public String userSelect(Integer id) {
        User user = userService.selectUser(id);
        return user.toString();
    }
}

5.在pom文件配置resource插件,解决不会拷贝除了resource目录下的配置文件

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <!--包括目录下的.properties,.xml 文件都会扫描到-->
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.txt</include>
                </includes>
                <!--filtering 选项 false 不启用过滤器, *.property 已经起到过滤的作用了 -->
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

6.配置属性配置文件

#设置端口
server.port=8081
#设置项目名路径
server.servlet.context-path=/springboot

#连接数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=981122

7.在浏览器输入访问路径,测试结果
http://localhost:8081/springboot/user/userSelect?id=2
User{id=2, loginName=‘lisi’, loginPwd=123, realName=‘李四’}

-----------------------------------------------

springboot中的事务

1.在service业务层public的方法上加注解@Transactional,表示方法有事务支持,如果有抛出异常就回滚事务
2.在主启动类上加注解@EnableTransactionManagement,表示启动事务管理器

在这里插入图片描述------------------------------------------------------------

restful风格

接口架构风格restful

使用resetful风格,更简洁,更有层次感
使用url表示资源,使用http动作操作资源

发送请求的方式有四种,但是实际上能用的只有get和post,因为浏览器不能识别put和delete,但是可以间接的转换提交的方式,如下
在这里插入图片描述
注解@PathVariable的使用

从url中获取数据,位置在方法的形参前面,要求形参只能是简单类型,Java对象不行

注解@RestController的使用

是@Controller和@ResponseBody的组合
在类上面使用,表示当前类的所有方法都加入了@ResponseBody

@GetMapping,@PostMapping,@PutMapping,@DeleteMapping的使用

支持get请求方式,等同于@RequestMapping(method=RequestMethod.GET),其他注解略同

--------------------------------------------------------------------

springboot的注解

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

springboot集成mybatis

  <!--mybatis的起步依赖-->
  			<!--因为不是springboot提供的,所以需要自己加版本号-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

编写mybatis相关的配置信息



#连接数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=981122

将所有dao层的mapper配置文件写在resources目录下创建的mappers目录下,然后在properties配置文件,配置mapper文件所在的位置,这步是mybatis必须要配置的

#指定mapper文件所在位置,默认以resource目录为根
mybatis.mapper-locations=classpath:mappers/*.xml
#指定mybatis的日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

在pom文件指定将resources目录中的文件,编译到指定目录中,springboot默认会自动编译resources目录下的资源,如果没有编译在去配置,跟版本有关系

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
</build>

选择性使用:
应用场景是mapper文件跟dao接口同目录下,解决无法扫描除了resources目录下配置文件的问题,如果没有将所有dao层的mapper配置文件写在resources目录下,用这个扫描编译

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <!--包括目录下的.properties,.xml 文件都会扫描到-->
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.txt</include>
                </includes>
                <!--filtering 选项 false 不启用过滤器, *.property 已经起到过滤的作用了 -->
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

springboot整合web

添加web依赖,包含了webmvc依赖

<!--整合web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

编写配置文件

#设置端口
server.port=8081
#设置项目名路径
server.servlet.context-path=/springboot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值