SSM整合记录

SSM整合记录

文章目录

SSM整合案例前言一、环境准备1.1、构建maven/Dynamic Web项目1.2、引入依赖/jar包1.3、创建数据库

二、项目搭建2.1、创建MVC三层架构

三、项目整合3.1、配置Web.xml文件3.2、配置applicationContext.xml3.3、配置Spring-MVC.xml3.4、配置MyBatis

四、编写前端

SSM整合案例

前言

首先,我们需要了解的前置知识,必须是学完了Spring,SpringMVC,MyBatis之后。因为SSM(Spring+SpringMVC+MyBatis)

回顾:

Spring:业务层框架,主要是完成对象的创建与管理SpringMVC:表现层框架,主要是替换原来的Servlet,转发请求MyBatis:著名持久层框架,事业服务与数据库之间的联系

一、环境准备

如果是构建的是Maven项目,则需要引入如下的依赖,如果是构建的Web项目,则需要引入jar包:

1.1、构建maven/Dynamic Web项目

构建一个Maven项目 或者构建一个Web项目

1.2、引入依赖/jar包

【版本对应】

MyBatis-Spring的官网对SSM整合的版本对应有了明确的说明,如下图:

【pom.xml】

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.wei

SSMBuilder

1.0-SNAPSHOT

SSMBuilder

war

UTF-8

1.8

1.8

5.8.2

5.2.8.RELEASE

8.0.28

3.5.5

jakarta.servlet

jakarta.servlet-api

5.0.0

provided

org.junit.jupiter

junit-jupiter-api

${junit.version}

test

org.junit.jupiter

junit-jupiter-engine

${junit.version}

test

mysql

mysql-connector-java

${mysql.version}

com.mchange

c3p0

0.9.5.5

com.github.pagehelper

pagehelper

5.2.0

commons-dbcp

commons-dbcp

1.4

javax.servlet

servlet-api

2.5

javax.servlet.jsp

jsp-api

2.2

javax.servlet

jstl

1.2

log4j

log4j

1.2.17

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

2.0.5

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.aspectj

aspectjweaver

1.8.4

org.projectlombok

lombok

1.18.12

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

org.apache.maven.plugins

maven-war-plugin

3.3.2

【导入所需要的Jar包】

1.3、创建数据库

创建一张用户表,用于演示用户管理系统的用户模块

CREATE TABLE `ssmbuilde`.`book`(

`book_Id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书本编号',

`book_Name` VARCHAR(100) NOT NULL COMMENT '书名',

`price` float(11,2) NOT NULL COMMENT '单价',

PRIMARY KEY (`book_Id`)

) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci;

二、项目搭建

2.1、创建MVC三层架构

pojo(实体类)、mapper/mapper.xml(ORM映射)、Service/ServiceImpl(业务层)、Controller(控制层)

三、项目整合

3.1、配置Web.xml文件

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

xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"

version="5.0">

contextConfigLocation

classpath:applicationContext.xml

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceRequestEncoding

true

forceResponseEncoding

true

characterEncodingFilter

/*

org.springframework.web.context.ContextLoaderListener

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

springmvc

/

注意顺序,先是过滤器、监听器、Servlet。同时在写完web.xml的时侯,还需要再资源目录(resources)下创建applicationContext.xml和Spring-MVC.xml的文件

3.2、配置applicationContext.xml

用于整合Spring、以及MyBatis的配置文件,包含了MyBatis的配置文件以及事物的相关操作

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

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

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

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

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

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

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

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

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

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

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

https://www.springframework.org/schema/aop/spring-aop.xsd">

3.3、配置Spring-MVC.xml

配置SpringMVC的配置文件,主要是视图解析器,静态资源以及注解驱动开启

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

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

3.4、配置MyBatis

整合MyBatis时候,不需要再设置数据源,数据源将在Spring里完成,Mybatis的配置文件只需要设置别名(typealiases、日志以及驼峰命名转化),以及扫描的包。

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

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

【db.preperties】

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

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC

jdbc.username=root

jdbc.password=123456

四、编写前端

省略。。。

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

首页查询

编号 书名 价格 操作
${books.bookId } ${books.bookName } ${books.price }

相关推荐

手机卡丢了怎么办?补办SIM卡全攻略 365网页版bet

手机卡丢了怎么办?补办SIM卡全攻略

📅 06-28 👁️ 2312
为啥晚上ATM都存不了钱?揭秘背后的原因与解决方案 365bet体育在线注册

为啥晚上ATM都存不了钱?揭秘背后的原因与解决方案

📅 08-21 👁️ 2728