当前位置: 首页 > news >正文

做生鲜食品最好的网站行唐网站建设

做生鲜食品最好的网站,行唐网站建设,如何网站平台建设好,sem推广是什么意思呢SpringBoot之整合SSM步骤一、环境准备二、创建SpringBoot项目三、配置数据库连接四、整合Spring(Service层)五、整合MyBatis(Dao层)六、整合SpringMVC(Controller层)七、编写启动类八、测试整合结果总结传统…

SpringBoot之整合SSM步骤

    • 一、环境准备
    • 二、创建SpringBoot项目
    • 三、配置数据库连接
    • 四、整合Spring(Service层)
    • 五、整合MyBatis(Dao层)
    • 六、整合SpringMVC(Controller层)
    • 七、编写启动类
    • 八、测试整合结果
    • 总结

传统的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 {@Autowiredprivate UserMapper userMapper;@Overridepublic 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 {@Autowiredprivate 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映射只需简单的属性配置

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

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

http://www.dtcms.com/a/612187.html

相关文章:

  • C语言是什么编译? | 了解C语言编译过程及其重要性
  • 买个网站多少钱北京网站策划联系电话
  • 怎么建免费网站泸州市建设工程管理局网站
  • 国内免费的建网站平台做照片用的视频模板下载网站
  • 做网站与网店运营wordpress小工具放入用户中心
  • 如何让 AI 按照你的预期输出
  • 河南网站建设首选公司微网站怎么做的好名字吗
  • 做衣服外单网站有哪些重庆网络公司做什么生意好
  • 王建设医生网站搜索引擎网站建设公司
  • 南通企业网站排名优化网站制作软件
  • 微网站建设完 不知道怎么推广咋办百度搜索优化软件
  • 品牌营销型网站建设公司软件开发公司排行
  • 绿化信息网站建设观音桥网站建设
  • 上海网站论坛建设在汕头的网络公司有哪些
  • 在网上做国际快递淘宝网站做网站要买什么空间
  • 电子商务网站建设报告分析网站设计杭州
  • 云梦做网站的优势手机网站有免费做的吗
  • 怎样制作网站电话多语言商城源码
  • 潮州企业网站建设扬中网站推广导流
  • 建设个人网站ip俄乌局势最新进展
  • 吉林省电力建设总公司网站知名网站有哪些
  • 单页网站怎么做外链网站开发与程序开发
  • 响应式网站和传统网站异同app开发合同模板最新版
  • 50015_基于微信小程序的红色旅游系统
  • 谁做的12306网站哪些企业网站做的好
  • 做网站专用素材wordpress 自定义分类
  • 什么是微网站产品朋友圈推广词
  • vip解析网站如何做上海缪斯设计公司
  • php做的网站预览wordpress getthememod
  • 天津网站优化排名网络营销案例报告