An example of rapid integration of SSM framework — student query


1、 Quick preparation

SSM framework includes spring framework, spring MVC framework and mybatis framework. I have written several articles to introduce the basis and entry procedures of these frameworks. For students who are not familiar with these three frameworks, please refer to the following articles:

Spring series of [SSH framework] (I)

Annotation management of bean in spring framework series (2)

AOP idea of spring framework series

JDBC integration of spring framework series

Transaction management of spring framework series

Introduction and examples of spring MVC framework series

Component overview and configuration details of spring MVC framework series

Basic knowledge of mybatis framework

Quick start program of mybatis framework

If you have learned the relevant knowledge of SSM framework in succession, you can ignore this part and look directly at the following content.

2、 Quick create project

Since the jar package depends on the convenience of management, we use Maven for project management and development. In this step, we use idea to quickly create a maven project. We won’t go into too much detail here about how to use idea to quickly create a maven project. You can refer to the following article:

Maven project management tool Foundation Series (I)

3、 Quickly configure jar package dependencies

After the Maven project is created, quickly open and configure itpom.xmlFile, the specific configuration is as follows:

<project xmlns="" xmlns:xsi=""
  <name>SSMDemo Maven Webapp</name>

        <!--  Configuring spring MVC dependency packages -- >

        <!--  Spring JDBC dependency package -- >

        <!--  Spring AOP dependency package -- >

        <!-- Mybatis dependency package -- >
        <!--  Spring integrates mybatis dependency package -- >

        <!--  MySQL driver dependency package -- >
        <!--  C3p0 data source dependency package -- >
        <!--  JSTL dependency package -- >

        <!--  Servlet API dependency package -- >

        <!--  JUnit test dependency package -- >


    <!--  If you do not add this node, mybatis's mapper XML files will be missed -- >


4、 Quickly configure coding filtering and resource loading

openweb.xmlFile, quick configuration enables spring, spring MVC coding filtering and static resource loading. The specific configuration code is as follows:

  <display-name>Archetype Created Web Application</display-name>
  <!--  Launch spring's container -- >
  <!--  The front-end controller of spring MVC intercepts all requests -- >
  <!--  Character encoding filters must be placed before all filters -- >
  <!--  Load static resources -- >

5、 Quickly configure spring configuration files

stayresourcesNew under folderapplicationContext.xmlFile to configure mybatis and database related information. The specific code configuration is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <!--  Load resource file -- >
    <context:property-placeholder location=""/>
    <!--  Configure c3p0 data source -- >
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="initialPoolSize" value="5"></property>
        <property name="maxPoolSize" value="10"></property>
    <!--  Configure mybatis sqlsessionfactory -- >
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--  Specify mybatis data source -- >
        <property name="dataSource" ref="dataSource"/>
        <!--  Specify mybatis mapper mapping file location -- >
        <property name="mapperLocations" value="classpath:com/ssm/example/dao/*.xml"/>
        <!--  Specify the location of the mybatis global configuration file -- >
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    <!--  Scan mapper interface of mybatis -- >
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- Scan all Dao interfaces and add them to IOC container -- >
        <property name="basePackage" value="com.ssm.example.dao"/>
    <!--  Configure transaction manager -- >
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--  Specify data source -- >
        <property name="dataSource" ref="dataSource"></property>
    <!--  Configure transaction enhancements -- >
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <!--  Configure that all methods are transaction methods -- >
            <tx:method name="*"/>
            <tx:method name="get*" read-only="true"/>
    <!--  Enable annotation based transactions -- >
        <!--  Pointcut expression -- >
        <aop:pointcut expression="execution(* com.ssm.example.service.impl.*.*(..))" id="txPoint"/>
        <!--  Configure transaction enhancements -- >
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>

6、 Quick configuration database connection information

stayresourcesNew under folderdb.propertiesFile to configure database connection related information. The specific code configuration is as follows:


7、 Quick configuration database operation auxiliary information

stayresourcesNew under foldermybatis-config.xmlFile to configure relevant database operation auxiliary information. The specific code configuration is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-// Config 3.0//EN" "">
        <!--  Print SQL -- >
        <setting name="logImpl" value="STDOUT_LOGGING" />
        <!--  Specify a package name, and mybatis will search for the required JavaBean -- > under the package name
        <package name="com.ssm.example.entity"/>

8、 Quickly configure spring MVC annotation scanning and view parser

stayresourcesNew under folderspringmvc.xmlFile to configure relevant database operation auxiliary information. The specific code configuration is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <!--  Enable spring MVC annotation driven -- >
    <mvc:annotation-driven />
    <!--  Scan business code -- >
    <context:component-scan base-package="com.ssm.example"></context:component-scan>
    <!--  Configure view parser -- >
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>

