Struts2、Mybatis、Spring整合
1、工程结构
2、web.xml配置
<!-- 配置Spring --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Struts的配置 --><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>
3、struts.xml配置
<struts><constant name="struts.enable.DynamicMethodInvocation" value="true" /><constant name="struts.devMode" value="true" /><package name="default" namespace="/aaa" extends="struts-default"><!-- class要写Spring配置文件里的类名,不要写真实的类名 --><action name="list" class="userAction"><result>/list.jsp</result></action></package><include file="example.xml"/>
</struts>
4、db.properties配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=mysqladmin
5、log4j.properties配置
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
6、SqlMapperConfig.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><!-- 上面的配置全部交给Spring管理,此处不用配置 --><mappers><package name="cn.sxt.vo"/></mappers>
</configuration>
7、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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties"/><!-- 配置数据源 ,数据库链接信息--><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="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="insert" propagation="REQUIRED"/> <tx:method name="get" read-only="true"/><tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/></tx:attributes></tx:advice><aop:config><aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/></aop:config><!-- 声明式事务配置 结束 --><!-- 配置SqlSessionFactory,目的是的到SqlSessionFactory对象--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!-- 加载SqlMapConfig.xml配置文件 --><property name="configLocation" value="classpath:SqlMapConfig.xml"></property></bean><import resource="config/spring/user.xml"/></beans>
8、user.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- UserDaoImpl接口继承了SqlSessionDaoSupport,所以要注入sqlSessionFactory -->
<bean id="UserDao" class="cn.sxt.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="UserService" class="cn.sxt.service.impl.UserServiceImpl">
<property name="userDao" ref="UserDao"></property>
</bean>
<!-- 生成Action的对象,不能使用单例子 -->
<bean id="userAction" class="cn.sxt.action.UserAction" scope="prototype">
<property name="userService" ref="UserService"></property>
</bean>
</beans>
9、UserDaoImpl.java
package cn.sxt.dao.impl;//实现SqlSessionDaoSupport类,注入sqlSessionFactory
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {@Overridepublic List<User> getAll() throws Exception {SqlSession sqlSession = this.getSqlSession();//注入sqlSessionFactory,通过this得到UserMapper userMapper = sqlSession.getMapper(UserMapper.class);List<User> list = userMapper.getAll();return list;}
}
本例主要是ssm的配置,其余的java代码,dao层,service层,Action,pojo很简单,不在此一一列出
Struts2、Mybatis、Spring整合相关推荐
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- Struts2学习笔记——Struts2与Spring整合
Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的be ...
- Maven+Struts2+MyBatis+Spring搭建教程
教程目标: 在MyEclipse中搭建Struts2+MyBatis+Spring环境,用Maven管理项目,利用mybatis-gernerator插件生成部分代码 附带目标: struts2的自定 ...
- Struts2与Spring整合
Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的be ...
- 二十六:Struts2 和 spring整合
二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...
- 【Mybatis+spring整合源码探秘】--- mybatis整合spring事务原理
文章目录 1 mybatis整合spring事务原理 1 mybatis整合spring事务原理 本篇文章不再对源码进行具体的解读了,仅仅做了下面一张图: 该图整理了spring+mybatis整合后 ...
- springMvc+mybatis+spring 整合 包涵整合activiti 基于maven
2019独角兽企业重金招聘Python工程师标准>>> 最近自己独立弄一个activiti项目,写一下整合过程: 环境:jdk1.7 tomcat7.0 maven3.5 ecli ...
- MyBatis+Spring整合
不多说,直接上案例! 1. 建立数据库+表 CREATE TABLE s_user(user_id INT AUTO_INCREMENT PRIMARY KEY,user_name VARCHAR(3 ...
- Struts2 Hibernate Spring 整合的基本流程和步骤及其相关配置细节
配置Hibernate环境 1. 把Hibernate的相关jar包复制到lib目录下: ① HIBERNATE_HOME/lib 下的相关的依赖的第三方包 ② HIBERNATE_HOME/hibe ...
- mybaits二十六:mybatis,spring整合
整合需要的spring的jar包
最新文章
- Excel访问局域网中OLAP方案
- Server.MapPath()目录详解
- ASP.NET Page执行顺序如:OnPreInit()、OnInit()
- Apache Thrift的使用
- CactiEZ V10.1 中文版 Cacti中文解决方案+使用教程(2)
- Azure App Service 如何在第一时间用上最新版 .NET Core
- sitecore系统教程之体验编辑器
- Memcache的最佳实践方案
- 一次 MySQL 索引面试,被面试官怼的体无完肤!
- 关于left join 一些测试
- 一建已经过去,正是中级通信工程师黄金备考期!
- Docker容器 Cgroup资源分配(CPU和内存资源分配)
- (三)基于Phyphox的三线摆法测量物体转动惯量
- js批量删除微博教程
- QT隐藏标题栏和背景
- 如何基于 RISC-V CPU 集成一个 RISC-V SoC 呢?(上)
- c语言成绩与平均分问题,用C语言编程平均分数
- 微信公众号开发本地环境搭建
- python命令解析使用多线程扫描端口
- [网络安全学习篇附]:利用5次shift漏洞破解win7密码(详)
热门文章
- python最详细 ---- 元类 __metaclass__
- break和continue的作用和区别是什么?(详细解答)
- 前端导出文件为word格式(React)
- 服务器固态硬盘120g多少钱,你还在买120G固态硬盘?已经有人在后悔
- 中国式家长如何成为计算机科学家,中国式家长清华大学怎么弄_一周目上清华流程介绍_3DM单机...
- Markdown、LaTeX数学公式编辑
- eclipse java启动参数设置_[Java教程]eclipse.ini配置eclipse的启动参数
- 留守儿童迎来“AI小伙伴” 金融壹账通携手金融机构落地“加马成长伙伴”
- 让模型理解和推断代码背后的意图是预训练模型的核心挑战 | NPCon演讲实录
- 实验室三维磁场电磁铁的主要用途及技术指标