六、整合SSM
6.1、搭建框架6.2、小项目测试(书籍展示)
6.2.1、数据库环境6.2.2、基本环境6.2.3、Mybatis层6.2.4、Spring层6.2.5、SpringMVC层 六、整合SSM 6.1、搭建框架
1、pom.xml导入项目依赖(junit,mysql-connector-java,c3p0,servlet-api,jsp-api,jstl,mybatis,mybatis-spring,spring-webmvc,spring-jdbc,lombok,aspectjweaver)
2、配置maven过滤问题
3、建立基本结构和配置框架!
com.rui.pojocom.rui.daocom.rui.servicecom.rui.controller
4、编写数据库配置文件 database.properties
jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=truejdbc.username=rootjdbc.password=123456
5、编写MyBatis的核心配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
6、编写Spring整合Mybatis的相关的配置文件;spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
7.编写Spring整合service层的相关的配置文件;spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
8、编写SpringMVC层的web.xml
<?xml version="1.0" encoding="UTF-8"?>
2、建立基本结构和配置框架!
com.kuang.pojocom.kuang.daocom.kuang.servicecom.kuang.controller 6.2.3、Mybatis层
1、数据库配置文件 database.properties
jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=truejdbc.username=rootjdbc.password=123456
2、IDEA关联数据库
3、编写MyBatis的核心配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
4、编写数据库对应的实体类 com.rui.pojo.Books
使用lombok插件!
package com.rui.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class Books { private int bookID; private String bookName; private int bookCounts; private String detail; }
5、编写Dao层的 Mapper接口!BookMapper.java
package com.rui.dao;import com.rui.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
6、编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;
<?xml version="1.0" encoding="UTF-8" ?>
7、编写Service层的接口和实现类
接口:
package com.rui.service;import com.rui.pojo.Books;import org.apache.ibatis.annotations.Param;import java.util.List;public interface BookService { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(@Param("bookId") int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(@Param("bookId")int id); //查询全部的书 List
实现类:
package com.rui.service;import com.rui.dao.BookMapper;import com.rui.pojo.Books;import java.util.List;public class BookServiceImpl implements BookService{ //service调用dao层 private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public int addBook(Books books) { return bookMapper.addBook(books); } @Override public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } @Override public int updateBook(Books books) { return bookMapper.updateBook(books); } @Override public Books queryBookById(int id) { return bookMapper.queryBookById(id); } @Override public List
1、编写Spring整合Mybatis的配置文件;spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
2、Spring整合service层配置文件spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
1、web配置文件web.xml
<?xml version="1.0" encoding="UTF-8"?>
2、SpringMVC到servlet的核心配置文件spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
3、Spring配置整合文件,applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
4、编写Controller 和 视图层
Controller:创建BookController
package com.rui.controller;import com.rui.pojo.Books;import com.rui.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.ArrayList;import java.util.List;@Controller@RequestMapping("/book")public class BookController { //controller层调用service层 @Autowired @Qualifier("BookServiceImpl") private BookService bookService; //查询全部书籍,并返回到一个所有书籍页面 @RequestMapping("/allBook") public String list(Model model){ List
视图层:index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
在WEB-INF文件夹下创建jsp文件夹:allBook.jsp、addBook.jsp、updateBook.jsp
allBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ page contentType="text/html;charset=UTF-8" language="java" %> 书籍展示——————显示所有书籍
新增书籍 显示全部书籍 书籍编号 书籍名称 书籍数量 书籍详情 操作
addBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
updateBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
项目结构如图:
最后启动Tomcat测试!