当前位置: 首页 > news >正文

[java][SSM]整合Mybatis3、Spring4 和 SpringMVC4 的步骤

整合Mybatis3、Spring4 和 SpringMVC4 的步骤可以概括为以下几点:

1. **创建Web项目**:在IDE中创建一个新的Web项目。

2. **导入依赖**:将所需的jar包导入到项目中,包括Mybatis3、Spring4、SpringMVC4以及数据库驱动(如mysql)。

3. **创建配置文件**:为Mybatis3、Spring4、SpringMVC4和数据库连接创建相应的配置文件。

4. **配置web.xml**:在web.xml中配置Spring的监听器、Mybatis的SqlSessionFactory、SpringMVC的DispatcherServlet等。

5. **配置数据源**:在datasource.properties文件中配置数据库连接信息。

6. **配置Mybatis**:在mybatis.cfg.xml中配置Mybatis的别名、类型处理器等。

7. **配置Spring MVC**:在mvc-servlet.xml中配置Spring MVC的视图解析器、静态资源处理等。

8. **配置Spring应用上下文**:在applicationContext.xml中配置Spring的事务管理、数据源、Mapper扫描等。

9. **编写测试**:创建相关的Java类和配置文件进行测试。

以下是整合框架的详细步骤和配置文件示例:

### 1. 创建Web项目

在IDE(如Eclipse、IntelliJ IDEA)中创建一个新的Web项目。

### 2. 导入依赖

将以下jar包导入到项目的lib目录中:

- spring-websocket-4.2.0.RELEASE.jar
- 其他Spring4、Mybatis3和数据库驱动的jar包

### 3. 创建配置文件

#### 3.1 数据源配置(datasource.properties)

```properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
```

#### 3.2 Mybatis配置(mybatis.cfg.xml)

```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>
    <!-- Mybatis配置内容 -->
</configuration>
```

#### 3.3 Spring MVC配置(mvc-servlet.xml)

```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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.cy.ssm">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
```

#### 3.4 Spring应用上下文配置(applicationContext.xml)

```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-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/tx
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <context:component-scan base-package="com.cy.ssm">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <aop:aspectj-autoproxy/>
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:datasource.properties</value>
        </property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis.cfg.xml"/>
        <property name="typeAliasesPackage" value="com.cy.ssm.beans"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cy.ssm.mapper"/>
    </bean>
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
            <tx:method name="load*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="serviceMethods" expression="execution(* com.cy.ssm.service.impl.*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
    </aop:config>
</beans>
```

### 4. 配置web.xml

```xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml, /WEB-INF/classes/mvc-servlet.xml</param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.xml</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>ssm.root</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/mvc-servlet.xml</param-value>
        </init-param>
    </servlet>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【zabbix监控软件(配置及常用键值)】
  • it基础软件运维管理:从操作系统到数据库,再到中间件和应用系统
  • 使用Docker快速安装和运行Elasticsearch
  • 【Android】SurfaceFlinger Dumpsys信息分析
  • openVX加速-结合AI推理引擎代码示例
  • 集群聊天服务器项目【C++】(三)muduo库的简单介绍
  • 网页模板该怎么选
  • MVC 控制器
  • Java | Leetcode Java题解之第401题二进制手表
  • yolov8 rect batch_shapes 672 图像大小变化
  • PHP智驭未来悦享生活智慧小区物业管理小程序系统源码
  • Java的发展史与前景
  • SQL Server详细使用教程(包含启动SQL server服务、建立数据库、建表的详细操作) 非常适合初学者
  • 4G模块、WIFI模块、NBIOT模块通过AT指令连接华为云物联网服务器(MQTT协议)
  • 高效数据移动指南 | 如何快速实现数据库 MySQL 到 MongoDB 的数据同步?
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • 0x05 Python数据分析,Anaconda八斩刀
  • 4. 路由到控制器 - Laravel从零开始教程
  • angular学习第一篇-----环境搭建
  • canvas 高仿 Apple Watch 表盘
  • Docker下部署自己的LNMP工作环境
  • es6
  • gops —— Go 程序诊断分析工具
  • httpie使用详解
  • pdf文件如何在线转换为jpg图片
  • php ci框架整合银盛支付
  • vuex 笔记整理
  • webpack入门学习手记(二)
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 码农张的Bug人生 - 初来乍到
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • 关于Android全面屏虚拟导航栏的适配总结
  • 我们雇佣了一只大猴子...
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • ​flutter 代码混淆
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • (C++二叉树05) 合并二叉树 二叉搜索树中的搜索 验证二叉搜索树
  • (C语言)fread与fwrite详解
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (附源码)springboot 基于HTML5的个人网页的网站设计与实现 毕业设计 031623
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (南京观海微电子)——I3C协议介绍
  • (三) diretfbrc详解
  • (四)事件系统
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)setTimeout 和 setInterval 的区别
  • (转载)Google Chrome调试JS
  • .env.development、.env.production、.env.staging
  • .mysql secret在哪_MYSQL基本操作(上)
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .NET Framework 服务实现监控可观测性最佳实践
  • .net SqlSugarHelper