inotgo.com

Download area
File name File size
lib.rar 10m
ssm.rar 10m

Tool version compatibility problem
This example demonstrates from 0 Start integrating one by one SSM Steps for , To learn this knowledge , Need to have Spring, SpringMVC, Mybatis Basis of , Without these foundations , Please master the basics before you learn , Don't jump to learn , More haste, less speed. .

Must read : Framework based programs run successfully , For JAR Package version , The correctness of the configuration file has strict requirements , Anything goes wrong , Will cause the framework program to fail . If you are learning this framework for the first time , Be sure to follow the instructions of the tutorial strictly , Completely imitate the operation , Until you successfully see the running effect . After the first success , Confidence , Ideas will have a better cushion , Then according to their doubts , stay “ success ” Make the desired changes and adjustments to your code , This can greatly save learning time , increase of efficiency , Don't change it at once , Create obstacles to your learning
Correct

Step 1 : IDEA   
Step 2 : database   
Step 3 : Create table   
Step 4 : Prepare data   
Step 5 : JDK edition   
Step 6 : Run... First , Learn again   
Step 7 : New project   
Step 8 : Import jar Bag   
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 diagram   
Step 21 : Delete , Start from scratch , Do it all yourself this time   

This tutorial uses Eclipse Made , If you use IDEA, Please jump to IDEA How to create SSM project . But jump to the tutorial after the past , There is no current for each class , Configuration file ,JSP The explanation is so detailed , So it's best to use Eclipse Roll up the current tutorial , After understanding , Reuse IDEA course
First prepare the database how2java
If the database is not installed , Please refer to Install mysql-server

notes : The newly installed database account password is root:admin, In the subsequent configuration , I also use this account password . If the password is not this , The runnable items in the upper right corner of this knowledge point can't run , So try to change the password to admin, Password modification method : modify root password
create database how2java;
create database how2java;
Then prepare the table Category only 2 Fields id and name
use how2java; CREATE TABLE category_ ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(30) , PRIMARY KEY (id) ) DEFAULT CHARSET=UTF8;
use how2java;
 
CREATE TABLE category_ (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(30) ,
  PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
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_
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_
This tutorial is through JDK8 Compiled , If the environment running on your machine is a lower version JDK, Will not recognize the class , Otherwise, an error will appear as shown in the figure .
Please use JDK8 , Please refer to testing JDK edition , And download and configure

Do not use JDK9 Or later , Compatibility risk
JDK edition
SSM Integration requires many steps , Any step is missing , Did something wrong , Can fail , This will affect the confidence of learning , And mistakenly think that this tutorial is impassable .

So first download the runnable project in the upper right corner ssm.rar, Unzip and import to eclipse In , Start Tomcat, Observe for normal operation . Make sure you can run , Make sure the tutorial can run , Learn the following .

Import to Eclipse For the method of and operation in, please refer to : Import dynamic Web Project to Eclipse In


After successful deployment , Test address

http://127.0.0.1:8080/ssm/listCategory
 Run... First , Learn again
stay eclipse New item in ssm, use dynamic web project The way . Students who are not familiar with this way , Please refer to use Dynamic Web Project Develop in a way J2EE application
 New project
Download the... In the upper right corner lib.rar, Unzip and copy to e:/project/ssm/WebContent/WEB-INF/lib Under the directory
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 + "]"; } }
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<Category> list(); public int count(); }
Category.xml Need and CategoryMapper Put it under the same bag , And namespace Must write CategoryMapper Full class name for
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.how2java.mapper.CategoryMapper"> <insert id="add" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert> <delete id="delete" parameterType="Category" > delete from category_ where id= #{id} </delete> <select id="get" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="update" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update> <select id="list" resultType="Category"> select * from category_ </select> </mapper>
package com.how2java.service; import java.util.List; import com.how2java.pojo.Category; public interface CategoryService { List<Category> list(); }
package com.how2java.service;

import java.util.List;

import com.how2java.pojo.Category;

public interface CategoryService {

	List<Category> list();

}
CategoryServiceImpl Annotated @Service Mark 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<Category> list(){ return categoryMapper.list(); } }
CategoryController cover @Controller Marked as controller
Automatic assembly categoryService
Through @RequestMapping Map access path /listCategory Path to method listCategory().
stay listCategory() In the method , Through categoryService After obtaining , Then store it in "cs" this key Go ahead .
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<Category> cs= categoryService.list(); // Put forward parameters mav.addObject("cs", cs); // Put in jsp route mav.setViewName("listCategory"); return mav; } }
stay WEB-INF New addition under directory web.xml, this web.xml It has two functions :
1. Through ContextLoaderListener stay web app When starting , obtain contextConfigLocation The file name of the configuration file applicationContext.xml, And Spring Related initialization work