9、 Quick new data table

Create a new MySQL database and quickly create a new data tablessm_exampleThe specific table building codes are as follows:

CREATE TABLE `student`  (
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gender` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `email` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `tel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `cla` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

Insert into ` student ` values (1, 'Kong Yiji', 'male', ' [email protected] ',' 13509856897 ',' computer class 1 ');
Insert into ` student ` values (2, 'a Qiang', 'female', ' [email protected] ',' 12345678909 ',' software engineering ');
Insert into ` student ` values (3, 'Ah Fu', 'male', ' [email protected] ',' 13657898762 ',' Mathematics');
Insert into ` student ` values (4, 'a Xia', 'female', ' [email protected] ',' 12378645987 ',' English major ');
Insert into ` student ` values (5, 'Guide', 'male', ' [email protected] ',' 13587690873 ',' miscellaneous brick handling specialty ');


10、 Quickly create a new entity class

Quickly create a new entity, the specific code is as follows:

package com.ssm.example.entity;

public class Student {
    private int id;
    private String name;
    private String gender;
    private String email;
    private String tel;
    private String cla;
    public int getId() {
        return id;
    public void setId(int id) { = id;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getGender() {
        return gender;
    public void setGender(String gender) {
        this.gender = gender;
    public String getEmail() {
        return email;
    public void setEmail(String email) { = email;
    public String getTel() {
        return tel;
    public void setTel(String tel) { = tel;
    public String getCla() {
        return cla;
    public void setCla(String cla) {
        this.cla = cla;

11、 Write business code quickly

Quick newStuentController.javaControl class, the specific code is as follows:

package com.ssm.example.controller;

import java.util.List;

import com.ssm.example.entity.Student;
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.ssm.example.service.StudentService;

public class StudentController {

    private StudentService studentService;
     *Find all students
     * @return
    public ModelAndView test(){
        List<Student> list = studentService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list", list);
        return modelAndView;

Quick newStudentService.javaThe interface code is as follows:

package com.ssm.example.service;

import java.util.List;

import com.ssm.example.entity.Student;

public interface StudentService {
    public List<Student> findAll();

Quick newStudentServiceImpl.javaClass implements the interface, and the specific code is as follows:

package com.ssm.example.service.impl;

import java.util.List;

import com.ssm.example.dao.StudentDAO;
import com.ssm.example.entity.Student;
import com.ssm.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class StudentServiceImpl implements StudentService {

    private StudentDAO studentDAO;
    public List<Student> findAll() {
        // TODO Auto-generated method stub
        return studentDAO.findAll();

Quickly create a new Dao, the specific code is as follows:

package com.ssm.example.dao;

import java.util.List;

import com.ssm.example.entity.Student;

public interface StudentDAO {
    public List<Student> findAll();

Quickly create a new Dao interface proxy fileStudentDAO.xml, the specific code is as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN" ""> 

    <mapper namespace="com.ssm.example.dao.StudentDAO">

        <resultMap type="Student" id="studentMap">
            <id property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="gender" column="gender"/>
            <result property="email" column="email"/>
            <result property="tel" column="tel"/>
            <result property="cla" column="cla"/>

        <select id="findAll" resultMap="studentMap">
            select * from student


12、 New access page

Quick new access pageindex.jspAnd the page uses the bootstrap framework for light rendering. The specific code is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@ page isELIgnored="false" %>
<%@taglib uri="" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
< title > student list < / Title >
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <div class="container" align="center">
        <div class="row">
            <div class="col-md-12">
                <h1>An example of rapid integration of SSM framework -- student query</h1>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-hover" id="emps_table">
                                <input type="checkbox" id="check_all"/>
                            < th > No. < / th >
                            < th > name < / th >
                            < th > gender < / th >
                            < th > email < / th >
                            < th > contact number < / th >
                            < th > class < / th >
                            < th > operation < / th >
                        <c:forEach items="${list }" var="student">
                                <td><input type='checkbox' class='check_item'/></td>
                                <td>${ }</td>
                                <td>${ }</td>
                                <td>${student.gender }</td>
                                <td>${ }</td>
                                <td>${ }</td>
                                <td>${student.cla }</td>

                                    <button class="btn btn-primary btn-sm edit_btn">
                                        < span class = "glyphicon pencil" > Edit</span>
                                    <button class="btn btn-danger btn-sm delete_btn">
                                        < span class = "glyphicon glyphicon Dash" > delete</span>

13、 Test quickly

Here, the SSM framework integration program has been written. Deploy and start the Tomcat server, and then test it in the browser address bar. The results are as follows:

An example of rapid integration of SSM framework -- student query

Project source code address: click here to get the source code directly

Pay attention to WeChat official account compassblog, learn more