数据库环境
创建一个存放书籍数据的数据库表
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
<?xml version="1.0" encoding="UTF-8" ?> applicationContext.xml <?xml version="1.0" encoding="UTF-8"?>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的核心配置文件
<?xml version="1.0" encoding="UTF-8" ?> 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的包;
接口:
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
<?xml version="1.0" encoding="UTF-8"?><context:property-placeholder location=“classpath:database.properties”/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
3、Spring整合service层 <?xml version="1.0" encoding="UTF-8"?>
<context:component-scan base-package=“com.kuang.service” />
Spring层搞定!再次理解一下,Spring就是一个大杂烩,一个容器!对吧!SpringMVC层
1、web.xml
<?xml version="1.0" encoding="UTF-8"?> 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 <?xml version="1.0" encoding="UTF-8"?><mvc:annotation-driven />
mvc:default-servlet-handler/
<context:component-scan base-package=“com.kuang.controller” />
3、Spring配置整合文件,applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> 配置文件,暂时结束!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” %>
书籍列表 —— 显示所有书籍
<tbody>
<c:forEach var="book" items="${requestScope.get('list')}">
<tr>
<td>${book.getBookID()}</td>
<td>${book.getBookName()}</td>
<td>${book.getBookCounts()}</td>
<td>${book.getDetail()}</td>
<td>
<a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.getBookID()}">更改</a> |
<a href="${pageContext.request.contextPath}/book/del/${book.getBookID()}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
书籍编号 | 书籍名字 | 书籍数量 | 书籍详情 | 操作 |
---|
@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” %>
修改信息
@RequestMapping("/del/{bookId}")
public String deleteBook(@PathVariable(“bookId”) int id) {
bookService.deleteBookById(id);
return “redirect:/book/allBook”;
}
配置Tomcat,进行运行!
到目前为止,这个SSM项目整合已经完全的OK了,可以直接运行进行测试!这个练习十分的重要,大家需要保证,不看任何东西,自己也可以完整的实现出来!
项目结构图
如果运行的时候报错classnotfind
如果报错数据库连接不上
pom文件的依赖和database.properties还有mysql的驱动的mysql版本要对应,时区也要对应。改完从重新去lib目录下导入jar包