2. Any access , All by DispatcherServlet Intercepted , This is it. Spring MVC That working mechanism .
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- spring Configuration file for --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring mvc Core : Distribute servlet --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- spring mvc Configuration file for --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
stay src New under directory applicationContext.xml File , This is Spring Configuration file for , Its function
1. By annotation , Will Service Life cycle inclusion Spring Management of

<context:annotation-config />
<context:component-scan base-package="com.how2java.service" />


2. Configure data sources

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">


3. Scan and store SQL Of the statement Category.xml

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">


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

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<?xml version="1.0" encoding="UTF-8"?> <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"> <context:annotation-config /> <context:component-scan base-package="com.how2java.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>admin</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.how2java.pojo" /> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.how2java.mapper"/> </bean> </beans>
stay src New under directory springMVC.xml

1. Scan Controller, And incorporate its life cycle into Spring Administration

<context:annotation-config/>
<context:component-scan base-package="com.how2java.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


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

<mvc:annotation-driven />


3. Static pages , as html,css,js,images You can access

<mvc:default-servlet-handler />


4. View navigate to /WEB/INF/jsp Under this directory

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<?xml version="1.0" encoding="UTF-8"?> <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:annotation-config/> <context:component-scan base-package="com.how2java.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <!-- View positioning --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
stay WEB-INF Create... Under jsp catalogue , And create a file listCategory.jsp.
In this jsp In the file , Through forEach label , Traverse CategoryController The collection data passed .
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <table align='center' border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> </tr> <c:forEach items="${cs}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> </tr> </c:forEach> </table>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
<table align='center' border='1' cellspacing='0'>
    <tr>
        <td>id</td>
        <td>name</td>
    </tr>
    <c:forEach items="${cs}" var="c" varStatus="st">
        <tr>
            <td>${c.id}</td>
            <td>${c.name}</td>
              
        </tr>
    </c:forEach>
</table>
Step 19 :

Deployed in tomcat In , Restart test

edit top fracture
Deployed in Tomcat In , Restart tomcat, Then visit the address , Observation effect

http://127.0.0.1:8080/ssm/listCategory


For deployment methods, please refer to Through Eclipse Start Tomcat-Run On Server
1. First, access the path on the browser /listCategory
2. tomcat According to web.xml Configuration information on , Intercepted /listCategory, And hand it over to DispatcherServlet handle .
3. DispatcherServlet According to springMVC Configuration of , Submit this request to CategoryController Class , So you need to instantiate this class
4. When instantiating CategoryController When I was , injection CategoryServiceImpl. ( Automatic assembly is realized CategoryService Instance of interface , only CategoryServiceImpl Realized CategoryService Interface , So it will inject CategoryServiceImpl)
5. When instantiating CategoryServiceImpl When I was , Inject... Again CategoryMapper
6. According to ApplicationContext.xml Configuration information in , Will CategoryMapper and Category.xml 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" Go ahead , Then the server jumps to listCategory.jsp Go
9. Finally in listCategory.jsp Display data in
 Train of thought diagram
Step 21 :

Delete , Start from scratch , Do it all yourself this time

edit top fracture
If it is executed by using the run project downloaded in the upper right corner , So this time , It's time to delete this item , Then do it yourself from beginning to end , Turn this knowledge into your own thing .


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2021-10-16 Oh, my God ,404 Dying
fql




According to the webmaster's ,404, Dying
 Loading
 
               
 
               


1 One answer

fql
Answer time :2021-10-16
sorry , You need to reopen the page in the browser :http://127.0.0.1:8080/ssm/listCategory instead of http://127.0.0.1:8080/ssm/



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2021-10-15 CategoryService cs then cs.list() Why can I call Isn't he an interface
mumu123123

rt







The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2021-07-02 ModelAndView mav = new ModelAndView(); What is this in the control layer
2021-04-17 Directly use the webmaster's code ,404 problem
2021-04-03 404 error , Downloaded from the webmaster can run , Do it yourself , Nothing has changed , appear 404


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 184 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about JAVA Frame -SSM- Integration steps Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot