SpringBoot项目开发

本文介绍了SpringBoot的基本概念,如何创建SpringBoot项目,并详细阐述了项目的特点,包括启动类、根包、静态页面的处理、控制器的使用以及数据库编程。通过SpringBoot,开发者可以快速构建无需额外配置的Web应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 基本概念

使用传统的SSM框架开发项目时,需要添加诸多依赖,并且还需要做大量的配置,但是每个项目使用到的依赖大致相同,配置方式也大致不变。

SpringBoot默认集成了绝大部分常用的依赖,并内置完成了绝大部分的配置。

2. 创建SpringBoot项目

首先访问https://start.spring.io/网站,这是创建SpringBoot项目的网站,在页面中输入必要的选项,点击Generate Project按钮即可创建项目,并自动下载。

点击Switch to the full version链接可以展开详细设置,在下方还有诸多其它的依赖,如果需要,可以勾选,则在创建项目时就已经自动添加了这些依赖,如果创建项目时没有勾选,也可以后续在开发过程中自行添加。

下载创建的项目压缩包后,将其解压到Workspace中(推荐),通过Eclipse的Import > Import > Existing Maven Projects功能将项目导入,其间,应该保证当前电脑是可以连接到Maven服务器的,加载完成后,在Eclipse中就可以看到正常的项目了。

当生成完项目后,如果pom.xml报错,多是因为使用的Eclipse版本较旧(是Elicpse中配置的Maven版本较旧),通常推荐使用Oxygen版本或以上版本,近期的Eclipse版本从新到旧是:

    Eclipse 2018-12 (4.10)
    Eclipse 2018-09 (4.9)
    Eclipse Photon (4.8)
    Eclipse Oxygen (4.7)
    Eclipse Neon (4.6)
    Eclipse Mars (4.5)
    Eclipse Luna (4.4)
    Eclipse Kepler (4.3)

3. SpringBoot项目的特点

3.1. 启动类

在项目的根包(创建时指定的包名)下有DemoApplication.java文件(文件名中Application左侧的单词是创建项目时指定的名称),这个文件就是项目的启动文件,后续,启动该项目时,无需添加Tomcat运行环境,也无需将项目添加到Servers中的Tomcat中,只需要通过Java Application的方式运行该文件即可!

SpringBoot项目内置Tomcat,所以当启动了项目且没有停止时,不可以再次启动,导致会因为端口冲突导致后续启动失败!

3.2. 根包

SpringBoot项目会根据创建项目时填写的GroupArtifact形成当前项目的根包,例如cn.tedu.demo,在开发过程中,所有的组件类都必须在根包或其子孙包中,因为SpringBoot项目已经指定了组件扫描的范围就是根包

3.3. 静态页面

在SpringBoot项目中,推荐将静态资源(包含html、css、js、图片等)存入到项目的resources下的static文件夹中。

可以在static下创建index.html(注意:创建html文件时,Eclipse会默认选中webapp文件夹,需要手动选中resources > static文件夹)。

创建完成后,即可通过`http://localhost:8080`来访问这个页面。

所有的SpringBoot项目默认内置Tomcat,且部署项目时,会将项目部署为服务器的根路径项目:`o.a.c.c.C.[Tomcat].[localhost].[/]`,所以,在访问项目时,不必像传统项目一样添加项目名称。

并且,SpringBoot项目将index.html视为默认访问页面,所以当访问该页面,无需显式的指定名称。

3.4. 控制器

