SpringBoot之整合SSM步骤

传统的JavaWeb开发中,Spring、SpringMVC和MyBatis(SSM)的整合需要繁琐的配置,涉及大量XML文件,而SpringBoot的出现,通过自动配置和起步依赖,极大简化了SSM的整合过程。

一、环境准备

在开始整合前,需要确保开发环境满足以下要求:

  • JDK版本:1.8及以上
  • 构建工具:Maven 3.6+(或Gradle,本文以Maven为例)
  • 开发工具:IntelliJ IDEA(或Eclipse)
  • 数据库:MySQL 8.0(本文以MySQL为例,其他数据库配置类似)

二、创建SpringBoot项目

  1. 通过Spring Initializr创建项目
    打开IDEA,选择“File -> New -> Project”,在弹出的窗口中选择“Spring Initializr”,设置项目基本信息(Group、Artifact、Package等),Type选择“Maven”,Java版本选择1.8,然后点击“Next”。

  2. 选择起步依赖
    在依赖选择界面,勾选以下必要依赖:

    • Spring Web(对应SpringMVC):提供Web开发支持,包含SpringMVC核心组件
    • MyBatis Framework:MyBatis框架支持
    • MySQL Driver:MySQL数据库驱动
    • Spring Boot DevTools(可选):热部署工具,方便开发调试
  3. 完成项目创建
    选择项目存储路径,点击“Finish”,IDEA会自动下载依赖并生成项目结构。

三、配置数据库连接

  1. 修改application.properties配置文件
    src/main/resources目录下,打开application.properties,添加数据库连接信息:
    # 数据库连接配置
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/ssm_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    spring.datasource.username=root
    spring.datasource.password=123456
    
    # MyBatis配置
    mybatis.mapper-locations=classpath:mapper/*.xml  # 指定Mapper映射文件路径
    mybatis.type-aliases-package=com.example.ssm.entity  # 指定实体类包路径,简化XML中的类名引用
    
    注意:需提前在MySQL中创建名为ssm_db的数据库。

四、整合Spring(Service层)

SpringBoot默认已经集成了Spring核心功能,无需额外配置,只需按照Spring的注解规范开发Service层即可。

  1. 创建实体类
    com.example.ssm.entity包下创建User实体类:

    public class User {
        private Integer id;
        private String username;
        private String password;
        // 省略getter、setter和toString方法
    }
    
  2. 创建Service接口及实现类
    com.example.ssm.service包下创建UserService接口:

    public interface UserService {
        User getUserById(Integer id);
    }
    

    com.example.ssm.service.impl包下创建实现类,并使用@Service注解标识为Spring服务:

    @Service
    public class UserServiceImpl implements UserService {
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public User getUserById(Integer id) {
            return userMapper.selectById(id);
        }
    }
    

五、整合MyBatis(Dao层)

  1. 创建Mapper接口
    com.example.ssm.mapper包下创建UserMapper接口,使用@Mapper注解标识为MyBatis映射接口:

    @Mapper
    public interface UserMapper {
        User selectById(Integer id);
    }
    
  2. 创建Mapper映射文件
    src/main/resources/mapper目录下创建UserMapper.xml,编写SQL语句:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.ssm.mapper.UserMapper">
        <select id="selectById" parameterType="int" resultType="User">
            select id, username, password from user where id = #{id}
        </select>
    </mapper>
    

六、整合SpringMVC(Controller层)

SpringBoot的spring-boot-starter-web依赖已自动配置好SpringMVC,包括DispatcherServlet、视图解析器等,只需创建Controller即可。

  1. 创建Controller类
    com.example.ssm.controller包下创建UserController,使用@RestController注解标识为REST风格控制器:
    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        private UserService userService;
    
        @GetMapping("/{id}")
        public User getUser(@PathVariable Integer id) {
            return userService.getUserById(id);
        }
    }
    

七、编写启动类

SpringBoot项目通过启动类启动,默认生成的SsmApplication类已包含@SpringBootApplication注解,该注解整合了@Configuration@EnableAutoConfiguration@ComponentScan,会自动扫描并加载Bean:

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

八、测试整合结果

  1. 创建测试数据
    在MySQL的ssm_db数据库中创建user表并插入测试数据:

    CREATE TABLE user (
        id INT PRIMARY KEY AUTO_INCREMENT,
        username VARCHAR(50) NOT NULL,
        password VARCHAR(50) NOT NULL
    );
    INSERT INTO user (username, password) VALUES ('test', '123456');
    
  2. 启动项目并测试
    运行启动类的main方法,项目启动成功后,在浏览器或Postman中访问http://localhost:8080/user/1,若返回以下JSON数据,则说明SSM整合成功:

    {"id":1,"username":"test","password":"123456"}
    

总结

SpringBoot整合SSM的核心优势在于“自动配置”和“起步依赖”,相比传统XML配置,省去了大量繁琐的配置工作:

  • 通过spring-boot-starter-web自动整合SpringMVC
  • 通过mybatis-spring-boot-starter自动整合MyBatis和Spring
  • 数据库连接和MyBatis映射只需简单的属性配置

我们只需关注业务代码的编写,极大提高开发效率,如果需要扩展功能(如事务管理、分页等),可在此基础上添加相应配置或依赖即可。

若这篇内容帮到你,动动手指支持下!关注不迷路,干货持续输出!
ヾ(´∀ ˋ)ノヾ(´∀ ˋ)ノヾ(´∀ ˋ)ノヾ(´∀ ˋ)ノヾ(´∀ ˋ)ノ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值