Spring+Struts2+MyBatis集成

本文详细介绍了如何将Spring、Struts2和MyBatis进行集成,包括添加依赖包、配置log4j、数据库连接、web.xml修改,以及Spring和Struts2的核心配置。重点讲解了数据源配置、SessionFactory设置、事务管理和注解使用,为初学者提供了清晰的步骤指导。
摘要由CSDN通过智能技术生成

1. 加入Struts2, Spring3.3,MyBatis 和数据库驱动包

注意:当有包重复的时候选择高版本即可

2. 添加log4j文件和连接数据库的配置文件

log4j.rootLogger=DEBUG,File,Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]%m%n

log4j.appender.File=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.File.File=D:/log/SmartBlog.log
log4j.appender.File.MaxFileSize=100MB

log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug 
log4j.logger.java.sql.Statement=debug 
log4j.logger.java.sql.PreparedStatement=debug 

#\u5236\u5B9A\u6D88\u606F\u8F93\u51FA\u7684\u6700\u4F4E\u5C42\u6B21
#指定消息输出的最低层次
log4j.appender.File.Threshold=debug
#包含日志产生的时间、线程、类别等等信息
log4j.appender.File.layout=org.apache.log4j.TTCCLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

3. 修改web.xml文件

3.1 指定Spring配置文件的位置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

3.2 添加Spring的监听器

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.3 添加Struts2的拦截器

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4. 添加Struts2的核心配置文件(struts.xml)和Spring的核心配置文件(applaction.xml)

5. 修改Spring的核心配置文件

5.1 在Spring的核心配置文件中引入头文件

<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.0.xsd   
                            http://www.springframework.org/schema/context   
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

5.2 引入数据库配置文件的位置

<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" />

5.3 配置数据源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值