Spring + spinning MVC + mybatis three framework integration detailed steps (SSM)
Cloud on server 2021-06-27 04:40:12
This example demonstrates how to 0 Start integrating one by one SSM Steps for , To learn this knowledge , Need to have Spring, SpringMVC, Mybatis  The basis of , Without these foundations , Please master the basics before you learn , Don't jump to learn , More haste, less speed. . 


Required reading :  To run a framework based program successfully , about JAR The version of the package , There are strict requirements for the correctness of configuration files , Anything goes wrong , Will cause the framework program to fail . If you're learning this framework for the first time , Be sure to follow the instructions of the tutorial , Fully mimic operation , Until we see the running effect . After the first success , confidence , There will be a better way of thinking , And then according to my doubts , stay “ success ” Make the changes and adjustments that you originally wanted to make to the code , This can greatly save the time of study , Increase of efficiency , Don't change it without permission , Create barriers to your own learning

step 1:IDEA
step 2: database
step 3: Create table
step 4: Prepare the data
step 5:JDK edition
step 6: First run , To learn
step 7: New projects
step 8: Import jar package
step 9:pojo
step 10:CategoryMapper
step 11:Category.xml
step 12:CategoryService
step 13:CategoryServiceImpl
step 14:CategoryController
step 15:web.xml
step 16:applicationContext.xml
step 17:springMVC.xml
step 18:listCategory.jsp
step 19: Deployed in tomcat in , Restart test
step 20: Train of thought
step 21: Delete , Start from scratch , Do it all yourself this time

step  1 : IDEA

This tutorial is about using Eclipse To make the , If it is to use IDEA, Please jump to  IDEA How to create SSM project . But jump to the past tutorial , There is no current for each class , The configuration file ,JSP My explanation is so detailed , So it's best to use Eclipse Roll over the current tutorial , After understanding , Reuse IDEA course

step  2 :  database

First, prepare the database how2java
If the database is not installed , Please refer to   install mysql-server
notes :  The password of the newly installed database account is root:admin, In the subsequent configuration , Also use this account password . If the password is not this , This knowledge point download area ( Click to enter ) You can't run your runnable projects , So try to change the password to admin, How to change the password : modify root password

create database how2java;

step  3 :  Create table

Then prepare the table Category Only 2 A field id and name

use how2java;

CREATE TABLE category_ (

  id int(11) NOT NULL AUTO_INCREMENT,

  name varchar(30) ,

  PRIMARY KEY (id)

) DEFAULT CHARSET=UTF8;

step  4 :  Prepare the data

use how2java;

insert into category_ values(null,"category1");

insert into category_ values(null,"category2");

insert into category_ values(null,"category3");

insert into category_ values(null,"category4");

insert into category_ values(null,"category5");

select * from category_

step  5 : JDK edition

This tutorial is through JDK8 Compilation of , If you have a lower version of the running environment on your machine JDK, The classes in it will not be recognized , Otherwise, there will be an error as shown in the figure . 
Please use JDK8 , Please refer to   testing JDK edition , And download and configure
Do not use JDK9 Or higher , There are compatibility risks


JDK edition

step  6 :  First run , To learn

SSM Integration takes a lot of steps , Any step is missing , Do wrong , It's all possible to fail , This will affect the confidence in learning , And mistakenly think that this tutorial is not going to work .
So download the download area first ( Click to enter ) The runnable project of ssm.rar, Unzip and import to eclipse in , start-up Tomcat, Observe the normal operation . Make sure it works , Make sure the tutorial can run , Learn more about the following .
Import to Eclipse Please refer to : Import dynamic Web The project to Eclipse in
After successful deployment , Address of the test

http://127.0.0.1:8080/ssm/listCategory


 First run , To learn

step  7 :  New projects

stay eclipse Medium new project ssm, Use dynamic web project The way . Students who are not familiar with this way , Please refer to   Use Dynamic Web Project Way of development J2EE application


 New projects

step  8 :  Import jar package

Download area ( Click to enter ) Of lib.rar, Unzip and copy to e:/project/ssm/WebContent/WEB-INF/lib Under the table of contents

step  9 : pojo

package com.how2java.pojo;

public class Category {

    private int id;

    private String name;

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String toString() {

        return "Category [id=" + id + ", name=" + name + "]";

    }

}

step  10 : CategoryMapper

package com.how2java.mapper;

import java.util.List;

import com.how2java.pojo.Category;

public interface CategoryMapper {

    public int add(Category category); 

    public void delete(int id); 

    public Category get(int id); 

    public int update(Category category);  

    public List

    public int count(); 

}

step  11 : Category.xml

Category.xml Need and CategoryMapper Under the same bag , also namespace Must write CategoryMapper The complete class name of

