SSM框架整合的其它方式
---------------------siwuxie095
SSM 框架整合的其它方式
1、主要是整合 Spring 框架和 MyBatis 框架时,可以不写
MyBatis 核心配置文件:mybatis-config.xml
2、把 MyBatis 核心配置文件中的配置全都转移到 Spring
核心配置文件中
3、具体实现
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 使用spring自带的占位符替换功能 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 允许JVM参数覆盖 --> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<!-- 忽略没有找到的资源文件 --> <property name="ignoreResourceNotFound" value="true"/>
<!-- 配置资源文件(也称 外部属性文件) --> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property>
</bean>
<!-- 配置 BoneCP 连接池 --> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<!-- 数据库驱动 --> <property name="driverClass" value="${jdbc.driverClassName}" />
<!-- 相应驱动的jdbcUrl --> <property name="jdbcUrl" value="${jdbc.url}" />
<!-- 数据库的用户名 --> <property name="username" value="${jdbc.username}" />
<!-- 数据库的密码 --> <property name="password" value="${jdbc.password}" />
<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 --> <property name="idleConnectionTestPeriod" value="60" />
<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 --> <property name="idleMaxAge" value="30" />
<!-- 每个分区最大的连接数 --> <property name="maxConnectionsPerPartition" value="150" />
<!-- 每个分区最小的连接数 --> <property name="minConnectionsPerPartition" value="5" />
</bean>
<!-- 将 SqlSessionFactory 对象的创建交给 Spring 进行管理 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 --> <property name="dataSource" ref="dataSource" />
<!-- 开启自动驼峰命名规则映射 --> <property name="configuration"> <bean class="org.apache.ibatis.session.Configuration"> <property name="mapUnderscoreToCamelCase" value="true"/> </bean> </property>
<!-- 指定 MyBatis 映射配置文件的位置(路径) --> <property name="mapperLocations" value="classpath:com/siwuxie095/mapper/*.xml"/>
<!-- 指定 类型别名的扫描包 --> <property name="typeAliasesPackage" value="com.siwuxie095.entity"/>
</bean>
<!-- 配置 Service 对象 --> <bean id="userService" class="com.siwuxie095.service.UserService"> <property name="userMapper" ref="userMapper"></property> </bean>
<!-- 配置映射器接口(Mapper 接口)的扫描包 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 如有多个包,以逗号 或 分号隔开即可 --> <property name="basePackage" value="com.siwuxie095.mapper"/> </bean>
<!-- 配置映射器接口(以下方法二选一即可,这里选择法二):
法一:逐个配置:配置映射器接口(Mapper 接口)的对象
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.siwuxie095.mapper.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
法二:统一配置:配置映射器接口(Mapper 接口)的扫描包
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.siwuxie095.mapper"/> </bean>
-->
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入 dataSource --> <property name="dataSource" ref="dataSource" /> </bean>
<!-- 配置事务注解,即 开启事务注解 --> <tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 一般事务管理都是在 Service 层进行,只需在 Service 类上加上 @Transactional 注解 -->
</beans> |
注:主要针对 sqlSessionFactory 的 Bean 做修改
【made by siwuxie095】
- SSM框架整合的其它方式
- 框架整合____SSM框架整合(主流整合方式,最精简整合方式)
- JavaEE_Mybatis_SpringMVC_框架整合_lesson1_简单的SSM框架搭建_使用原始Dao开发方式进行测试
- SSM框架整合方式(二):XML配置文件代码
- SSM框架整合方式(一):XML配置文件代码
- 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis
- 基于IdeaIDE的ssm整合框架的maven管理项目,利用映射自动生成dao层,pojo层,mappers层
- spring+websocket整合(springMVC+spring+MyBatis即SSM框架和websocket技术的整合)
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- Maven+Spring+Spring MVC+MyBatis+MySQL整合SSM框架
- SSM(Spring+SpringMVC+MyBatis)框架——详细整合教程
- SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
- 框架整合____Spring整合Struts2(主流整合方式,最精简整合方式)
- 【框架】——SSM框架的整合(二)
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- java的ssm框架整合(一)
- 三、Memcached整合Spring cache注解完整示例(SSM框架)
- SpringMVC+Mybatis框架整合源码 项目SSM
- IDEA-Maven整合SSM框架,超详细教程(Spring+SpringMVC+Mybatis)