SpringBoot+Mybatis+RocketMQ+SpringCloud(一,SpringBoot整合)

简单说下自己目前的理解:springBoot是对spring的升级,为什么升级?因为各种语言发展到现在,已经非常方便了。但是java,甚至是spring都需要大量的jar包,编译,然后放入tomcat/apache中去执行,上线也需要很多。但是如果使用springBoot,就可以使用它自动生成的相当于main函数的,直接运行。上线,也变得简单。(待写)
特点:

  1. 创建独立的Spring应用程序
  2. 嵌入的Tomcat,无需部署WAR文件
  3. 简化Maven配置
  4. 自动配置Spring
  5. 提供生产就绪型功能,如指标,健康检查和外部配置
  6. 绝对没有代码生成并且对XML也没有配置要求

SpringBoot其实就相当于一个库:简单理解就是springboot并不是什么新型的框架,而是整合了spring,springmvc等框架,默认了很多配置,从而减少了开发者的开发时间。需要什么就去maven.

下面,请开始你的表演。
基础结构说明:src/main/java.是下的程序入口,spring-boot会自动加载启动类所在包下及其子包下的所有组件.。
src/main/resources:配置文件
src/test/java:测试入口

在这里插入图片描述
前面写了一半,隔了一段时间,中间换了电脑,那么重新来过。
1,构建SpringBoot项目
首先确保maven的引入:正确引入后才能构建maven项目。
在这里插入图片描述
1,构建项目,去到https://start.spring.io/;推荐maven + jar的形式,下面的有依赖添加,比如Mybatise,数据库等,你需要的直接勾上即可。
在这里插入图片描述
在这里插入图片描述
下载后,将该项目导入eclipse或者IDEA中。导入:直接导入已有maven项目即可,没什么可说的。
import - > 搜索maven --> 选择existed maven project --> 选择即可。如下:
在这里插入图片描述
关于配置信息的对应关系前面已经写过,不在累述。
尝试运行,运行结果:(运行方式:不要选择run on server,选择Java application选择你的主类运行)
这里普及下运行相关的知识:
maven build是重新对该maven项目进行打包
maven clean : 清除target目录下的之前打好的jar包或者是war包
maven install :是对上面两个命令的集合,既是通过maven自带的原生命令,同时执行了clean 和build,比较节省时间
打包推荐操作:maven clean + maven install 生成最新的jar包或其他包(这样是最保险的)
在这里插入图片描述

必须先说明两点:①必须加web依赖,不是web项目,启动不了;②由于加入依赖Mybatis,需要加上注解保证能扫描到(当然也可以application.properties中添加数据库信息)。另外也可能是其他的原因:properties文件没有被扫描到,需要在pom文件中添加,来保证文件都能正常被扫描到并且加载成功.
这里说明下注解:
@SpringBootApplication // Springboot的核心 全局注解,
可以看到这是一个注解接口包含自动扫描和开启自动配置

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
	@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
	@Filter(type = FilterType.CUSTOM,
			classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

/**
 * Exclude specific auto-configuration classes such that they will never be applied.
 * @return the classes to exclude
 */
@AliasFor(annotation = EnableAutoConfiguration.class)  //开启自动配置
Class<?>[] exclude() default {};

/**
 * Exclude specific auto-configuration class names such that they will never be
 * applied.
 * @return the class names to exclude
 * @since 1.3.0
 */
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};

/**
 * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
 * for a type-safe alternative to String-based package names.
 * @return base packages to scan
 * @since 1.3.0
 */
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")  //自动扫描
String[] scanBasePackages() default {};

/**
 * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
 * scan for annotated components. The package of each class specified will be scanned.
 * <p>
 * Consider creating a special no-op marker class or interface in each package that
 * serves no purpose other than being referenced by this attribute.
 * @return base packages to scan
 * @since 1.3.0
 */
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};

}

在这里插入图片描述
依赖查看:Dependencies可查看有那些依赖;Dependency hierarchy可查看每一个依赖包含的内容。从下面可以看到已经导入很多的包。
在这里插入图片描述
更改端口号:(及运行结果)
在这里插入图片描述
测试controller

package com.example.demo;

//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// 复合注解 @RestController注解:其实就是@Controller和@ResponseBody注解加在一起
// 复合注解
@RestController
//@EnableAutoConfiguration
@RequestMapping(value = "/demo")
public class HelloController {

@RequestMapping("/test")
String index() {
    return "nihao!zhouyi";
}
}

启动项目:结果如下:(纠结了一天终于解决了)
在这里插入图片描述
但是这其中有很多坑:
①报错端口无端被占用,只能是在配置文件中改端口或者cmd命令查看端口并在任务管理器中关闭对应的PID,(重启端口被占用的根本原因查看日志可以发现一次启动却启动了两次tomcat,这是因为项目部署的tomcat没有移除的原因)---------这里告诉我一个道理,分析日志可以解决很多问题。

②报错This application has no explicit mapping for /error, so you are seeing this as a fallback.
查了很多网上的方法:(但是都没有解决我的问题)
出现这个异常说明了跳转页面的url无对应的值.
原因1:
Application启动类的位置不对.要将Application类放在最外侧,即包含所有子包
原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件.
原因2:
在springboot的配置文件:application.yml或application.properties中关于视图解析器的配置问题:
当pom文件下的spring-boot-starter-paren版本高时使用:
spring.mvc.view.prefix/spring.mvc.view.suffix
当pom文件下的spring-boot-starter-paren版本低时使用:
spring.view.prefix/spring.view.suffix
原因3:
控制器的URL路径书写问题
@RequestMapping(“xxxxxxxxxxxxxx”)
实际访问的路径与”xxx”不符合.
原因4:
我在这里补充我自己问题:是因为我原先启动的项目是由tomcat去运行的。然而SpringBoot的项目是不需要这个,因为SpringBoot本身嵌入了Tomcat,前面已经说过(?????)。上面的两个问题都是由这个原因引起的,解决方式:请删除本地的Tomcat部署。

测试测试类Tests

// 以classes = SpringBootDemoApplication.class为基础做单元测试,在里面写测试内容即可。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootDemoApplication.class)
public class SpringBootDemoApplicationTests {

@Test
public void contextLoads() {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值