首页 > 其他分享 >springboot整合ssm详细讲解

springboot整合ssm详细讲解

时间:2022-10-09 12:34:09浏览次数:40  
标签:springboot 讲解 boot springframework ssm org test import


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.工程结构

springboot整合ssm详细讲解_html

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

springboot整合ssm详细讲解_spring_02

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 作为页面模板引擎,后
续的学习中
 

标签:springboot,讲解,boot,springframework,ssm,org,test,import
From: https://blog.51cto.com/u_11334685/5740099

相关文章

  • Springboot整合缓存
    一、缓存的引入一个应用主要瓶颈在于数据库的IO,大家都知道内存的速度是远远快于硬盘的速度(即使固态硬盘与内容也无法比拟)。应用之中经常会遇到返回相同的数据(数据字典,行政......
  • Springboot创建项目(idea版本)
    一:概述由于springboot项目,不管是java工程还是web工程都可以直接以jar方式运行,所以推荐创建jar工程,这里创建jar工程项目为例。二:两种方式创建springboot项目1.第一种方式手动......
  • Springboot自定义Stater
    1、默认启动器Boot会将项目中常用的场景做成对应的starter启动器,项目中涉及到什么场景就引入该场景对应的启动器,项目中引入这些启动器之后,和这个starter相关的依赖也会被引......
  • Springboot自动配置原理
    一个boot项目启动类有个@SpringBootApplication注解,查看此注解主要包括@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三个注解@SpringBootConfigura......
  • 把你的springboot启动图(banner)设置成圣嘉然!
    在resource文件夹下面新建一个banner.txt的文件!然后插入如下的文字!点击查看代码...,]]]/@@@@@O]]]]]`........,]]]..............
  • Springboot传参时通过注解转换RequestBody中的枚举类型
    请求对象为@RequestBodyMyRequestrequest时,MyReqeust中包含了枚举类型。如果不加处理,前端只能传递枚举名或者枚举数组下标。经过改造,可以传递自已定义的枚举值。参考......
  • Spring、springboot、springMVC、JPA、SpringData、springCloud的概述
    https://blog.csdn.net/qq_51308214/article/details/125165747Spring框架是Java平台上的一种开源应用框架, Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了......
  • SpringBoot框架中的DAO层、Entity层、Service层、Controller层
    https://blog.csdn.net/qq_40963664/article/details/123900763一般的项目模块中都有DAO、Entity、Service、Controller层。Entity层:实体层数据库在项目中的类DAO层:......
  • SSM的科研成果转化管理系统
    ​很多高校目的就是为了解决上述出现的问题,使整个学院的科研信息管理更加有效,可以从各个环节节省时力,让老师把更多的精力投身于科研研究中。同时科研能更有效的转化出成果......
  • SpringBoot整合fastdfs
    1、背景在前一节中,我们搭建了一个单机版的fastdfs服务,此处我们将fastdfs与springboot进行整合,实现文件的上传和下载。2、整合步骤2.1、引入依赖<dependency><group......