手把手教你如何搭建SSM框架、图书商城系统案例

手把手教你如何搭建SSM框架、图书商城系统案例

前言

前一段时间将Spring + SpringMVC + Mybatis 的笔记整理了出来【爆肝五万字】传送门 。大家如有需要可以看看,这篇文章介绍如何整合这三个框架,也就是所谓的SSM框架。当然,你也可以直接学SpringBoot框架,但是那个也是在这个基础上进行的封装,了解SSM,更有利于学习后续知识。

该项目源码已发布到GitHub:源码传送门这里只展示如何整合SSM框架,只给出来几个配置文件的整合哦

1、新建一个maven项目

填写项目的基本信息

创建好的Maven项目

2、添加web骨架

提示: 添加webapp的方式有多种,我这里是其中一种方式。另外还有:1、可以在选择创建maven项目的时候,勾选webapp骨架。2、也可以手动创建一个webapp的包,然后手动修改相关配置。

我这里直接添加相关骨架,然后在弹出框选择web

添加骨架后的项目结构如下

3、修改pom文件

pom 中相关依赖,

1.8

8

8

4.11

6.0.6

0.9.5.2

2.5

1.2

3.5.2

2.0.2

5.1.9.RELEASE

5.1.9.RELEASE

1.2.17

junit

junit

${junit.version}

test

mysql

mysql-connector-java

${mysql.connector.java.version}

com.mchange

c3p0

${c3p0.version}

javax.servlet

servlet-api

${servlet.api.version}

javax.servlet

jstl

${jstl.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

${mybatis.spring.version}

org.springframework

spring-webmvc

${spring.webmvc.version}

org.springframework

spring-jdbc

${spring.jdbc.version}

log4j

log4j

${log4j.version}

src/main/resources

**/*.properties

**/*.xml

true

src/main/java

**/*.properties

**/*.xml

true

当依赖加载完成后,需要做一件事,如下操作。确保编译后的项目存在这些jar包。如果不做这个操作,运行项目可能会报类找不到错误提示

4、整合SSM

4.1 项目结构

我这里只给出整合的配置文件,其它代码+数据库 已经上传到Github。如有需要、可自行下载

4.2 整合mybatis

database.properties

jdbc.driver=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&serverTimezone=Asia/Shanghai

jdbc.username=root

jdbc.password=root

mybatis-config.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

Spring整合 Mybatis

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

4.3 整合springmvc

spring-mvc.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

4.4 整合spring

spring-service.xml

这里使用注解的形式注入各个层的对象、直接扫描整个包

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

4.5 汇总

applicationContext.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

4.6 web.xml

读取配置文件、加载配置文件的信息

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

>

springMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

springMVC

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

5、效果

配置Tomcat 的过程略。如有需要可以查看这篇文章:IDEA 配置Tomcat

在SSM框架的基础上 前端使用BootStrap + jsp(或html) 完成图书的基本增,删,改,查操作。效果如下:

相关文章