惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
罗磊的独立博客
博客园 - 【当耐特】
D
Docker
Y
Y Combinator Blog
L
LangChain Blog
博客园 - 三生石上(FineUI控件)
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
B
Blog
The GitHub Blog
The GitHub Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
爱范儿
爱范儿
A
About on SuperTechFans
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 编程我的一切

避免大规模日志告警滞后的三种MySQL深度优化实战方案 如何平衡复杂度与扩展性:从过度设计到演进式架构的设计思考 从零开始:程序员快速上手大模型开发——从 Prompt Engineering 到 RAG 架构落地 深度解析:如何利用开源项目贡献构建高质量的个人技术品牌 打破成长瓶颈:程序员构建深度知识体系与高效学习的方法论 从慢查询到高并发:MySQL 索引优化与后端架构性能调优实战指南 从慢查询到高并发:MySQL 数据库性能调优的深度实践与核心策略 从零开始构建你的第一个 AI 应用:大模型 API 调用与 Prompt Engineering 实战指南 大模型时代下的开发者转型:从理论理解到 RAG 实战落地的进阶路径 从零开始构建你的 AI 助手:大模型本地部署与 Prompt 工程实战入门指南 从内存分配到零拷贝:深度解析 .NET 中 Span<T> 与 Memory<T> 的高性能实践 拒绝代码腐烂:深度解析代码整洁之道与高频重构模式 从“能跑就行”到“优雅之美”:深度解析代码整洁之道与重构实战技巧 Apache Hop实战:Windows平台MySQL数据迁移的深度排错与性能调优 线程与进程的区别与联系:操作系统入门详解(含 Python 示例) 打破同源枷锁:深入理解 postMessage 跨域通信机制 三大搜索引擎 URL 推送 API 详解:百度、必应、谷歌 PandasAI:当数据分析遇上自然语言处理 Windows 左ctrl和左alt键互换 Bootstrap下拉菜单、按钮式下拉菜单 SSM三大框架的运行流程、原理、核心技术详解 app启动速度怎么提升? canvas画布基本知识点总结 Lambda入门 Spring boot+CXF开发WebService Demo HTML5中的Web Notification桌面通知 Open3d之交互式可视化 行为识别TSM训练ucf101数据集 Python3列表、元组及之间的区别和转换 Java 字符串简介
SSM框架整合(Spring + SpringMVC + MyBatis)
编程我的一切 · 2021-02-10 · via 博客园 - 编程我的一切

搭建环境

使用Spring(业务层)整合其他的框架SpringMVC(表现层)和MyBatis(持久层)

Spring框架

  1. 创建数据库表

    CREATE DATABASE ssm;
    USE ssm;
    CREATE TABLE account(
    	id INT PRIMARY KEY AUTO_INCREMENT,
    	NAME VARCHAR(20),
    	money DOUBLE
    );
    
  2. pom文件导入坐标

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.0.2.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <mysql.version>5.1.6</mysql.version>
        <mybatis.version>3.4.5</mybatis.version>
      </properties>
    
      <dependencies>
        
  3. 实体类Account

    public class Account implements Serializable {
    
        private Integer id;
        private String name;
        private Double money;
    }
    
  4. AccountDao接口(Spring框架)

    public interface AccountDao {
    
        
  5. AccountService接口

    public interface AccountService {
    
        
  6. AccountServiceImpl实现类

    @Service("accountService")
    public class AccountServiceImpl implements AccountService {
        @Override
        public List<Account> findAll() {
            System.out.println("业务层,查询所有账户");
            return null;
        }
    
        @Override
        public void saveAccount(Account account) {
            System.out.println("业务层,保存账户信息");
        }
    }
    
  7. 表现层AccountController

    @Controller
    @RequestMapping(value = "/account")
    public class AccountController {
    }
    
  8. 配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.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
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        

SpringMVC框架搭建

  1. web.xml
1. <web-app>
     <display-name>Archetype Created Web Application</display-name>

  
  1. springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.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">
    
        
  2. AccountController

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.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">
    
        <!--开启注解扫描,只扫描Controller注解-->
        <context:component-scan base-package="com.atgw">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <!--配置的视图解析器对象-->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--过滤静态资源-->
        <mvc:resources mapping="/css/**" location="/css/"/>
        <mvc:resources mapping="/images/**" location="/images/"/>
        <mvc:resources mapping="/js/**" location="/js/"/>
    
        <!--开启SpringMVC注解的支持-->
        <mvc:annotation-driven/>
    </beans>
    
  3. index.jsp

        <a href="account/findAll">测试</a>
    
  4. list.jsp

          <h3>查询所有账户</h3>
    

Spring整合SpringMVC框架

在这里插入图片描述

1web.xml

  <!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoader</listener-class>
  </listener>
  

2AccountController)(方法中自动注入service)

@Controller
@RequestMapping(value = "/account")
public class AccountController {

    

Spring整合MyBatis框架

  1. web.xml配置Sping监听器,在服务器启动时加载applicationContext.xml配置文件

      <!--配置Spring的监听器,默认只加载WEB-INF目录下的application.xml配置文件-->
      <listener>
          <listener-class>org.springframework.web.context.ContextLoader</listener-class>
      </listener>
      
  2. SqlMapConfig.xml配置数据库环境,引入映射类的地址

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
        
  3. AccountDao接口,添加注释,写SQL语句

    public interface AccountDao {
    
        
  4. 测试

    //查询所有数据    
    @Test
        public void run1() throws Exception {
            

  5. 希望把dao对象代理到IOC容器中

    SQLSessionFactory工厂可以创建SQLSession,然后创建dao代理对象,代理到IOC容器中

        <!--Spring整合MyBatis框架-->
        <!--配置连接池-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"/>
            <property name="user" value="root"/>
            <property name="password" value="000519"/>
        </bean>
    
        
  6. AccountDao接口中添加@Repository注解

    @Repository//交给IOC容器
    public interface AccountDao {
    }
    
  7. AccountController中的方法中对遍历到数据的进行页面遍历

        @RequestMapping("/findAll")
        public String findAll(Model model){
            System.out.println("表现层:查询所有账户");
            
  8. list.jsp页面(遍历数据)

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h3>查询所有账户</h3>
        <c:forEach items="${list}" var="account">
            ${account.name}<br/>
        </c:forEach>
    </body>
    </html>
    

  9. 配置事务

    applicationContext.xml

        <!--配置Spring框架声明式事务管理-->
        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        
  10. AccountController

        @RequestMapping("/save")
        public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException {
            accountService.saveAccount(account);
           
  11. index.jsp

        <form action="account/save" method="post">
            姓名:<input type="text" name="name" /><br/>
            金额:<input type="text" name="money" /><br/>
            <input type="submit" value="保存"/><br/>
        </form>