SpringMVC 整合SSM框架详解

网友投稿 240 2022-10-01


SpringMVC 整合SSM框架详解

整合SSM

环境要求

环境:

IDEA

mysql5.7.19

Tomcat9

Maven3.6

要求:

需要熟练掌握MySQL数据库,Spring,javaWeb及MyBatis知识,简单的前端知识;

数据库环境

创建一个存放书籍数据的数据库表

CREATE DATABASE `ssmbuild`;

USE `ssmbuild`;

DROP TABLE IF EXISTS `books`;

CREATE TABLE `books` (

`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',

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

`bookCounts` INT(11) NOT NULL COMMENT '数量',

`detail` VARCHAR(200) NOT NULL COMMENT '描述',

KEY `bookID` (`bookID`)

) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES

(1,'Java',1,'从入门到放弃'),

(2,'MySQL',10,'从删库到跑路'),

(3,'linux',5,'从进门到进牢');

基本环境搭建

1、新建一Maven项目!ssmbuild , 添加web的支持

2、导入相关的pom依赖!

junit

junit

4.12

mysql

mysql-connector-java

5.1.47

com.mchange

c3p0

0.9.5.2

javax.servlet

servlet-api

2.5

javax.servlet.jsp

jsp-api

2.2

javax.servlet

jstl

1.2

org.mybatis

mybatis

3.5.2

org.mybatis

mybatis-spring

2.0.2

org.springframework

spring-webmvc

5.1.9.RELEASE

org.springframework

spring-jdbc

5.1.9.RELEASE

3、Maven资源过滤设置

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

4、建立基本结构和配置框架!

com.kuang.pojo

com.kuang.dao

com.kuang.service

com.kuang.controller

mybatis-config.xml

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

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

applicationContext.xml

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

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

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

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

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

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

Mybatis层编写

1、数据库配置文件 database.properties

jdbc.driver=com.mysql.jdbc.Driver

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

jdbc.username=root

jdbc.password=123456

2、IDEA关联数据库

3、编写MyBatis的核心配置文件

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

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

4、编写数据库对应的实体类 com.kuang.pojo.Books 使用lombok插件!

package com.kuang.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

@Data

@AllArgsConstructor

@NoArgsConstructor

public class Books {

private int bookID;

private String bookName;

private int bookCounts;

private String detail;

}

5、编写Dao层的 Mapper接口!

package com.kuang.dao;

import com.kuang.pojo.Books;

import java.util.List;

public interface BookMapper {

//增加一个Book

int addBook(Books book);

//根据id删除一个Book

int deleteBookById(int id);

//更新Book

int updateBook(Books books);

//根据id查询,返回一个Book

Books queryBookById(int id);

//查询全部Book,返回list集合

List queryAllBook();

}

6、编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;

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

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

insert into ssmbuild.books(bookName,bookCounts,detail)

values (#{bookName}, #{bookCounts}, #{detail})

delete from ssmbuild.books where bookID=#{bookID}

update ssmbuild.books

set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}

where bookID = #{bookID}

select * from ssmbuild.books

where bookID = #{bookID}

SELECT * from ssmbuild.books

7、编写Service层的接口和实现类

接口:

package com.kuang.service;

import com.kuang.pojo.Books;

import java.util.List;

//BookService:底下需要去实现,调用dao层

public interface BookService {

//增加一个Book

int addBook(Books book);

//根据id删除一个Book

int deleteBookById(int id);

//更新Book

int updateBook(Books books);

//根据id查询,返回一个Book

Books queryBookById(int id);

//查询全部Book,返回list集合

List queryAllBook();

}

实现类:

package com.kuang.service;

import com.kuang.dao.BookMapper;

import com.kuang.pojo.Books;

import java.util.List;

public class BookServiceImpl implements BookService {

//调用dao层的操作,设置一个set接口,方便Spring管理

private BookMapper bookMapper;

public void setBookMapper(BookMapper bookMapper) {

this.bookMapper = bookMapper;

}

public int addBook(Books book) {

return bookMapper.addBook(book);

}

public int deleteBookById(int id) {

return bookMapper.deleteBookById(id);

}

public int updateBook(Books books) {

return bookMapper.updateBook(books);

}

public Books queryBookById(int id) {

return bookMapper.queryBookById(id);

}

public List queryAllBook() {

return bookMapper.queryAllBook();

}

}

OK,到此,底层需求操作编写完毕!

Spring层

1、配置Spring整合MyBatis,我们这里数据源使用c3p0连接池;

2、我们去编写Spring整合Mybatis的相关的配置文件;spring-dao.xml

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

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

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

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

http://springframework.org/schema/context

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

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

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

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

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

http://springframework.org/schema/context

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

3、Spring整合service层

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

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

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

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

http://springframework.org/schema/context

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

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

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

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

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

http://springframework.org/schema/context

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

Spring层搞定!再次理解一下,Spring就是一个大杂烩,一个容器!对吧!

SpringMVC层

1、web.xml

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

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

DispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

DispatcherServlet

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

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

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

DispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

DispatcherServlet

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

2、spring-mvc.xml

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

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

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

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

3、Spring配置整合文件,applicationContext.xml

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

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

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

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

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

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

配置文件,暂时结束!Controller 和 视图层编写

1、BookController 类编写 , 方法一:查询全部书籍

@Controller

@RequestMapping("/book")

public class BookController {

@Autowired

@Qualifier("BookServiceImpl")

private BookService bookService;

@RequestMapping("/allBook")

public String list(Model model) {

List list = bookService.queryAllBook();

model.addAttribute("list", list);

return "allBook";

}

}

2、编写首页 index.jsp

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

点击进入列表页

3、书籍列表页面 allbook.jsp

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

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

书籍列表 —— 显示所有书籍

新增

书籍编号

书籍名字

书籍数量

书籍详情

操作

更改 |

删除

4、BookController 类编写 , 方法二:添加书籍

@RequestMapping("/toAddBook")

public String toAddPaper() {

return "addBook";

}

@RequestMapping("/addBook")

public String addPaper(Books books) {

System.out.println(books);

bookService.addBook(books);

return "redirect:/book/allBook";

}

5、添加书籍页面:addBook.jsp

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

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

新增书籍

书籍名称:


书籍数量:


书籍详情:


6、BookController 类编写 , 方法三:修改书籍

@RequestMapping("/toUpdateBook")

public String toUpdateBook(Model model, int id) {

Books books = bookService.queryBookById(id);

System.out.println(books);

model.addAttribute("book",books );

return "updateBook";

}

@RequestMapping("/updateBook")

public String updateBook(Model model, Books book) {

System.out.println(book);

bookService.updateBook(book);

Books books = bookService.queryBookById(book.getBookID());

model.addAttribute("books", books);

return "redirect:/book/allBook";

}

7、修改书籍页面 updateBook.jsp

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

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

修改信息

书籍名称:

书籍数量:

书籍详情:

8、BookController 类编写 , 方法四:删除书籍

@RequestMapping("/del/{bookId}")

public String deleteBook(@PathVariable("bookId") int id) {

bookService.deleteBookById(id);

return "redirect:/book/allBook";

}

配置Tomcat,进行运行!

到目前为止,这个SSM项目整合已经完全的OK了,可以直接运行进行测试!这个练习十分的重要,大家需要保证,不看任何东西,自己也可以完整的实现出来!

项目结构图

小结及展望

这个是SSM整合案例,一定要烂熟于心!

SSM框架的重要程度是不言而喻的,学到这里,大家已经可以进行基本网站的单独开发。但是这只是增删改查的基本操作。可以说学到这里,大家才算是真正的步入了后台开发的门。也就是能找一个后台相关工作的底线。

或许很多人,工作就做这些事情,但是对于个人的提高来说,还远远不够!

我们后面还要学习一些 SpringMVC 的知识!

Ajax 和 Json

文件上传和下载

拦截器


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:计算机网络知识点的全面总结(计算机网络 知识点总结)
下一篇:如何进行服务器安全防护?常用防范措施有哪些?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~