java--SSM框架整合简单实例

40 篇文章 0 订阅
32 篇文章 0 订阅

SSM框架简介(Spring、SpringMVC和Mybatis)

SSM框架Spring+SpringMVC+MyBatis的缩写,这是继SSH之后,主流的Java EE企业级框架,它适用于搭建各种大型的企业级应用系统。
Spring是一个轻量级的Java开发框架,它是为了解决企业应用开发的复杂性而创建的,从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中收益。
Spring MVC是一个典型的教科书式框架,它易于同其它View框架无缝集成,功能强大,且简单易学。
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。


web.xml 配置:
<?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.xml文件配置:

<?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>

service层:

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);
    }
}

这里需要注意一点:如果有报错的信息就是因为在接口那里忘记了注释!
在这里插入图片描述
控制器层:

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

jsp页面:

<%--
  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>

结果是:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值