SSM (spring mybatis spring MVC) framework integration [Full Version]

Time:2021-12-29

Integrate SSM


01 relationship between basic configuration files

web. XML configurationDispatcherServlet


02 Maven dependency required

log4j
        log4j
        1.2.17
    
    
    
    
        org.projectlombok
        lombok
        1.16.10
    
    
    
        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

1、 Spring integrates mybatis configuration

01 mybatis core configuration file mybatis config xml

  • Alias
  • Add mapper mapping
  • Add setting

02 spring integration configuration file spring Dao xml

  • Associate database configuration information and configure connection pool
  • Configure sqlsessionfactory object
  • Configure and scan Dao interface package to dynamically inject Dao interface into spring container
  • Here, the data acquisition part of Dao layer has been configured. The way to obtain sqlsession is different. There are two ways to obtain sqlsession objects:
  1. Using constructor injection
package com.kuang.dao;

import com.kuang.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements  UserMapper {
	//Sqlsession does not need to be created by ourselves, but managed by spring
	private SqlSessionTemplate sqlSession;
	public void setSqlSession(SqlSessionTemplate sqlSession) {
		this.sqlSession = sqlSession;
	}
	public List selectUser() {
		UserMapper mapper = sqlSession.getMapper(UserMapper.class);
		return mapper.selectUser();
	}
}
  1. Inherit sqlsessiondaosupport and directly use getsqlsession() getMapper(UserMapper.class); obtain

    Reference spring Dao Sqlsessionfactory of XML, proxy generates sqlsession object

package com.kuang.dao;

import com.kuang.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper  {
	public List selectUser() {
		UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
		return mapper.selectUser();
	}
}
  • Now the configuration method is the simplest. There is no need to manually obtain the sqlsession object. Spring dynamically generates and executes SQL statements!

2、 Spring mybatis spring MVC

01 spring MVC mainly manages the controller layer and service layer

  • The controller layer is mainly responsible for deciding what data to get from the Dao layer and what view to render the data to.
  • The service layer is mainly responsible for implementing Dao layer methods, defining a new interface. The methods in this interface come from Dao layer interface methods, and then writing an impl to implement this interface.

spring-mvc. XML integration controller layer

  • Enable annotation support
  • Filter static resources by default
  • Configure viewresolver view resolver
  • Scan the beans of the controller layer to complete the injection

spring-service. XML integration service layer

  • Scan the bean of the service layer to complete the injection
  • The impl implementation method is injected into the IOC container, mainly to assign a value to the parameter private bookmapper bookmapper
  • Configure transaction manager

3、 Spring configuration integration file, ApplicationContext xml


4、 Implementation method of supplementary service layer

package com.kuang.service;
import com.kuang.dao.BookMapper;
import com.kuang.pojo.Books;
import java.util.List;

public class BookServiceImpl implements BookService {
   //Call the operation of Dao layer and set a set interface to facilitate spring management
   private BookMapper bookMapper;
   public void setBookMapper(BookMapper bookMapper) {
      this.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();
   }
}
  • //Call the operation of Dao layer and set a set interface to facilitate spring management
    private BookMapper bookMapper;

In spring service In XML

5、 Method of supplementing controller layer

package com.kuang.controller;

import com.kuang.pojo.Books;
import com.kuang.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.List;

@Controller
@RequestMapping("/book")
public class BookController {
   @Autowired
   @Qualifier("BookServiceImpl")
   private BookService bookService;
   @RequestMapping("/allBook")
   public String list(Model model) {
      List list = bookService.queryAllBook();
      model.addAttribute("list", list);
      return "allBook";
   }

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

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

   @RequestMapping("/del/{bookId}")
   public String deleteBook(@PathVariable("bookId") int id) {
      bookService.deleteBookById(id);
      return "redirect:/book/allBook";
   }
}

6、 Summary

  • mybatis-config.xmlComplete the mapper and mapper of Dao layer XML Mapping (scan the package and configure the mapper)
  • spring-dao.xmlHelp mybatis integrate into spring, mainly complete the acquisition of database connection and sqlsession object, and complete the automatic injection of Dao layer mapper.
  • spring-mvc.xmlComplete the configuration of the controller layer, scan the bean of the controller layer, and complete the view analysis.
  • spring-service.xmlInject the methods of the service layer into the IOC container to complete the configuration transaction management.
  • web.xmlComplete the configuration of dispatcher servlet and character encoding filtering (UTF-8).
  • applicationContext.xmlComplete the integration and introduction of all configuration files.