前言
在学习完Mybaits+Spring+SpringMVC之后,对学过的知识做一个框架搭建。所以搭建SSM框架来做一个书城项目的后端,里面实现了几个基本的增删改查的功能。
开发工具
数据库环境
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
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
4.建立基本结构和框架配置
分别有:
在搭建SSM框架的时候的顺序为:dao→service→controller
Mybatis层编写
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
//下面生成有参、无参构造和getter、setter方法、toString省略,可以使用lomnok来进行
}
package cn.zhku.dao;
import cn.zhku.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById(@Param("bookId") int id);
//更新一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(@Param("bookId") int id);
//查询全部书
List queryAllBook();
//根据书名查询书本信息
Books queryBookByName(@Param(("bookName")) String bookName );
}
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}
package cn.zhku.service;
import cn.zhku.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookService {
//增加一本书
int addBook(Books books);
//删除一本书
int deleBookById(int id);
//更新一本书
int updateBook(Books books);
//查询一本书
Books queryBookById( int id);
//查询全部书
List queryAllBook();
Books queryBookByName(String bookName);
}
实现类:
package cn.zhku.service;
import cn.zhku.dao.BookMapper;
import cn.zhku.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService{
private BookMapper bookMapper;
//这里注意要写一个bookMapper的set方法,装载进bean中
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
return bookMapper.addBook(books);
}
public int deleBookById(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();
}
public Books queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}
}
mybatis底层需求编写完毕!
spring层
//下面是映射扫描器,配置扫描Dao接口包,动态实现Dao接口注入到spring容器中,负责直接扫描BookMapper接口而不用写实现类,要注意下面的属性: sqlSessionFactoryBeanName的value要对应上面的sqlSessionFactory
到这里spring层已经整合完了!
SpringMVC层
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
DispatcherServlet
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
15
到这里框架整合的配置文件就写完了!后面我们只要关注具体需要的功能,然后在页面和controller去做文章即可!
controller和视图层编写
@Controller
@RequestMapping("/book")
public class BookController {
//controller调service层
@Autowired
private BookService bookService;
//查询全部的数据,并且返回到一个书籍展示页面
@RequestMapping("/allBook")
public String list(Model model){
List books = bookService.queryAllBook();
model.addAttribute("list",books);
//返回到WEB-INF/jsp/allBook.jsp
return "allBook";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
进入书籍页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
书籍列表
@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";
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
新增书籍
新增书籍
@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";
}
<%@ 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框架就整合完毕了。
总结
搭建完SSM框架后整个项目的结构就变得十分清晰了,如果想要添加功能,只要按照流程去修改添加就好了,所以SSM框架整合的复用性很高!