spring-boot第一次搭建使用

本文介绍如何使用Spring Boot快速搭建基于MyBatis的微型服务应用,包括配置数据库连接、编写Mapper接口、实现Service层及Controller层,并介绍了预加载数据的方法。

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

个人理解:spring-boot作为spring的一个分支,主要用来提供面向微型服务的轻量框架。spring-boot把spring的一些通用的,默认的,繁琐的配置默认为已经配置,不用自己去进行重复的配置,但是可以增加,或者修改。

1、框架中有一个默认的配置文application.properties用来指定数据库等配置。

mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.qyer.entity

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

写入数据库的基本的配置,这里用了Hikari的数据库连接池,同时指定mybatis的一些配置,包括xml的位置和映射的model的位置。


2、关于mybatis
当时学习搭建的时候,网上有很多的方式,看着有些糊涂,后来看了一篇博客,总结起来就有两种配置方式。
一种就是我应用的方式,指定xml的方式编写sql语句,然后通过dao层引用。

package com.qyer.mapper;

import com.qyer.entity.UserEntity;

import java.util.List;

public interface UserMapper {

  List<UserEntity> getAll();

  UserEntity getOne(Long id);

  void insert(UserEntity user);

  void update(UserEntity user);

  void delete(Long id);

}

这里面的mapper就是DAO层,这里写的比较简单,本应该实现一个更加基本的mapper接口,这里先不考虑。这里与spring-mvc不同的是这里没有任何托管的标签来告诉spring这里是托管的数据库接口层,而是在启动入口处指明。
另一种方式就是直接在数据库接口层用@select等标签直接编写sql语句,我没有采用这种方式。


3、service层与spring-mvc没有什么区别

@Service
public class UserService {

  @SuppressWarnings("SpringJavaAutowiringInspection")
  @Autowired
  private UserMapper userMapper;

  public List<UserEntity> getList(){
    List<UserEntity> users=userMapper.getAll();
    return users;
  }

有一点是在注入数据库接口层的时候,userMapper处会标红,但是没有任何问题,也不会对运行造成影响,由于没有搞清楚是什么原因,就用了@SuppressWarnings(“SpringJavaAutowiringInspection”)这个标签,用来告诉系统这里的异常不用理会,他的作用域只是注入的片段。


4、controller层相对于mvc来说也没有多大差别

@RestController
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/getUsers")
  public List<UserEntity> getUsers() {
    return userService.getList();
  }

这里的@RestController是@Controller与@@RequestBody的合体,代表这个controller的所有请求返回json形式的数据。由于目前前后端高度分离,没有引用jsp的返回形式,引入也比较简单。


5、程序入口

@SpringBootApplication
@MapperScan("com.qyer.mapper")
public class SpringBootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

spring-boot的启动方式很便捷,由于它自己内嵌了端口为8080的tomcat,只要我们启动这个main函数,一切就交给它去执行,@MapperScan(“com.qyer.mapper”)就是要指定的数据库接口层的位置。这样一个简单的spring-boot项目就搭建完成。


6、由于公司业务需要,简单的了解了一下预先加载数据的功能。

@Component
@Order(value = 1)
public class Loading implements CommandLineRunner {
  @Override
  public void run(String... args){
    System.out.println("2222");
  }

}

在一个类里面加入@Component标签,然后实现CommandLineRunner接口,实现它的run方法,就可以实现在程序启动前加载一些依赖的数据。
@Order(value = 1)是指明数据加载顺序的标签,数字越小,优先级越大。


7、pom依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <!-- 版本号可以不用指定,Spring Boot会选用合适的版本 -->
        </dependency>
    </dependencies>

项目的结构图
这里写图片描述

### Spring Boot Starter Parent 的版本依赖关系 Spring Boot 使用 `spring-boot-starter-parent` 来管理项目的依赖和插件配置。通过这种方式,开发者可以在不指定版本的情况下引入常见的库,因为父 POM 文件已经预设了这些库的最佳实践版本。 当使用 `spring-boot-starter-parent` 作为父级时,所有的子模块都可以继承其定义好的属性、依赖管理和插件管理设置。这不仅简化了构建文件的编写工作量,也确保了一致性和兼容性[^2]。 对于具体的依赖信息,在 `dependencyManagement` 部分会声明一系列常用的第三方库及其推荐使用的版本号。这意味着如果一个项目直接或间接地指定了某个被管理过的依赖,则无需再次提供该依赖的具体版本号;而那些未在此处列出的外部组件则仍需手动设定完整的坐(groupId, artifactId 和 version)。这种机制允许团队成员快速搭建新应用的同时保持技术栈的一致更新节奏[^3]。 为了查看特定于某次发布的 `spring-boot-starter-parent` 所携带的确切依赖列表及对应版本: 1. 访问 Maven Central Repository 或其他官方仓库; 2. 查找相应版本的 `pom.xml` 文档; 3. 浏览其中 `<dependencyManagement>` 下的内容即可获得所需详情。 例如,在最新稳定版中可能看到如下片段: ```xml <properties> <!-- 各种工具类库的基础版本 --> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${revision}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 更多依赖条目... --> </dependencies> </dependencyManagement> ``` 上述代码展示了如何导入另一个聚合型POM(`spring-boot-dependencies`)来进一步扩展可利用资源池,并且实际有效控制着整个生态系统的协调运作[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值