spring boot 整合ssm 使用SpringBoot整合ssm项目的实例详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring boot 整合ssm 使用SpringBoot整合ssm项目的实例详解

彳亍风   2021-03-30 我要评论

SpringBoot是什么?

Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。

Spring Boot 现在已经成为 Java 开发领域的一颗璀璨明珠,它本身是包容万象的,可以跟各种技术集成。成为 SpringBoot 全家桶,成为一把万能钥匙。

SpringBoot的特点

1.创建独立的 Spring 应用程序

2.嵌入的 Tomcat ,无需部署 WAR 文件

3.简化 Maven 配置

4.自动配置 Spring

5.提供生产就绪型功能,如指标,健康检查和外部配置

Spring 官方支持 SpringBoot 提供的项目框架生成页面

https://start.spring.io/

在eclipse上创建springboot工程

( jdk 版本必须 1.8 以上, springboot 基本上废除了 1.6 、 1.7)

eclipse版本也有要求,版本过低,创建的工程会报错或者可以使用springboot低版本。也可以使用STS或IDEA,版本支持较好,下面演示用的是eclipse

简单的使用springboot整合ssm

1. 创建 Maven 工程,创建 simple project ,类型为 jar

pom.xml

额外需要的 jar ,还得自己依赖,例如: mysql 驱动包,阿里的数据源 druid 包

<parent>  

<groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.4.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
 </parent>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>
 <dependencies>
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.0</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid-spring-boot-starter</artifactId>
   <version>1.1.0</version>
  </dependency>
 </dependencies>

创建 pojo 对象

public class User implements Serializable{
 private static final long serialVersionUID = 1L;
 private Integer id;
 private String name;
 @DateTimeFormat(pattern="yyyy-MM-dd")
 private Date birthday;
 private String address;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 @Override
 public String toString() {
  return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", address=" + address + "]";
 }
}

创建 UserMapper.xml

<?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">
<!-- namespace命名空间,唯一特性 -->
<mapper namespace="com.lmq.mapper.UserMapper">
 <select id="find" resultType="User">
  select id,name,birthday,address from user
 </select>
</mapper>

创建UserMapper 接口

public interface UserMapper {
 //调用xml方式
 public List<User> find();

 //调用注解方式
 @Select("select * from user where id=#{id}")
 public User get(@Param("id") Integer id);
}

创建UserService接口

public interface UserService {
 public List<User> find();
 public User get(Integer id);
}

创建UserServiceImpl接口实现类

@Service
public class UserServiceImpl implements UserService{
 @Autowired
 private UserMapper userMapper;
 public List<User> find() {
  return userMapper.find();
 } 
 public User get(Integer id){
  return userMapper.get(id);
 }
}

创建UserController类

使用 @RestController 替代 @Controller 和 @ResponseBody (返回 json 串)

@RestController
@RequestMapping(value = "/user")
public class UserController {
 @Autowired
 private UserService userService;
 @RequestMapping("/find")
 public List<User> find() {
  return userService.find();
 } 
 @RequestMapping("/get/{id}")
 public User get(@PathVariable Integer id){
  return userService.get(id);
 }
}

创建application.yml

全局配置文件, yml 为新的配置文件方式,注意其中格式为空格,不能有 tab 。

配置端口,配置数据源,配置 mybatis 全局配置。

注意:如果端口,启动时日志显示 8080 ,说明此文件未加载。检查原因一般是文件名或者路径不正确。

server:
 port: 8080
spring:
 datasource:
  type: com.alibaba.druid.pool.DruidDataSource
  driver-class-name: com.mysql.jdbc.Driver
  url: jdbc:mysql://127.0.0.1:3306/mybatisdb
  username: root
  password: root
mybatis:
 typeAliasesPackage: com.lmq.pojo
 mapperLocations: classpath:mappers/*.xml
logging:
 level: 
 com.lmq.mapper: debug

创建RunApplication.java

@SpringBootApplication
@MapperScan("cn.lmq.mapper")  //扫描Mybatis接口文件
public class RunApplication {
 public static void main(String[] args) {
  SpringApplication.run(RunApplication.class, args);
 }
}

初步整合完毕,比三大框架ssm好用太多了

传统构建 Maven 项目, pom 中的依赖包繁多,升级一个 jar 版本时,会引发新的冲突,调试许久。而 SpringBoot 接管了 jar 的版本,它构建好一套,这套中各 jar 的版本已经测试好,开发者再无需去关注每个依赖包的版本及冲突问题,从而简化开发。

再者,它启动也非常快,直接运行一个类,使用 tomcat 的 maven 插件。开发调试时效率提高。

热部署支持

配置pom.xml

<!-- 热部署支持 -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
  </dependency>

总结

以上所述是小编给大家介绍的使用SpringBoot整合ssm项目的实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们