SSM框架整合---2021-10-20

SSM框架整合---2021-10-20,第1张

SSM框架整合---2021-10-20
          SSM框架整合
项目结构:

SSM整合顺序:

1.dao层: 数据库,mybatis层的编写
2.Spring层:主要是spring整合mybatis
3.SpringMVC层:主要处理前后端的交互

1.创建数据库环境

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

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,'从进门到进牢');
2.导入maven 相关的依赖包
在这里 
        
        
            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.thymeleaf
            thymeleaf-spring5
            3.0.12.RELEASE
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.3
        
               
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            org.slf4j
            slf4j-log4j12
            1.7.14
            test
        
             
        
            com.github.pagehelper
            pagehelper
            5.1.2
        

        
        
            org.hibernate.validator
            hibernate-validator
            6.2.0.Final
        



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


插入代码片
3.创建对应的包、基本的结构框架
  • com.controller
  • com.dao
  • com.pojo
  • com.service

4.编写对应的实体类

package com.pojo;


import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;

public class Books {

    private int bookID;
    @NotEmpty(message = "书名不为空")
    private String bookName;
    private int bookCounts;
    private String detail;

    public Books() {
    }

    public Books(int bookID, String bookName, int bookCounts, String detail) {
        this.bookID = bookID;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.detail = detail;
    }

    @Override
    public String toString() {
        return "Books{" +
                "bookID=" + bookID +
                ", bookName='" + bookName + ''' +
                ", bookCounts=" + bookCounts +
                ", detail='" + detail + ''' +
                '}';
    }

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getBookCounts() {
        return bookCounts;
    }

    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }
}


5.编写配置文件

全局配置文件 applicationContext.xml




1.mybatis层

1.数据库连接database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root

1.mybatis核心配置文件 mybatis-config.xml




    
    
        
    
    
        
    

编写Dao层的 bookMapper接口

package com.dao;

import com.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.awt.print.Book;
import java.util.List;

public interface BookMapper {

    //获取所有的图书
   List getAllBook();

   //根据id获取书籍
   Books getBookById(@Param("bookID") int bookID);

   //添加一个书籍
    int addBook(Books book);

    //更新一个书籍
    int updateBook(Books book);

    //删除指定id的书籍
    int deleteBookById(@Param("bookID") int bookID) ;

}

编写编写接口对应的 bookMapper.xml 文件




    
        select * from books where bookID=#{bookID}
    

    
        insert into books (bookName,bookCounts,detail) values(#{bookName},#{bookCounts},#{detail})

    

    
        update books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookID=#{bookID}
    
    
    
        delete from books where bookID=#{bookID}
    


2.service层

bookService接口:

package com.service;

import com.pojo.Books;

import java.util.List;

public interface BookService {

    List getAllBook();

    Books getBookById(int bookID);

    int updateBook(Books book);

    int addBook(Books book);

    int deleteBookById(int bookId);

}

bookServiceIm接口实现类

package com.service;

import com.dao.BookMapper;
import com.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

public class BookServiceIm implements BookService {

    @Autowired
    private BookMapper bookMapper;

    public BookMapper getBookMapper() {
        return bookMapper;
    }

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    public List getAllBook() {
        return bookMapper.getAllBook();
    }

    public Books getBookById(int bookID) {
        return bookMapper.getBookById(bookID);
    }

    public int updateBook(Books book) {
        return bookMapper.updateBook(book);
    }

    public int addBook(Books book) {
        return bookMapper.addBook(book);
    }

    public int deleteBookById(int bookId) {
        return bookMapper.deleteBookById(bookId);
    }
}

mybatis完成。。

1.spring层

spring整合mybatis,database.peoperties配置文件–.数据库连接池datasource---->注入sqlSessionfactoryBean,MapperScannerConfigurer将dao层导入spring中,交给spring管理
编写配置文件 spring-dao.xml:




    

    
        
        
        
        
    

    
        
        
        
    

    
        
        
    

2.Spring整合service层

Spring-Service.xml:




    

    
        
    

    
        
    


spring整合完成

3. SpringMVC层

web.xml:




    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceResponseEncoding
            true
        
    
    
        CharacterEncodingFilter
        /*
    

    
        HiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    
    
        HiddenHttpMethodFilter
        /*
    

    
        HttpPutFormContentFilter
        org.springframework.web.filter.HttpPutFormContentFilter
    
    
        HttpPutFormContentFilter
        /*
    

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

springMVC.xml




    

    

    
    

    
        
        
        
            
                
                    
                        
                        
                        
                        
                        
                        
                    
                
            
        
    

全局配置文件applicationContext.xml



    
    
    

配置结束

编写controller层

bookController

package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Books;
import com.pojo.Msg;
import com.service.BookServiceIm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Controller
public class BookController {
    @Autowired
    private BookServiceIm bookServiceIm;

    @RequestMapping(value = "/books", method = RequestMethod.GET)
     public String getBooks(Model model, Integer pn) {
        PageHelper.startPage(1, 5);
        List bookList = bookServiceIm.getAllBook();
        PageInfo page = new PageInfo(bookList);
        model.addAttribute("PageInfo", page);
        model.addAttribute("bookList", bookList);
        return "bookList";
    }


}


编写前端页面:bookList.html




    
    Title
    
    
    



    
        
            SSM-CRUD					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4748515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存