• B058-SpringBoot


    springboot

    概念与作用

    springboot是为 spring服务的,为简化Spring项目配置而生
    它使用maven的方式对Spring应用开发进行进一步封装和简化
    是用来简化spring应用搭建,开发,部署,监控的开发工具

    简化Spring应用的搭建,开发,部署,监控的开发工具
    简单的说,它使用maven的方式对Spring应用开发进行进一步封装和简化。
    提供自动化配置
    使编码更简单,使配置更简单,使部署更简单,使监控更简单

    入门案例

    创建Maven父项目和子项目

    导入Spring Boot依赖
    父节点添加parent依赖管理 子节点添加spring-boot-starter-web依赖

    编码测试
    新建一个Controller类
    新建启动类
    浏览器测试代码运行

    
    <groupId>cn.itsourcegroupId>
    <artifactId>springboot-parentartifactId>
    
    <packaging>pompackaging>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    父子项目在pom.xml文件的互相定位
    在这里插入图片描述

    springboot运行方式

    1.直接点击main方法
    2.使用插件运行
    3.打包运行
    配置打包依赖 用package命令 到jar包所在路径cmd打开黑窗口 运行jar

    热部署

    1.添加依赖
    2.启动项目
    3.改代码
    4.重新编译(关键)

    配置文件

    application.yml
    application.properties (首选)
    1.有了properties 可以存在yml吗? 可以存在
    2.如果同时存在,我该用谁? 优先用properties,但是可以同时使用不一样的配置
    yml(推荐)
    冒号
    空格 回车/换行 缩进/tab (最后一个值,只需要空格)

    Profile多环境支持

    1.多文档块 (不推荐使用)
    将所有的环境配置写到一个yml中,通过—(必须是三个横杆)做分隔
    在这里插入图片描述
    2.多文件方式
    application-环境名.yml active表示生效环境
    在这里插入图片描述

    整合测试-springboot-test
    1.基本测试  junit
    2.基于Spring的测试
        在测试类加注解
        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration("classpath:applicationContext.xml")
    3.SpringBoot测试 - 使用流程
       导对应包/依赖				引入测试依赖包
       MyBean					@Component
       启动类						@SpringBootApplication		psvm	SpringApplication
       测试类						@RunWith(SpringRunner.class)	@SpringBootTest(classes = App.class)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = TestApp.class)
    public class TestSpringBoot {
        @Autowired
        MyBean myBean;
    
        @Test
        public void testHello(){
            System.out.println(myBean);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    Springboot-web
    1.返回json数据

    如我们的controller中的所有方法,返回的都是json格式
    那么请你使用:@RestController === @Controller + @ResponseBody

    2.返回页面(模板技术)thymeleaf

    在这里插入图片描述

    1.导入thymeleaf依赖
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-thymeleafartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    2.模板文件

    页面引入thymeleaf命名空间以支持th属性,使用th属性获取来自controller里model的数据

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>aaatitle>
    head>
    <body>
        <div th:text="${msg}">你好大兄弟div>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    3.controller

    后端用model往页面添加数据,返回页面

    @Controller
    @RequestMapping("/thy")
    public class ThyController {
    
        @RequestMapping("/index")
        public String toIndex(Model model){
            model.addAttribute("msg", "你好,thymeleaf!!!");
            return "hello";//页面路径/名称
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    因为thymeleaf是页面,需要放置到资源文件中,SpringBoot的默认配置会到resources/templates/找模板
    在这里插入图片描述

    4.启动类

    SSM整合

    1.导包

    导入mybatis核心包(mysql+jdbc)、Mybatis提供的SpringBoot依赖包、SpringBoot测试包

        <dependencies>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>1.1.1version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    2.项目目录结构+配置文件+扫描注解

    项目目录结构 domain query mapper service controller
    在这里插入图片描述
    核心配置文件yml (数据源四大金刚 扫描别名和扫描文件路径)

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql:///mybatis
        username: root
        password: root
    
    mybatis:
      type-aliases-package: cn.itsource.domain,cn.itsource.query		# 可免除实体类@Component注解
      mapper-locations: classpath:cn/itsource/mapper/*.xml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    启动类加@MapperScan扫描

    @SpringBootApplication
    @MapperScan("cn.itsource.mapper")
    public class SsmApp {
        public static void main(String[] args) {
            SpringApplication.run(SsmApp.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    3.测试

    表&实体 -> mapper接口+xml实现 -> service -> test -> controller

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = SsmApp.class)
    public class TestProduct {
        @Autowired
        ProductServiceImpl productService;
        
        @Test
        public void test(){
            productService.loadAll().forEach(a->{
                System.out.println(a);
            });
        }
    
        @Test
        public void testSave(){
            Product product = new Product("测试数据");
            productService.save(product);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    注意resource包下不能一次直接建多层包

    事务的传播机制

    一组操作同时成功或者同时失败

        @Override
        @Transactional
        public void save(Product product) {
            productMapper.save(product);
            //int i=1/0;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    只读事务 —加到查询上面

    	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    	public List<Product> loadAll() {
    	    return productMapper.loadAll();
    	}
    
    • 1
    • 2
    • 3
    • 4

    类与方法上同时存在的注解使用哪个?就近原则

        @Service
        @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
        public class ProductServiceImpl implements IProductService {
            @Autowired
            ProductMapper productMapper;
    
            @Override
            @Transactional		// 后面不写等同于@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
            public void save(Product product) {
                productMapper.save(product);
                //int i=1/0;
            }
    
            @Override
            public List<Product> loadAll() {
                return productMapper.loadAll();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    一个方法里只能有一个事务
    在这里插入图片描述
    事务的传播机制:
    REQUIRED:支持当前事务,如果当前没有事务,则新建一个事务(默认)
    SUPPORTS:支持当前事务,当前当前没有事务,就不加事务
    REQUIRES_NEW:新建事务,如果当前有事务,则把事务挂起,等着我先执行完成
    NEVER: 不支持事务,如果当前有事务,则抛出异常

    事务传播机制的作用:用来保证一组操作只有一个事务,解决事务冲突。

    @Options(useGeneratedKeys = true, keyProperty = “id”, keyColumn = “id”)
    @Insert(“insert into Demo(name,password) values(#{name},#{password})”)
    public long save(Demo name);//对象上面也有

  • 相关阅读:
    【自然资源】全域国土综合整治相关问题
    手动关闭PS中的TopazStudio2的登录窗口
    k8s中几个基本概念的理解,pod,service,deployment,ingress的使用场景
    未来可期!我国保健食品将迎来黄金时期
    oracle创建数据库,导入dmp操作全家桶
    uniApp常见面试题-附详细答案
    Spring框架中bean的生命周期
    mysql 时间字段默认设置为当前时间
    Flink-处理函数以及TopN运用案例
    前端面试第一周快速复盘,不标准的面试经验分享 (一)
  • 原文地址:https://blog.csdn.net/ffhgjgj6576567/article/details/133378762