Simple example of java--SSM framework integration

Introduction to SSM framework (Spring, SpringMVC and Mybatis)

The abbreviation of SSM framework Spring+SpringMVC+MyBatis, which is the mainstream Java EE enterprise-level framework after SSH, is suitable for building various large-scale enterprise-level application systems.
Spring is a lightweight Java development framework, which was created to solve the complexity of enterprise application development. From the perspective of simplicity, testability and loose coupling, any Java application can benefit from Spring.
Spring MVC is a typical textbook framework, which is easy to seamlessly integrate with other View frameworks, powerful, and easy to learn.
MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets.


web.xml Configuration:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:springmybatis.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!--过滤器设置编码格式-->
    <filter>
        <filter-name>characterencoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>Encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterencoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

springmybatis.xmlFile configuration:

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd

">

    <!--自动扫描上下文-->
    <context:component-scan base-package="cn.zbw.*"></context:component-scan>

    <!--视图层-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/school?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <!--mybatis连接工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="cn.zbw.entity"></property>
        <property name="mapperLocations" value="classpath*:cn/zbw/dao/*.xml"></property>
    </bean>

    <!--mybatis映射输入数据-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.zbw.dao"></property>
    </bean>

    <!--事务管理-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:annotation-driven></tx:annotation-driven>
</beans>

servicelayer:

package cn.zbw.service;

import cn.zbw.dao.StudentInfoMapper;
import cn.zbw.entity.StudentInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class StudentService {
    
    
    @Resource
    private StudentInfoMapper studentInfoMapper;

    public StudentInfoMapper getStudentInfoMapper() {
    
    
        return studentInfoMapper;
    }

    public void setStudentInfoMapper(StudentInfoMapper studentInfoMapper) {
    
    
        this.studentInfoMapper = studentInfoMapper;
    }
    public List<StudentInfo> findStudentByArray(int[] array){
    
    
        return studentInfoMapper.findStudentByArray(array);
    }
}

One thing to note here: 如果有报错的信息就是因为在接口那里忘记了注释!
insert image description here
the controller layer:

package cn.zbw.controller;

import cn.zbw.entity.StudentInfo;
import cn.zbw.service.StudentService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Scope("prototype")
public class StudentController {
    
    
    @Resource
    private StudentService studentService;

    public StudentService getStudentService() {
    
    
        return studentService;
    }

    public void setStudentService(StudentService studentService) {
    
    
        this.studentService = studentService;
    }

    @RequestMapping("/findall.do")
    public String findAllByArray(Model model){
    
    
        int[] array={
    
    2};
        List<StudentInfo> stulist = studentService.findStudentByArray(array);
        model.addAttribute("stulist",stulist);
//        System.out.println(stulist);
        return "show";
    }
}

jsppage:

<%--
  Created by IntelliJ IDEA.
  User: 冰冰椰果
  Date: 2021/03/22
  Time: 上午 11:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table align="center" border="1px">
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>地址</td>
        <td>班级编号</td>
        <td>年龄</td>
    </tr>
    <c:forEach items="${stulist}" var="s">
        <tr>
            <td>${
    
    s.studentId}</td>
            <td>${
    
    s.studentName}</td>
            <td>${
    
    s.studentSex}</td>
            <td>${
    
    s.studentAddress}</td>
            <td>${
    
    s.classId}</td>
            <td>${
    
    s.studentAge}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

turn out:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45686583/article/details/115081755