创建数据库
创建数据库
create DATABASE ssmbuild;
切换到当前数据库下面
use ssmbuild
查看原来有没有book这张表
drop TABLE if EXISTS books;`在这里插入代码片`
创建数据库表
CREATE TABLE book(
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 book(bookID,bookName,bookCounts,detail)
VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
程序员的本质任务就是crud
创建idea中MmzSsmLearning工程
具体步骤比较复杂,放在github的代码上面了
开始业务
报出了500这个错误
排错思路
bean不存在
1.查看这个bean注入是否成功
2.junit单元测试,看代码是否查询出来结果
3.问题,一定不在我们的底层,是spring出了问题
4.springmvc ,整合的时候,没有调用我们的service的bean:
1)ApplicationContext 没有整合bean
2)由于整合了springmvc,web.xml我们也绑定了spring-mvc配置文件
原来我们只在这里绑定了spring-mvc.xml ,所以导致spring的bean没有获取到
前后端增加书籍方法
前端jsp页面
这是我们增加书籍的页面
在前端输入之后,点击添加
<%--
Created by IntelliJ IDEA.
User: mmz
Date: 2021/4/12
Time: 20:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12" column>
<div class="page-header">
<h1>
<small>书籍列表 ------- 新增书籍</small>
</h1>
</div>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/addBook" method="post">
<div class="form-group">
<label>书籍名称</label>
<input type="text" class="form-control" name="bookName">
</div>
<div class="form-group">
<label>书籍数量</label>
<input type="text" class="form-control" name="bookCounts" >
</div>
<div class="form-group">
<label>书籍描述</label>
<input type="text" class="form-control" name="detail">
</div>
<div class="form-group">
<input type="submit" class="form-control" value="添加" >
</div>
</form>
</div>
</body>
</html>
这是整个的前端代码。可以看到form action中跳转到了book/addBook,在用SpringMVC跳转到Controller层面的这个接口
Controller接口
package com.mmz.controller;
import com.mmz.pojo.Book;
import com.mmz.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;
/**
* @Classname BookController
* @Description TODO
* @Date 2021/4/10 12:06
* @Created by mmz
*/
@Controller
@RequestMapping("/book")
public class BookController {
// controller 调用 service层
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
// 查询全部的书籍,并且返回到一个书籍展示页面
@RequestMapping("/allBook")
public String list(Model model){
List<Book> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "allBook";
}
// 跳转到增加书籍页面
@RequestMapping("/toAddPage")
public String toAddPage(){
return "addBook";
}
// 添加书籍请求
@RequestMapping("/addBook")
public String addBook(Book book ){
System.out.println(book);
bookService.addBook(book);
return "redirect:/book/allBook";
}
}
controller层会掉业务层bookService,然后涉及到了myBatis
Dao层,MyBatis
这里直接调用了addBook方法
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mmz.dao.BookMapper">
<insert id="addBook" parameterType="Book">
insert into book (bookName, bookCounts, detail)
values (#{bookName},#{bookCounts},#{detail});
</insert>
<delete id="deleteBookById" parameterType="int">
delete from book where bookID = #{bookId};
</delete>
<update id="updateBook" parameterType="Book">
update book
set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID = #{bookID};
</update>
<select id="queryBookById" resultType="Book">
select * from book where bookID = #{bookID};
</select>
<select id="queryAllBook" resultType="Book">
select * from book;
</select>
</mapper>
最后能在前端页面上进行返回