SSM框架整合

环境要求

环境:

  • IDEA
  • MySQL 5.7.19
  • Tomcat 9
  • Maven 3.6

要求:

  • 需要熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识;

项目结构图

java目录

  • pojo
  • dao
  • service
  • controller

resources目录

  • database.properties
  • mybatis-config.xml
  • spring-dao.xml
  • spring-service.xml
  • spring-mvc.xml
  • applicationContext.xml

web目录

  • index.jsp
  • WEB-INF
    • jsp目录
    • web.xml

数据库环境

创建一个存放书籍数据的数据库表

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
    
    
    
        org.projectlombok
        lombok
        1.16.10
    
    
    
        org.aspectj
        aspectjweaver
        1.8.13
    

3、Maven资源过滤设置


   
       
           src/main/java
           
               **/*.properties
               **/*.xml
           
           false
       
       
           src/main/resources
           
               **/*.properties
               **/*.xml
           
           false
       
   

4、建立基本结构和配置框架!

java目录 下建立四个包

com.qiu.pojo
com.qiu.dao
com.qiu.service
com.qiu.controller

mybatis-config.xml






applicationContext.xml





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的核心配置文件


        

    
        
        
    
    
        
        
    
    
        
    

4、编写数据库对应的实体类 com.qiu.pojo.Books

使用 lombok 插件!

package com.qiu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}

5、编写Dao层的 Mapper 接口!

package com.qiu.dao;
import com.qiu.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
    //增加一本图书
    int addBook(Books books);
    //删除一本图书
    int deleteBook(@Param("bookId") int id);
    //更新一本图书
    int updateBook(Books books);
    //查询一本图书
    Books queryBookById(@Param("bookId") int id);
    //查询全部的图书
    List queryAllBook();
    //根据书名查询图书
    List queryBookByName(@Param("bookName") String bookName);
}

6、编写接口对应的 BookMapper.xml 文件。需要导入 MyBatis 的包;




    
        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};
    

    

    

    

7、编写 Service 层的接口和实现类

接口:

package com.qiu.service;
import com.qiu.pojo.Books;
import java.util.List;
public interface BookService {
    //增加一本图书
    int addBook(Books books);
    //删除一本图书
    int deleteBook(int id);
    //更新一本图书
    int updateBook(Books books);
    //查询一本图书
    Books queryBookById(int id);
    //查询全部的图书
    List queryAllBook();
    //根据书名查询图书
    List queryBookByName(String bookName);
}

实现类:

package com.qiu.service;
import com.qiu.dao.BookMapper;
import com.qiu.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 books) {
        return bookMapper.addBook(books);
    }

    public int deleteBook(int id) {
        return bookMapper.deleteBook(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 List queryBookByName(String bookName) {
        return bookMapper.queryBookByName(bookName);
    }
}

底层需求操作编写完毕!

Spring层编写

1、配置Spring整合MyBatis,我们这里数据源使用c3p0连接池;

2、我们去编写Spring整合Mybatis的相关的配置文件;【spring-dao.xml




    
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
    
    
    
        
        
        
        
    

3、Spring整合service层(配置文件:spring-service.xml




    
    

    
    
        
    

    
    
        
        
    

    
    
    
    
        
        
        
            
            
        
    
    
    
        
        
        
    

Spring层搞定!

SpringMVC层编写

1、web.xml



    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:applicationContext.xml
        
        1
    
    
        SpringMVC
        /
    

    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        encodingFilter
        /*
    

    
    
        
        15
    

2、spring-mvc.xml




    
    
    
    
    
    
    
    
        
        
    

3、Spring配置整合文件,applicationContext.xml




    
    
    

配置文件,暂时结束!

Controller 和 视图层

1、BookController 类编写 ,方法一:查询全部书籍

import com.qiu.pojo.Books;
import com.qiu.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 {
    // controller 调用 service 层
    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    //查询全部的书籍,并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model){
        List books = bookService.queryAllBook();
        model.addAttribute("list",books);
        return "allBook";
    }
}

2、编写首页 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    主页
    
  
  
    

进入书籍展示页面

3、书籍列表页面 allBook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    书籍展示
    <%--BootStrap 美化界面--%>
    

    
        
<%--查询书籍--%>
${error}
<%--toAddBook--%> 新增书籍
<%--书籍从数据库中查询出来,从这个list中遍历出来:foreach--%>
书籍编号 书籍名称 书籍数量 书籍详情 操作
${book.bookID} ${book.bookName} ${book.bookCounts} ${book.detail} 修改   |   删除

4、BookController 类编写 , 方法二:添加书籍

//跳转到增加书籍页面
@RequestMapping("/toAddBook")
public String toAddPaper(){
    return "addBook";
}

//添加书籍的请求
@RequestMapping("/addBook")
public String addBook(Books books){
    System.out.println("addBook=>" + books);
    bookService.addBook(books);
    return "redirect:/book/allBook";    //重定向到我们的allBook
}

5、添加书籍页面:addBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    新增图书
    <%--BootStrap 美化界面--%>
    


6、BookController 类编写 , 方法三:修改书籍

//跳转到修改书籍页面
@RequestMapping("/toUpdateBook/{bookId}")
public String toUpdatePaper(@PathVariable ("bookId") int id,Model model){
    Books book = bookService.queryBookById(id);
    model.addAttribute("QBook",book);
    return "updateBook";
}

//修改书籍的请求
@RequestMapping("/updateBook")
public String updateBook(Books books){
    System.out.println("updateBook=>" + books);
    bookService.updateBook(books);
    return "redirect:/book/allBook";
}

7、修改书籍页面 updateBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    修改图书
    <%--BootStrap 美化界面--%>
    


8、BookController 类编写 , 方法四:删除书籍

//删除书籍
@RequestMapping("/deleteBook/{bookId}")
public String deleteBook(@PathVariable ("bookId") int id){
    bookService.deleteBook(id);
    return "redirect:/book/allBook";
}

9、BookController 类编写 , 方法五:根据书名模糊查询书籍

//查询书籍
@RequestMapping("/queryBook")
public String queryBook(String queryBookName,Model model){
    List books = bookService.queryBookByName(queryBookName);
    if(books.size()==0){
        books=bookService.queryAllBook();
        model.addAttribute("error","未查到");
    }
    model.addAttribute("list",books);
    return "allBook";
}

配置Tomcat,进行运行!

到目前为止,这个SSM项目整合已经完全的OK了,可以直接运行进行测试!

SSM框架整合_第1张图片

本篇总结参考 B站狂神说Java:https://space.bilibili.com/95256449

你可能感兴趣的:(SSM框架整合)