控制器类必须在根包或其子孙包中,例如创建cn.tedu.demo.controller.UserController类,并添加@RestController和@RequestMapping("/user")`注解。

@RestController相当于@Controller和每个方法之前都添加@ResponseBody,所以,当类之前添加了@RestController,等同于每个方法之前都已经添加@ResponseBody。当然,如果某个方法不想响应正文,还是希望转发或重定向,则类之前不能使用@RestController。

在SpringBoot项目中,配置资源的路径时,不要求必须是`*.do`,因为SpringBoot配置`DispatcherServlet`的路径时,配置值是`/*`,即由`DispatcherServlet`接收任何请求。

3.5. 数据库编程

使用MySQL数据库,并使用MyBatis框架,需要添加依赖:

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

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

添加以上依赖后,SpringBoot将视为当前项目需要连接数据库,如果在`application.properties`中没有添加连接数据库的相关配置,则运行项目时就会提示错误!则需要在application.properties中配置:

# datasource
spring.datasource.url=jdbc:mysql://localhost:3306/tedu_ums?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

可以编写单元测试:

 @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DemoApplicationTests {    
        @Test
        public void contextLoads() {
        }        
        @Autowired
        DataSource dataSource;
        @Test
        public void testConnection()  throws SQLException {
            Connection conn = dataSource.getConnection();
            System.out.println(conn);
        }  
    }

为了保证SpringBoot能扫描得到MyBatis的接口文件所在的包,需要在启动类`DemoApplication`之前添加`@MapperScan("cn.tedu.demo.mapper")`注解:

 @SpringBootApplication
    @MapperScan("cn.tedu.demo.mapper")
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }   
    }

然后,需要在`application.properties`中指定XML映射文件的位置:

    mybatis.mapper-locations=classpath:mappers/*.xml

关于MyBatis的开发与传统SSM项目一样,先创建接口并定义抽象方法,然后使用XML配置映射,完成后,可以编写并执行单元测试:

  @Autowired
    UserMapper userMapper; 
    @Test
    public void findByUsername() {
        String username = "admin888";
        User user = userMapper.findByUsername(username);
        System.err.println(user);
    }

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

    MVC = Model,View,Controller
    
    View:视图

    Controller:控制器,接收请求,给予响应

    Model:数据模型 = 业务逻辑(Service) + 数据操作(Dao)

    请求 ---> Controller ---> Service ---> Mapper

项目示例基于spring boot 最新版本(2.1.9)实现,Spring Boot、Spring Cloud 学习示例,将持续更新…… 在基于Spring Boot、Spring Cloud 分布微服务开发过程中,根据实际项目环境,需要选择、集成符合项目需求的各种组件和积累各种解决方案。基于这样的背景下,我开源了本示例项目,方便大家快速上手Spring Boot、Spring Cloud 。 每个示例都带有详细的介绍文档、作者在使用过程中踩过的坑、解决方案及参考资料,方便快速上手为你提供学习捷径,少绕弯路,提高开发效率。 有需要写关于spring boot、spring cloud示例,可以给我提issue哦 ## 项目介绍 spring boot demo 是一个Spring Boot、Spring Cloud的项目示例,根据市场主流的后端技术,共集成了30+个demo,未来将持续更新。该项目包含helloworld(快速入门)、web(ssh项目快速搭建)、aop(切面编程)、data-redis(redis缓存)、quartz(集群任务实现)、shiro(权限管理)、oauth2(四种认证模式)、shign(接口参数防篡改重放)、encoder(用户密码设计)、actuator(服务监控)、cloud-config(配置中心)、cloud-gateway(服务网关)、email(邮件发送)、cloud-alibaba(微服务全家桶)等模块 ### 开发环境 - JDK1.8 + - Maven 3.5 + - IntelliJ IDEA ULTIMATE 2019.1 - MySql 5.7 + ### Spring Boot 模块 模块名称|主要内容 ---|--- helloworld|[spring mvc,Spring Boot项目创建,单元测试](https://github.com/smltq/spring-boot-demo/blob/master/helloworld/HELP.md) web|[ssh项目,spring mvc,过滤器,拦截器,监视器,thymeleaf,lombok,jquery,bootstrap,mysql](https://github.com/smltq/spring-boot-demo/blob/master/web/HELP.md) aop|[aop,正则,前置通知,后置通知,环绕通知](https://github.com/smltq/spring-boot-demo/blob/master/aop/HELP.md) data-redis|[lettuce,redis,session redis,YAML配置,连接池,对象存储](https://github.com/smltq/spring-boot-demo/blob/master/data-redis/HELP.md) quartz|[Spring Scheduler,Quartz,分布式调度,集群,mysql持久化等](https://github.com/smltq/spring-boot-demo/blob/master/quartz/HELP.md) shiro|[授权、认证、加解密、统一异常处理](https://github.com/smltq/spring-boot-demo/blob/master/shiro/HELP.md) sign|[防篡改、防重放、文档自动生成](https://github.com/smltq/spring-boot-demo/blob/master/sign/HELP.md) security|[授权、认证、加解密、mybatis plus使用](https://github.com/smltq/spring-boot-demo/blob/master/security/HELP.md) mybatis-plus-generator|[基于mybatisplus代码自动生成](https://github.com/smltq/spring-boot-demo/blob/master/mybatis-plus-generator) mybatis-plus-crud|[基于mybatisplus实现数据库增、册、改、查](https://github.com/smltq/spring-boot-demo/blob/master/mybatis-plus-crud) encoder|[主流加密算法介绍、用户加密算法推荐](https://github.com/smltq/spring-boot-demo/blob/master/encoder/HELP.md) actuator|[autuator介绍](https://github.com/smltq/spring-boot-demo/blob/master/actuator/README.md) admin|[可视化服务监控、使用](https://github.com/smltq/spring-boot-demo/blob/master/admin/README.md) security-oauth2-credentials|[oauth2实现密码模式、客户端模式](https://github.com/smltq/spring-boot-demo/blob/master/security-oauth2-credentials/README.md) security-oauth2-auth-code|[基于spring boot实现oauth2授权模式](https://github.com/smltq/spring-boot-demo/blob/master/security-oauth2-auth-code/README.md) mybatis-multi-datasource|[mybatis、数据库集群、读写分离、读库负载均衡](https://github.com/smltq/spring-boot-demo/blob/master/mybatis-multi-datasource) template-thymeleaf|[thymeleaf实现应用国际化示例](https://github.com/smltq/spring-boot-demo/blob/master/template-thymeleaf) mq-redis|[redis之mq实现,发布订阅模式](https://github.com/smltq/spring-boot-demo/blob/master/mq-redis) email|[email实现邮件发送](https://github.com/smltq/spring-boot-demo/blob/master/email) jGit|[java调用git命令、jgit使用等](https://github.com/smltq/spring-boot-demo/blob/master/jGit) webmagic|[webmagic实现某电影网站爬虫示例](https://github.com/smltq/spring-boot-demo/blob/master/webmagic) netty|[基于BIO、NIO等tcp服务器搭建介绍](https://github.com/smltq/spring-boot-demo/blob/master/netty) ### Spring Cloud 模块 模块名称|主要内容 ---|--- cloud-oauth2-auth-code|[基于spring cloud实现oath2授权模式](https://github.com/smltq/spring-boot-demo/blob/master/cloud-oauth2-auth-code) cloud-gateway|[API主流网关、gateway快速上手](https://github.com/smltq/spring-boot-demo/blob/master/cloud-gateway) cloud-config|[配置中心(服务端、客户端)示例](https://github.com/smltq/spring-boot-demo/blob/master/cloud-config) cloud-feign|[Eureka服务注册中心、负载均衡、声明式服务调用](https://github.com/smltq/spring-boot-demo/blob/master/cloud-feign) cloud-hystrix|[Hystrix服务容错、异常处理、注册中心示例](https://github.com/smltq/spring-boot-demo/blob/master/cloud-hystrix) cloud-zuul|[zuul服务网关、过滤器、路由转发、服务降级、负载均衡](https://github.com/smltq/spring-boot-demo/blob/master/cloud-zuul) cloud-alibaba|[nacos服务中心、配置中心、限流等使用(系列示例整理中...)](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba) #### Spring Cloud Alibaba 模块 模块名称|主要内容 ---|--- nacos|[Spring Cloud Alibaba(一)如何使用nacos服务注册和发现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README1.md) config|[Spring Cloud Alibaba(二)配置中心多项目、多配置文件、分目录实现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README2.md) Sentinel|[Spring Cloud Alibaba(三)Sentinel之熔断降级](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README3.md) Dubbo|[Spring Cloud Alibaba(四)Spring Cloud与Dubbo的融合](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README4.md) RocketMQ|[Spring Cloud Alibaba(五)RocketMQ 异步通信实现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README5.md) ### 其它 模块名称|主要内容 ---|--- leetcode|[力扣题解目录](https://github.com/smltq/spring-boot-demo/blob/master/leetcode) ## Spring Boot 概述 Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的、产品级别的Spring应用。 Spring Boot为Spring平台及第三方库提供开箱即用的设置,这样你就可以有条不紊地开始。多数Spring Boot应用只需要很少的Spring配置。 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Sprin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值