您的位置:首页 > 健康 > 养生 > 1.Spring Boot 简介(Spring MVC+Mybatis-plus)

1.Spring Boot 简介(Spring MVC+Mybatis-plus)

2024/10/6 2:29:20 来源:https://blog.csdn.net/m0_61086522/article/details/140622512  浏览:    关键词:1.Spring Boot 简介(Spring MVC+Mybatis-plus)

文章目录

    • 一,`Spring Boot` 简介
    • 二,搭建springboot项目并整合mybatis-plus框架
      • 1.pom导依赖
      • 2.添加启动项
      • 3.配置文件`.yml`
    • 三,springboot集成` Spring MVC`
      • 1.springmvc定义
      • 2.应用注解

一,Spring Boot 简介

SpringBoot是Spring的子工程(或是spring的脚手架),快速搭建spring项目,自动配置了Spring 应用程序和第三方库

(spring+mybaties+web(servlet)+ reids+ 消息中间件),而且使用很少xml配文件,提高开发效率。

二,搭建springboot项目并整合mybatis-plus框架

1.pom导依赖

  <!--  springboot项目依赖的父工程--><!--springboot项目依赖的父工程
父工程主要有市面上流行技术很多jar包,
自已项目使用的话,只需引入,不需要写版本号,好处解决了很多jar依赖版本问题
--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><!--    不能版本3.0后,之后需要jdk17版本--><version>2.5.4</version></parent><dependencies><!--是springmvc依赖,也是springboot启动依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--连接数据库--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--mybatis-plus依赖,对应yml文件的配置--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency><!--  druid: 数据库连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version></dependency><!--测试依赖,集成多个测试库,例如: JUnit:Java的单元测试框架.......--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.34</version></dependency></dependencies>

2.添加启动项

/*** 启动类* * @SpringBootApplication:标识该类是启动类*/
@SpringBootApplication
//让Mybatis扫描接口路径 || 或者每个mapper接口加注解@Mapper
@MapperScan("com.woniuxy.mapper")
public class App {//    证明已经启动成功:
//    Tomcat initialized with port(s):8080 (http)
//    Tomcat started on port(s):8080 (http)with context path ''public static void main(String[] args) {SpringApplication.run(App.class);}
}

3.配置文件.yml

类似于jdbc | source.properties配置文件

注意事项:

  • 0: 注释是#
  • 1:使用换行加空格代表层级关系
  • 2:同级和同级之间要对齐
  • 3:值和属性名之间要有空格
#启动时: 端口号被占用,解决办法修改端口号 || 任务管理器结束jdk进程
#server:
#  port: 8081
spring:datasource:#连接数据库driver-class-name: com.mysql.cj.jdbc.Driver #连接驱动#连接数据库的路径url: jdbc:mysql://127.0.0.1:3306/community?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=trueusername: rootpassword: 123456type: com.alibaba.druid.pool.DruidDataSourcedruid:min-idle: 5 #最小连接数max-active: 20 #最大连接数max-wait: 6000 #连接等待超时时间,单位毫秒数
#      对应导入的依赖pom文件mybatis-plus
mybatis-plus:#指定映射文件路径mapper-locations: classpath:mapper/*.xml configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #控制台日志输出sql语句内容#配置查询返回的实体类路径,ResultType可直接赋值对象type-aliases-package: com.woniuxy.entity

三,springboot集成 Spring MVC

1.springmvc定义

一款基于mvc模式、请求驱动、轻量级web框架(封装了Servlet)

  • 给予MVC模式
  • 请求驱动
  • 轻量级web框架
  • 封装了Servlet程序

2.应用注解

  1. @Controller

    申明此类是controller类, 接受客户端请求

  2. @RequestMapping

    设置可以访问该方法一个路径"浏览器请求的路径"__(类似@Webservlet);

  3. ReponseBody

    返回一个浏览器页面json数据,方法返回默认不是一个json, 是一个请求页面的路径

@Controller //接受客户端请求
public class UserController {@RequestMapping("/hello")@ResponseBodypublic String sayHello() {return "hello SpringMvc!!!";}@RequestMapping("/queryById")//客户端请求路径__映射路径@ResponseBody//指定返回的是数据还是页面,不加此注解默认返回页面,在根路径下查找页面,此注解返回json数据//获取请求路径的参数: 路径?id=1//@RequestParam(value = "userId", defaultValue = "10"设置传递参数的默认值为10public String queryById(@RequestParam(value = "userId") int id) {//@RequestParam("userId")对应地址栏传递的参数的属性名6System.out.println(id);return "/hello";}
}

yId(@RequestParam(value = “userId”) int id) {//@RequestParam(“userId”)对应地址栏传递的参数的属性名6
System.out.println(id);
return “/hello”;
}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com