<!DOCTYPE mapper

    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    

        

            insert into category_ ( name ) values (#{name})   

        

        

            delete from category_ where id= #{id}  

        

        

            select * from   category_  where id= #{id}   

        

        

            update category_ set name=#{name} where id=#{id}   

        

        

            select * from   category_     

            

    

step  12 : CategoryService

package com.how2java.service;

import java.util.List;

import com.how2java.pojo.Category;

public interface CategoryService {

    List

}

step  13 : CategoryServiceImpl

CategoryServiceImpl Be annotated @Service Marked as a Service
And assembled categoryMapper

package com.how2java.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.how2java.mapper.CategoryMapper;

import com.how2java.pojo.Category;

import com.how2java.service.CategoryService;

@Service

public class CategoryServiceImpl  implements CategoryService{

    @Autowired

    CategoryMapper categoryMapper;

    public List

        return categoryMapper.list();

    }

}

step  14 : CategoryController

CategoryController By @Controller It's for the controller
Automatically assembled categoryService
adopt @RequestMapping Mapping access paths /listCategory Path to method listCategory().
stay listCategory() In the method , adopt categoryService After acquisition , Then store it in "cs" This key On .

package com.how2java.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.how2java.pojo.Category;

import com.how2java.service.CategoryService;

// tell spring mvc This is a controller class

@Controller

@RequestMapping("")

public class CategoryController {

    @Autowired

    CategoryService categoryService;

    @RequestMapping("listCategory")

    public ModelAndView listCategory(){

        ModelAndView mav = new ModelAndView();

        List

        // Put forward parameters

        mav.addObject("cs", cs);

        // Put in jsp route

        mav.setViewName("listCategory");

        return mav;

    }

}

step  15 : web.xml

stay WEB-INF Add... To the list web.xml, This web.xml It does two things :
1. adopt ContextLoaderListener stay web app When it starts , obtain contextConfigLocation The filename of the configuration file applicationContext.xml, And carry on Spring Related initialization work
2. Have any visits , All be DispatcherServlet Intercepted , This is it. Spring MVC That working mechanism .

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns="http://java.sun.com/xml/ns/javaee"

         xmlns:web="http://java.sun.com/xml/ns/javaee"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    

    

        

        

    

    

        

    

    

    

        

        

        

        

            

            

        

        

    

    

        

        

    


step  16 : applicationContext.xml

stay src New under the directory applicationContext.xml file , This is a Spring Configuration file for , Its role
1. Through annotation , take Service The life cycle of Spring Management of


2. Configure data sources


3. Scanning storage SQL Of the statement Category.xml


4. scanning Mapper, And incorporate its life cycle into Spring Management of

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   

    

    

      

           

       

      

           

       

      

           

       

      

           

          

    

    

        

        

        

    

    

        

    


step  17 : springMVC.xml

stay src New under the directory springMVC.xml
1. scanning Controller, And incorporate its life cycle into Spring management

      <context:include-filter type="annotation"

      expression="org.springframework.stereotype.Controller"/>



2. Annotation driven , So that the matching of access path and method can be configured through annotation


3. Static page , Such as html,css,js,images You can visit


4. View to /WEB/INF/jsp Under this catalog

        <property name="viewClass"

            value="org.springframework.web.servlet.view.JstlView" />

        

        

    

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    

    

          <context:include-filter type="annotation"

          expression="org.springframework.stereotype.Controller"/>

    

    

    

    

    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="viewClass"

            value="org.springframework.web.servlet.view.JstlView" />

        

        

    


step  18 : listCategory.jsp

stay WEB-INF Create jsp Catalog , And create files listCategory.jsp.
In this jsp In file , adopt forEach label , Traverse CategoryController The aggregate data passed through .

        

        

    

    

        

            

            

        

    


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

    pageEncoding="UTF-8" import="java.util.*"%>

    


id name
${c.id} ${c.name}

step  19 :  Deployed in tomcat in , Restart test

Deployed in Tomcat in , restart tomcat, Then visit the address , Observe the effect

http://127.0.0.1:8080/ssm/listCategory


Please refer to   adopt Eclipse start-up Tomcat-Run On Server

step  20 :  Train of thought

1. First, access the path on the browser /listCategory
2. tomcat according to web.xml Configuration information on , It's intercepted /listCategory, And leave it to DispatcherServlet Handle .
3. DispatcherServlet according to springMVC Configuration of , Leave this request to CategoryController Class for processing , So we need to instantiate this class
4. In instantiation CategoryController When , Inject CategoryServiceImpl. ( Automatic assembly realizes CategoryService Instance of interface , Only CategoryServiceImpl Realized CategoryService Interface , So it's going to inject CategoryServiceImpl)
5. In instantiation CategoryServiceImpl When , Again CategoryMapper
6. according to ApplicationContext.xml Configuration information in , take CategoryMapper and Category.xml It's connected .
7. So you get the instantiated CategoryController, And call list Method
8. stay list In the method , visit CategoryService, And get the data , And put the data in "cs" On , Then the server jumps to listCategory.jsp Go to
9. Last in listCategory.jsp Data displayed in


 Train of thought

step  21 :  Delete , Start from scratch , Do it all yourself this time

If you are using the download area ( Click to enter ) Download run project execution , So at this point , It's time to delete this item , And then do it all by yourself , Turn this knowledge into your own .

Please bring the original link to reprint ,thank
Similar articles

2021-06-04

2021-06-04

2021-06-06

2021-06-27

2021-06-29