springboot整合ssm详细讲解

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: springboot整合ssm详细讲解

SSM是企业中广泛应用的框架。大家再熟练地使用SSM进行业务逻辑开发的时候,也被它大量的xml配置困扰。今

天快速优雅的使用SpringBoot实现简易的SSM工程。废话不多说,come on

开发工具idea

1.创建一个web工程,pom.xml中加入如下配置:

 

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
 
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--使用jsp页面-->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

2.工程结构

3.数据库和实体类

数据库使用mysql,创建部门表dept,表结构很简单,id主键和name部门名称。

4.dao层

dao的代码我们演示两种方式,一种使用传统的Mapper映射文件,一种使用注解编写sql:


package com.test.springboot.ssm.dao;
import com.test.springboot.ssm.pojo.Dept;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import java.util.List;
public interface DeptDAO {
//查询列表,演示使用传统的mapper映射文件
List<Dept> getDeltList();
//插入,演示使用注解编写sql,省略xml配置
@Insert("insert into DEPT(NAME) values(#{name})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "ID")
void addDept(String name);
}
mapper映射文件如下
<?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.test.springboot.ssm.dao.DeptDAO">
<resultMap id="deptMap" type="Dept">
<id property="id" column="ID"/>
<result property="name" column="NAME"/>
</resultMap>
<select id="getDeltList" resultMap="deptMap">
select ID,NAME from DEPT
</select>
</mapper>

注意mapper的namespace是DAO接口的全路径,mapper中语句的id与DAO接口中的方法名是一致的

5.service层

接口:

package com.test.springboot.ssm.service;
import com.test.springboot.ssm.pojo.Dept;
import java.util.List;
public interface DeptService {
List<Dept> getDeltList();
void addDept(String name);
}
实现类;
package com.test.springboot.ssm.service.impl;
import com.test.springboot.ssm.dao.DeptDAO;
import com.test.springboot.ssm.pojo.Dept;
import com.test.springboot.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptDAO deptDAO;
@Override
public List<Dept> getDeltList() {
return deptDAO.getDeltList();
}
@Override
public void addDept(String name) {
deptDAO.addDept(name);
}
}

6.controller层

package com.test.springboot.ssm.controller;
import com.test.springboot.ssm.pojo.Dept;
import com.test.springboot.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping("list.html")
public ModelAndView list() {
List<Dept> deptList = deptService.getDeltList();
return new ModelAndView("list", "deptList", deptList);
}
@RequestMapping("add.html")
public String add(String name) {
deptService.addDept(name);
//添加成功后重定向到列表页
return "redirect:list.html";
}
}
jsp页面
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form action="/add.html" method="post">
部门名:<input type="text" name="name"/><br/>
<input type="submit" value="add"/>
</form>
</body>
</html>
list.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<c:forEach items="${deptList}" var="dept">
${dept.id}-${dept.name}<br/>
</c:forEach>
</body>
</html>

貌似到目前为止,编码的形式和我们开发普通的SSM没什么区别,不要着急,重点来了。传统的ssm中我们需需要
在spring.xml中配置数据源、声明式事务、mybatis配置信息、注解扫描等。使用SpringBoot,我们可以省略掉所
有的xml。没错,就是一个配置文件都没有,废话少说,上代码:
SpringBoot有个全局的属性配置文件application.properties


#数据源的基本信息

spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password =
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#mybatis中mapper文件的路径
mybatis.mapper-locations=classpath*:com/test/springboot/ssm/dao/mappers/*.xml
#起别名。可省略写mybatis的xml中的resultType的全路径
mybatis.type-aliases-package=com.test.springboot.ssm.pojo
#springMVC中的视图信息,响应前缀
spring.mvc.view.prefix=/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
#DispatcherServlet中响应的url-pattern
server.sevlet-path=*.html

7.入口类

package com.test.springboot.ssm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement//开启事务管理
@ComponentScan("com.test.springboot.ssm")//扫描注解元素
@MapperScan("com.test.springboot.ssm.dao")//Mybatis的DAO所在包
public class SpringbootSsmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSsmApplication.class, args);
}
}

对比发现,SpringBoot可以省略大量的xml配置文件,几个注解轻松代替繁琐的配置。为了方便大家接受

SpringBoot,本文中的示例使用的页面模板引擎还是jsp。Spring官网推荐使用Thymeleaf 作为页面模板引擎,后

续的学习中

 


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
前端开发 JavaScript Java
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
|
4天前
|
Java 数据库
【问题记录(已解决)】springboot整合ssm报错‘url‘ attribute is not specified and no embedded datasource
【问题记录(已解决)】springboot整合ssm报错‘url‘ attribute is not specified and no embedded datasource
|
8月前
|
Java 数据库连接 Maven
使用maven快速搭建SpringBoot的SSM项目(下)
使用maven快速搭建SpringBoot的SSM项目(下)
62 0
|
4天前
|
Java 关系型数据库 MySQL
springboot基于ssm框架实现的家具商城管理系统
springboot基于ssm框架实现的家具商城管理系统
|
4天前
|
供应链 前端开发 Java
基于SSM框架springBoot实现的企业级进销存ERP系统【源码+数据库+毕设】
基于SSM框架springBoot实现的企业级进销存ERP系统【源码+数据库+毕设】
|
8月前
|
前端开发 Java Maven
使用maven快速搭建SpringBoot的SSM项目(上)
使用maven快速搭建SpringBoot的SSM项目
75 0
|
4天前
|
存储 人工智能 Java
ssm637教材管理系统
ssm637教材管理系统
|
4天前
|
存储 安全 前端开发
ssm172旅行社管理系统的设计与实现
ssm172旅行社管理系统的设计与实现
|
4天前
|
前端开发 JavaScript Java
基于SSM家政预约管理系统的设计与实现
基于SSM家政预约管理系统的设计与实现
15 2
|
4天前
|
JavaScript Java 项目管理
基于SSM大创项目申报管理系统的设计与实现
基于SSM大创项目申报管理系统的设计与实现
19 2