16.SSM整合

环境要求

  • IDEA
  • Mysql 8.0.21
  • Tomcat 9
  • Maven 3.6

基本环境搭建

项目结构:


image.png

1.新建一个Maven项目,添加web支持

  1. 导入依赖

        
        
            junit
            junit
            4.13
        
        
        
            mysql
            mysql-connector-java
            8.0.21
        
        
        
            com.mchange
            c3p0
            0.9.5.2
        

        
        
            javax.servlet
            servlet-api
            2.5
            provided
        
        
            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.2.9.RELEASE
        
        
        
            org.projectlombok
            lombok
            1.18.12
        
    

3.Maven资源过滤设置

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

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

Mybatis层编写

  1. 数据库配置文件 database,properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/own?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123
  1. 编写mybatis核心配置文件mybatis-config.xml



    
        
    
    
        
    


3.编写数据库对应实体类
使用lombok插件!

package com.yc.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author hillock
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {

    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;

}

4.编写Dao层的Mapper接口

package com.yc.dao;

import com.yc.pojo.Books;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author hillock
 */
@Repository
public interface BookMapper {
    /**
     * 增加一个Book
     * @param book
     * @return int
     * */
    int addBook(Books book);
    /**
     * 根据id删除一个Book
     * @param id
     * @return int
     * */
    int deleteBookById(int id);
    /**
     * 更新Book
     * @param books
     * @return int
     * */
    int updateBook(Books books);
    /**
     * 根据id查询,返回一个Book
     * @param id
     * @return books
     * */
    Books queryBookById(int id);
    /**
     * 查询全部Book,返回list集合
     * @return List
     * */
    List queryAllBook();
}

5.编写接口对应的Mapper文件BookMapper.xml






    
    
      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}
   

    
    

    
    


6.编写Service层接口和实现类

package com.yc.service;

import com.yc.pojo.Books;

import java.util.List;

/**
 * @author hillock
 */
public interface BookService {
    /**
     * 增加一个Book
     * @param book
     * @return int
     * */
    int addBook(Books book);
    /**
     * 根据id删除一个Book
     * @param id
     * @return int
     * */
    int deleteBookById(int id);
    /**
     * 更新Book
     * @param books
     * @return int
     * */
    int updateBook(Books books);
    /**
     * 根据id查询,返回一个Book
     * @param id
     * @return books
     * */
    Books queryBookById(int id);
    /**
     * 查询全部Book,返回list集合
     * @return List
     * */
    List queryAllBook();
}

实现类:

package com.yc.service.impl;

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

import java.util.List;

/**
 * @author hillock
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private 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();
    }
}

Spring层

1.Spring整合Mybatis spring-dao.xml




    
    
    

    
    
    
        
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
    
        
        
        
        
    
    
    
        
        
    

  1. Spring和service层** spring-service.xml**



    
    

SpringMvc层

1.web.xml




    
    
        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




    
    
    
    
    

    
    
        
        
        
    

    
    


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




    
    
    


配置文件,暂时结束!Controller 和 视图层编写
1.BookController

package com.yc.controller;

import com.yc.pojo.Books;
import com.yc.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 org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hillock
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/allBook")
    public String list(Model model) {
        List list = bookService.queryAllBook();
        model.addAttribute("list", list);
        return "allBook";
    }
}

2.书籍列表页面 allbook.jsp

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


   书籍列表
   
   
   



书籍编号 书籍名字 书籍数量 书籍详情 操作
${book.getBookID()} ${book.getBookName()} ${book.getBookCounts()} ${book.getDetail()} 更改 | 删除

3.添加lib【https://www.jianshu.com/p/c8c392d752fb】

4.配置tomcat【https://www.jianshu.com/p/1823fcce668b】

5.测试


image.png

你可能感兴趣的:(16.SSM整合)