目录
1、创建数据库
2、创建maven项目,添加web支持,导入所需依赖
3、创建基本结构
3、编写配置文件和接口以及实现类
4、Spring整合mybatis
5、springmvc层
6、编写控制类和视图层
准备环境
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,'从进门到进牢');
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
org.projectlombok
lombok
1.16.10
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
创建pojo、dao、service、controller包
配置资源文件
mybatis-config.xml
applicationContext.xml
数据库配置文件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=root
编写mybatis配置文件
编写实体类Books
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;
}
在Dao层写入接口
public interface BookMapper {
// 增加
int addBook(Books books);
// 删除
int deleteBookById(@Param("bookId") int id);
// 更新
int updateBook(Books books);
// 查询
Books queryBookById(int id);
// 查询所有
List queryAllBook();
}
编写对应的Mapper.xml文件
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}
编写Service层接口
public interface BookService {
// 增加
int addBook(Books books);
// 删除
int deleteBookById(int id);
// 更新
int updateBook(Books books);
// 查询
Books queryBookById(int id);
// 查询所有
List queryAllBook();
}
编写service实现类
public class BookServiceImpl implements BookService{
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
return bookMapper.addBook(books);
}
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();
}
}
spring-dao.xml
spring整合service层 spring-service.xml
编写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
编写spring-mvc.xml
spring整合文件 applicationContext.xml
package com.kun.controller;
import com.kun.pojo.Books;
import com.kun.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 {
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
@RequestMapping("/allBook")
public String list(Model model){
List list = bookService.queryAllBook();
model.addAttribute("list",list );
return "allBook";
}
@RequestMapping("/toAddBook")
public String toAdd(){
return "toAddBook";
}
@RequestMapping("/addBook")
public String addBook(Books books){
System.out.println(books);
bookService.addBook(books);
return "redirect:/book/allBook"; //重定向
}
// 修改界面
@RequestMapping("/toUpdateBook")
public String update(int id,Model model){
Books books = bookService.queryBookById(id);
model.addAttribute("QBook",books);
return "updateBook";
}
@RequestMapping("/updateBook")
public String updateBook(Books books){
System.out.println("update"+books);
bookService.updateBook(books);
return "redirect:/book/allBook";
}
// 删除书籍
@RequestMapping("/deleteBook")
public String delete(int id){
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
}
各个界面的jsp文件
首页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
首页
点击进入列表页
书籍列表界面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
书籍列表
添加书籍界面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
新增书籍
新增书籍
修改书籍界面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
修改信息
修改信息