根据狂神大佬的教学项目整理笔记:
核心架构如下图:
分为步骤进行整合:
1.前期准备工作。(创建maven项目,导包,创建目录,创建数据库)
-
Mybatis 整合部分
-
1.1创建项目
-
1.2 添加web依赖
**
-
1.3连接数据库,创建表和数据
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.4 添加项目依赖 pom.xml
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.projectlombok
lombok
1.18.16
org.springframework
spring-webmvc
5.1.9.RELEASE
org.springframework
spring-jdbc
5.1.9.RELEASE
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
-
1.5 添加artifacts 依赖,新建lib文件,
添加,选中全部加入
1.6 创建项目包结构和基本数据
com.zhy.pojo
com.zhy.controller
com.zhy.service
com.zhy.dao
新建 mybatis-config.xml
新建 applicationContext.xml
新建 database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
数据库mysql 8.0+ 需要注意
- 时区 :serverTimezone=Asia/Shanghai
- useSSL=false
- 1.7 编写mybatis配置
package com.zhy.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;
}
编写BookMapper 接口
package com.zhy.dao;
import com.zhy.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.awt.print.Book;
import java.util.List;
public interface BookMapper {
/***
* 新增一个books
* @param books
* @return
*/
int addBoook(Books books);
/***
* 删除一个books
* @param bookID
* @return
*/
int deleteBook(@Param("bookID") int bookID);
/***
* 更新一个books
* @param books
* @return
*/
int updateBook(Books books);
/***
* 根据id查询一个books
* @param bookID
* @return
*/
Books queryBookByID(@Param("bookID") int bookID);
/***
* 查询所有books
* @return
*/
List queryBookAll();
}
编写 BookMapper.xml
- IDEA 编写数据库 快捷 用法
- 插入 ins
- 更新 upd
- 查询 sel
经验:如果没有提示数据库名字和表名可以参考如下设置:
insert into ssmbuild.books (bookID, bookName, bookCounts, detail)
values (#{bookID},#{bookName},#{bookCounts},#{detail});
delete from ssmbuild.books where bookID=#{bookID}
update ssmbuild.books
set bookName = #{bookName} , bookCounts=#{bookCounts} ,detail=#{detail}
where bookID=#{bookID};
别忘了在 mybatis-config.xml 配置mapper
创建 BooksService
package com.zhy.service;
import com.zhy.pojo.Books;
import java.util.List;
public interface BooksService {
/***
* 新增一个books
* @param books
* @return
*/
int addBoook(Books books);
/***
* 删除一个books
* @param bookID
* @return
*/
int deleteBook( int bookID);
/***
* 更新一个books
* @param books
* @return
*/
int updateBook(Books books);
/***
* 根据id查询一个books
* @param bookID
* @return
*/
Books queryBookByID( int bookID);
/***
* 查询所有books
* @return
*/
List queryBookAll();
}
创建 BooksServiceImpl 业务层实现类
package com.zhy.service;
import com.zhy.dao.BookMapper;
import com.zhy.pojo.Books;
import java.util.List;
@Service
public class BooksServiceImpl implements BooksService{
/***
* service 业务层 持有 dao层进行业务处理
* service 可以使用dao的方法
*/
private BookMapper bookMapper;
/***
* set 方法提供spring 进行ioc注入
* @param bookMapper
*/
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBoook(Books books) {
return bookMapper.addBoook(books);
}
public int deleteBook(int bookID) {
return bookMapper.deleteBook(bookID);
}
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
public Books queryBookByID(int bookID) {
return bookMapper.queryBookByID(bookID);
}
public List queryBookAll() {
return bookMapper.queryBookAll();
}
}
============================Mybatis 配置基本结束=================================
Spring 整合 dao(mybatis) 层
- 2.1 新建 spring-dao.xml
Spring 整合 service 层
- 2.2 新建 spring-service.xml
记得在 applicationContext.xml 导入关联
==================Spring 整合基本结束======================
- SpringMVC层
- 3.1 web.xml
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
DispatcherServlet
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
15
- 3.2 新建spring-mvc.xml
- 3.3 最后添加配置依赖关系 applicationContext.xml
最后编写controler
package com.zhy.controller;
import com.zhy.pojo.Books;
import com.zhy.service.BooksService;
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 {
@Autowired
@Qualifier("BooksServiceImpl")
private BooksService bookService;
@RequestMapping("/allBook")
public String list(Model model) {
List list = bookService.queryBookAll();
model.addAttribute("list", list);
return "allBook";
}
}
最后配置几个页面进行测试 allBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${list}
配置tomcat 进行测试
浏览器输入 http://localhost:8081/book/allBook
[图片上传失败...(image-dbbcaa-1604987605888)]
测试完成
至此简单的SSM 架构搭建完毕,后面都是页面和义务代码,我直接贴了
- 1 index.jsp 完整页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
首页
点击进入列表页
-
- 书籍列表页面 allbook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
书籍列表
-
- BookController 类编写
package com.zhy.controller;
import com.zhy.pojo.Books;
import com.zhy.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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
@RequestMapping("/allBook")
public String list(Model model){
final Books books1 = bookService.queryBookById(1);
System.out.println(books1);
final List list = bookService.queryAllBook();
for (Books books : list) {
System.out.println(books);
}
model.addAttribute("list",list);
return "allBook";
}
@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";
}
@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";
}
@RequestMapping("/del/{bookId}")
public String deleteBook(@PathVariable("bookId") int id) {
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
}
- 4 updateBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
修改信息
修改信息
至此完整的项目编写完毕。收获满满~这个项目我练习了两遍,因为狂神说这是基础入门,一定要熟练掌握基础。
这是我第一次练习的代码仅供参考,和上面截图有点不一样:
练习项目 https://gitee.com/zhyazaq/sbmbuild
想学习后台的可以 看https://www.bilibili.com/read/cv5702420 狂神教学,一起学习一起进步~撒