这里以图书查询的功能为例子,来进行SSM的整合,从创建数据库,到导入jar包,一步一步的进行整合,直到能跑为止,我也是初学SSM整合,这里只是给出了能跑起来的代码,里面有简单的注释,并没有详细讲解原理,希望能帮到初学SSM整合的朋友,这些东西,可以拷过去直接用。
目录
首先给出整体的架构
创建数据库
导入maven依赖
opjo
dao层
service层
controller层
resource
BookMapper.xml
mybatis-config.xml
database.properties
spring-dao.xml
spring-service.xml
spring-mvc.xml
applicationContext.xml
jsp文件
部署到Tomcat上运行
首先给出整体的架构这里导出了数据库文件,可以直接使用,这里给出关于数据库的导入和导出问题
-- MySQL dump 10.13 Distrib 8.0.17, for Win64 (x86_64)---- Host: localhostDatabase: ssmbuild-- -------------------------------------------------------- Server version8.0.17/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!50503 SET NAMES utf8mb4 */;/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;/*!40103 SET TIME_ZONE='+00:00' */;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;---- Table structure for table `books`--DROP TABLE IF EXISTS `books`;/*!40101 SET @saved_cs_client = @@character_set_client */;/*!50503 SET character_set_client = utf8mb4 */;CREATE TABLE `books` ( `bookId` int(11) NOT NULL AUTO_INCREMENT, `bookName` varchar(255) DEFAULT NULL, `bookCount` int(11) DEFAULT NULL, `bookDetail` varchar(255) DEFAULT NULL, PRIMARY KEY (`bookId`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;/*!40101 SET character_set_client = @saved_cs_client */;---- Dumping data for table `books`--LOCK TABLES `books` WRITE;/*!40000 ALTER TABLE `books` DISABLE KEYS */;INSERT INTO `books` VALUES (1,'Java',1,'从入门到放弃'),(2,'MySQL',10,'从删库到跑路'),(3,'Linux',5,'从入门到入狱');/*!40000 ALTER TABLE `books` ENABLE KEYS */;UNLOCK TABLES;/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;-- Dump completed on 2020-01-18 16:41:28 导入maven依赖数据库导入成功之后,接下来开始创建一个maven项目,这里给出需要导入的依赖
4.0.0org.examplessmbuild1.0-SNAPSHOTjunitjunit4.12mysqlmysql-connector-java8.0.17com.mchangec3p00.9.5.2javax.servletservlet-api2.5javax.servlet.jspjsp-api2.2javax.servletjstl1.2org.mybatismybatis3.5.2org.mybatismybatis-spring2.0.2org.springframeworkspring-webmvc5.2.0.RELEASEorg.springframeworkspring-jdbc5.2.0.RELEASE opjo依赖导入完毕后,是pojo中的实体类
package com.zhiying.pojo;public class Books {private int bookId;private String bookName;private int bookCount;private String bookDetail;public Books() {}public Books(int bookId, String bookName, int bookCount, String bookDetail) {this.bookId = bookId;this.bookName = bookName;this.bookCount = bookCount;this.bookDetail = bookDetail;}public int getBookId() {return bookId;}public void setBookId(int bookId) {this.bookId = bookId;}public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName = bookName;}public int getBookCount() {return bookCount;}public void setBookCount(int bookCount) {this.bookCount = bookCount;}public String getBookDetail() {return bookDetail;}public void setBookDetail(String bookDetail) {this.bookDetail = bookDetail;}@Overridepublic String toString() {return "Books{" +"bookId=" + bookId +", bookName='" + bookName + '\'' +", bookCount=" + bookCount +", bookDetail='" + bookDetail + '\'' +'}';}} dao层 package com.zhiying.dao;import com.zhiying.pojo.Books;import java.util.List;public interface BookMapper {public int addBook(Books books);public int deleteBookById(int bookId);public int updateBook(Books books);public Books queryBookById(int bookId);public List queryAllBook();} service层 package com.zhiying.service;import com.zhiying.pojo.Books;import java.util.List;public interface BookService {public int addBook(Books books);public int deleteBookById(int bookId);public int updateBook(Books books);public Books queryBookById(int bookId);public List queryAllBook();} package com.zhiying.service;import com.zhiying.dao.BookMapper;import com.zhiying.pojo.Books;import java.util.List;public class BookServiceImpl implements BookService {//service调dao层private BookMapper bookMapper;public void setBookMapper(BookMapper bookMapper) {this.bookMapper = bookMapper;}public int addBook(Books books) {return bookMapper.addBook(books);}public int deleteBookById(int bookId) {return bookMapper.deleteBookById(bookId);}public int updateBook(Books books) {return bookMapper.updateBook(books);}public Books queryBookById(int bookId) {return bookMapper.queryBookById(bookId);}public List queryAllBook() {return bookMapper.queryAllBook();}} controller层 package com.zhiying.controller;import com.zhiying.pojo.Books;import com.zhiying.service.BookService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import java.util.List;@Controller@RequestMapping("/book")public class BookController {//controller调用service层@Autowired@Qualifier("BookServiceImpl")private BookService bookService;@RequestMapping("/allBook")public String list(Model model) {List list = bookService.queryAllBook();model.addAttribute("list", list);return "allBook";}} resource BookMapper.xml insert into books (bookName, bookCount, bookDetail)values (#{bookId}, #{bookCount}, #{bookDetail})delete from books where bookId = #{bookId}update booksset bookName = #{bookName}, bookCount = #{bookCount}, bookDetail = #{bookDetail}where bookId = #{bookId}select * from books where bookId = #{bookId}select * from books mybatis-config.xml database.properties jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTCjdbc.username=rootjdbc.password=root spring-dao.xml spring-service.xml spring-mvc.xml applicationContext.xml jsp文件最后是jsp文件
书籍展示书籍展示书籍编号书籍名称书籍数量书籍详情${book.bookId}${book.bookName}${book.bookCount}${book.bookDetail} 部署到Tomcat上运行