springboot 使用

  spring 功能非常强大,已经是java web开发标配,但是配置较为复杂繁琐。直到spring boot的出现,彻底改变这种现状。基于spring boot可以用极简的配置,极少的代码,快速开发web应用。

 

一. 创建springboot工程

File --> New

注:Idea comunity 没有 spring Initializr 组件,需使用Idea Ultimate 版本。

编辑包名版本号等信息:

Next,选择web模板:

下一步点击完成后,IDEA 需要一段时间从maven仓库下载依赖库。

生成工程目录如下:

 

二. 创建controller

创建HelloController控制器,添加@RestController  @RequestMapping 注解,代码十分简洁。

package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }
}

启动DemoApplication,请求:localhost:8080/hello 一直显示404错误。

解决方案:出现这个错误一般是Controller没有注册,spring入口类 DemoApplication 默认只加载同目录的类,添加注解@ComponentScan 指定com.example.controller 包名。

重新运行:

 

三. mybatis 集成 

3.1 添加mysql 驱动

pom.xml

<!-- mybatis -->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.1.1</version>
</dependency>

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.21</version>
</dependency>

3.2 配置mysql连接

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?charaterEncoding=UTF-8
    username: root
    password: test
    driver-class-name: com.mysql.jdbc.Driver

  jpa:
    hibernate:
      ddl-auto: update

3.3 添加Tbservices实体类和Tbservices映射类

TbServiceMapper 映射类:

package com.example.mapper;

import com.example.bean.Tbservices;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TbservicesMapper {

    // 优雅的mybatis
    @Select("select * from tb_services")
    List<Tbservices> findAll();
}

Tbservices 控制器:

package com.example.controller;

import com.example.bean.Tbservices;
import com.example.mapper.TbservicesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class HelloController {
    @Autowired
    TbservicesMapper tMapper;

    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String show() {
        StringBuffer sb = new StringBuffer();

        // 查询db
        List<Tbservices> services = tMapper.findAll();

        for (Tbservices srv : services) {
            sb.append("id: ").append(srv.getId()).append("\t");
            sb.append("key: ").append(srv.getKey()).append("\t");
            sb.append("name: ").append(srv.getName()).append("\t");
            sb.append("pid: ").append("<br/>");
        }

        return sb.toString();
    }
}

运行springboot服务,控制台报错:

Field tMapper in com.example.controller.HelloController required a bean of type 'com.example.mapper.TbservicesMapper' that could not be found.

解决方案:spring找不到Mapper,需要添加@MapperScan 注解指定映射器目录:

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"com.example.controller", "com.example.bean"})
@MapperScan("com.example.mapper")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

重新启动springboot,显示正常:

 

参考链接:

https://www.cnblogs.com/wmyskxz/p/9010832.html

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文档内容 一、 Spring介绍 1 1.1、SpringBoot简介 1 1.2、系统要求: 1 1.3、SpringBoot和SpringMVC区别 1 1.4、SpringBoot和SpringCloud区别 2 1.5常见错误 2 二、快速入门 2 2.1、创建一个Maven工程 2 2.2、pom文件引入依赖 3 2.3、编写HelloWorld服务 3 2.4、@RestController 4 2.5、@EnableAutoConfiguration 4 2.6 SpringApplication.run(HelloController.class, args); 4 2.7、SpringBoot启动方式1 4 2.8、SpringBoot启动方式2 4 2.9、SpringBoot启动方式3 5 三、 Web开发 5 3.1、静态资源访问 5 3.2、渲染Web页面 5 3.3、使用Freemarker模板引擎渲染web视图 6 3.3.1、pom文件引入: 6 3.3.2、后台代码 6 3.3.3、前台代码 6 3.3.4、Freemarker其他用法 7 3.3.5、Freemarker配置 8 3.4、使用JSP渲染Web视图 8 3.4.1、pom文件引入以下依赖 8 3.4.2、在application.properties创建以下配置 9 3.4.3、后台代码 9 3.5、全局捕获异常 10 四、 数据访问 10 4.1、springboot整合使用JdbcTemplate 10 4.2、springboot整合使用mybatis 12 4.3、springboot整合使用springjpa 18 4.4、springboot整合多数据源 19 五、 事物管理 25 5.1.1SpringBoot整合事物管理 25 5.1.2SpringBoot分布式事物管理 25 六、 日志管理 30 6.1使用log4j记录日志 30 6.2使用AOP统一处理Web请求日志 32 6.3Spring Boot集成lombok让代码更简洁 33 七、 缓存支持 35 7.1注解配置与EhCache使用 35 7.2使用Redis集成缓存 37 八、 热部署 37 8.1 什么是热部署 37 8.2 项目演示案例 37 8.3 热部署原理 37 8.4 Devtools依赖 38 8.5 Devtools原理 38 九、 监控管理 38 Actuator监控应用 38 Maven依赖 38 YML配置 39 Actuator访问路径 40 Admin-UI分布式微服务监控中心 40 Admin-UI-Server 40 Admin-UI-Client 41 十、 性能优化 43 组件自动扫描带来的问题 43 将Servlet容器变成Undertow 44 SpringBoot JVM参数调优 44 十一、 2.0版本新特性 45 以Java 8 为基准 45 内嵌容器包结构调整 45 Servlet-specific 的server properties调整 45 Actuator 默认映射 46 Spring Loaded不再支持 46 支持Quartz Scheduler 46 OAuth 2.0 支持 46 支持Spring WebFlux 46 版本要求 46 十二、 其他内容 47 12.1、使用@Scheduled创建定时任务 47 12.2、使用@Async实现异步调用 47 12.3、自定义参数 49 12.4、多环境配置 50 12.5、修改端口号 50 12.6、SpringBoot yml 使用 50 12.7、SpringBoot整合拦截器 51 12.8、发布打包 52
SpringBoot是一个由Pivotal团队开发的框架,它将常用的Spring、SpringMVC、data-jpa等框架封装在一起,帮助开发者隐藏框架整合的细节,实现敏捷开发。\[2\]在SpringBoot中,可以使用启动器(starter)来简化配置。启动器是一组依赖的集合,它会默认进行一些配置设置,例如使用spring-boot-starter-web启动器可以快速搭建一个Web应用。\[1\] 在SpringBoot中,可以使用@SpringBootApplication注解来标记启动类。@SpringBootApplication是一个组合注解,包含了@Configuration、@EnableAutoConfiguration和@ComponentScan等注解。@Configuration表示启动类是一个配置类,@EnableAutoConfiguration实现自动装配,@ComponentScan用于扫描注解。\[3\] SpringBoot的配置文件格式可以是properties文件或者yaml文件。在配置文件中,可以设置各种属性和参数,用于配置应用程序的行为。\[3\] 以上是关于SpringBoot的简要介绍和使用方法的说明。如果您有具体的问题或者需要更详细的信息,请提供更多的上下文,我将尽力为您解答。 #### 引用[.reference_title] - *1* [SpringBoot的基本使用](https://blog.csdn.net/weixin_52574640/article/details/126462910)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot使用](https://blog.csdn.net/weixin_45427945/article/details/131138698)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值