spring与mybatis四种整合方法
转载:
1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
(1)Spring配置文件:
- <!-- 引入jdbc配置文件 -->
- <context:property-placeholder location="jdbc.properties"/>
- <!--创建jdbc数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${driver}"/>
- <property name="url" value="${url}"/>
- <property name="username" value="${username}"/>
- <property name="password" value="${password}"/>
- <property name="initialSize" value="${initialSize}"/>
- <property name="maxActive" value="${maxActive}"/>
- <property name="maxIdle" value="${maxIdle}"/>
- <property name="minIdle" value="${minIdle}"/>
- </bean>
- <!-- 创建SqlSessionFactory,同时指定数据源-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!--创建数据映射器,数据映射器必须为接口-->
- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="com.xxt.ibatis.dbcp.dao.UserMapper" />
- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
- </bean>
- <bean id="userDaoImpl2" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl2">
- <property name="userMapper" ref="userMapper"/>
- </bean>
- public interface UserMapper {
- @Select("SELECT * FROM user WHERE id = #{userId}")
- User getUser(@Param("userId") long id);
- }
- public interface UserDao {
- public User getUserById(User user);
- }
- public class UserDaoImpl2 implements UserDao {
- private UserMapper userMapper;
- public void setUserMapper(UserMapper userMapper) {
- this.userMapper = userMapper;
- }
- public User getUserById(User user) {
- return userMapper.getUser(user.getId());
- }
- }
mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。MyBatis-
Spring 中,使用了SqlSessionFactoryBean来替代。SqlSessionFactoryBean有一个必须属性
dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)。
(1)Spring配置文件:
- <!-- 创建SqlSessionFactory,同时指定数据源-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <!-- 指定sqlMapConfig总配置文件,订制的environment在spring容器中不在生效-->
- <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
- <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation 有一个即可,当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
- <!- - <property name="mapperLocations" value="classpath*:com/xxt/ibatis/dbcp/**/*.xml"/> -->
- </bean>
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="sqlSessionFactory" />
- </bean>
- <bean id="UserDaoImpl " class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl">
- <!--注入SqlSessionTemplate实例 -->
- <property name="sqlSessionTemplate" ref="sqlSession" />
- -->
- </bean>
- <configuration>
- <typeAliases>
- <typeAlias type="com.xxt.ibatis.dbcp.domain.User" alias="User" />
- </typeAliases>
- <mappers>
- <mapper resource="com/xxt/ibatis/dbcp/domain/user.map.xml" />
- </mappers>
- </configuration>
- <mapper namespace="com.xxt.ibatis.dbcp.domain.User">
- <resultMap type="User" id="userMap">
- <id property="id" column="id" />
- <result property="name" column="name" />
- <result property="password" column="password" />
- <result property="createTime" column="createtime" />
- </resultMap>
- <select id="getUser" parameterType="User" resultMap="userMap">
- select * from user where id = #{id}
- </select>
- <mapper/>
- public class UserDaoImpl implements UserDao {
- public SqlSessionTemplate sqlSession;
- public User getUserById(User user) {
- return (User)sqlSession.selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user);
- }
- public void setSqlSession(SqlSessionTemplate sqlSession) {
- this.sqlSession = sqlSession;
- }
- }
(1)spring配置文件:
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
- <!-- <property name="mapperLocations" value="classpath*:com/xxt/ibatis/dbcp/domain/user.map.xml"/ > -->
- </bean>
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="sqlSessionFactory" />
- </bean>
- <bean id="userDaoImpl3" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl3">
- <!--注入SqlSessionTemplate实例 -->
- <property name="sqlSessionTemplate" ref="sqlSession" />
- <!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效 -->
- <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
- -->
- </bean>
(2) dao层接口实现类UserDaoImpl3:
- public class UserDaoImpl3 extends SqlSessionDaoSupport implements UserDao {
- public User getUserById(User user) {
- return (User) getSqlSession().selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user);
- }
- }
- <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <beans:property name="dataSource" ref="simpleDataSource" />
- <beans:property name="configLocation"
- value="classpath:conf/core/mybatis-config.xml" />
- </beans:bean>
- <beans:bean id="sqlSessionFactory_contact" class="org.mybatis.spring.SqlSessionFactoryBean">
- <beans:property name="dataSource" ref="simpleDataSource_contact" />
- <beans:property name="configLocation"
- value="classpath:conf/core/mybatis-config-contact.xml" />
- </beans:bean>
- <beans:bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <beans:property name="dataSource" ref="simpleDataSource" />
- </beans:bean>
- <beans:bean id="transactionManager_contact"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <beans:property name="dataSource" ref="simpleDataSource_contact" />
- </beans:bean>
- <!-- <tx:annotation-driven transaction-manager="transactionManager" />
- <tx:annotation-driven transaction-manager="transactionManager_contact" />
- -->
- <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="com.mybatis.UserDao"></property>
- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
- </bean>
- <bean id="userService" class="com.mybatis.UserServiceImpl">
- <property name="userDao" ref="userDao"></property>
- </bean>
- <!-- 加载配置文件 -->
- <beans:bean name="mapperScannerConfigurer"
- class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <beans:property name="basePackage" value="com.elong.hotel.crm.data.mapper" />
- <beans:property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></beans:property>
- </beans:bean>
- <beans:bean name="mapperScannerConfigurer_contact"
- class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <beans:property name="basePackage"
- value="com.elong.hotel.crm.data.contact.mapper" />
- <beans:property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_contact"></beans:property>
- </beans:bean>
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <!-- name表示以什么开始的方法名,比如 add*表示add开头的方法 propagation表示事务传播属性,不写默认有 -->
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="del*" />
- <tx:method name="update*" />
- <tx:method name="find*" read-only="true" />
- <tx:method name="get*" read-only="true" />
- <tx:method name="search*" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <tx:advice id="txAdvice_contact" transaction-manager="transactionManager_contact">
- <tx:attributes>
- <!-- name表示以什么开始的方法名,比如 add*表示add开头的方法 propagation表示事务传播属性,不写默认有 -->
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="del*" />
- <tx:method name="update*" />
- <tx:method name="find*" read-only="true" />
- <tx:method name="get*" read-only="true" />
- <tx:method name="search*" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <!-- 配置事务切面 -->
- <aop:config>
- <aop:pointcut expression="execution(* com.elong.hotel.crm.service..*.*(..))"
- id="pointcut" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
- <aop:advisor advice-ref="txAdvice_contact" pointcut-ref="pointcut" />
- </aop:config>
spring与mybatis四种整合方法的更多相关文章
- Spring学习笔记:spring与mybatis四种整合方法
1.采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数. (1)Spring配置文件: <!-- 引入jdbc ...
- spring与mybatis三种整合方法
spring与mybatis三种整合方法 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接 http://code.googl ...
- spring与mybatis五种整合方法
1.采用数据映射器(MapperFactoryBean)的方式 不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数. (1)Spring配置文件: <!-- 引入jdbc ...
- Mybatis(六):spring与mybatis三种整合方法
1.采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean. spring-mybatis.xml: <?xml ve ...
- 使用Spring Security3的四种方法概述
使用Spring Security3的四种方法概述 那么在Spring Security3的使用中,有4种方法: 一种是全部利用配置文件,将用户.权限.资源(url)硬编码在xml文件中,已经实现过, ...
- Spring+SpringMVC+MyBatis+Maven框架整合
本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Sprin ...
- 转:深入浅出spring IOC中四种依赖注入方式
转:https://blog.csdn.net/u010800201/article/details/72674420 深入浅出spring IOC中四种依赖注入方式 PS:前三种是我转载的,第四种是 ...
- 普通java类加入spring容器的四种方式
今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的s ...
- idea spring+springmvc+mybatis环境配置整合详解
idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2. ...
随机推荐
- Spring tokenizeToStringArray
tokenizeToStringArray: StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimToken ...
- Gamma函数深入理解
Gamma函数 当n为正整数时,n的阶乘定义如下:n! = n * (n - 1) * (n - 2) * … * 2 * 1. 当n不是整数时,n!为多少?我们先给出答案. 容易证明,Γ(x + 1 ...
- web3.js编译Solidity,发布,调用全部流程(手把手教程)
web3.js编译Solidity,发布,调用全部流程(手把手教程) 下面教程是打算在尽量牵涉可能少的以太坊的相关工具,主要使用web3.js这个以太坊提供的工具包,来完成合约的编译,发布,合约方法调 ...
- git log的常见用法
git log 使用git log命令,什么参数都没有的话,会以下面的格式输出所有的日志(我当前的git仓库只有三个提交).如果日志特别多的话,在git bash中,按向下键来查看更多,按q键退出查看 ...
- Python之pytest 基础
pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:1.简单灵活,容易上手:2.支持参数化:3.能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appn ...
- 大数据学习路线:Zookeeper集群管理与选举
大数据技术的学习,逐渐成为很多程序员的必修课,因为趋势也是因为自己的职业生涯.在各个技术社区分享交流成为很多人学习的方式,今天很荣幸给我们分享一些大数据基础知识,大家可以一起学习! 1.集群机器监控 ...
- oracle goldengate 远程捕获和投递
很早之前,OGG只支持部署在数据库主机上,这叫本地化部署.而现在OGG支持远端部署,即OGG软件不安装在数据库主机上,而是安装在单独的机器上,负责数据抽取和投递. 这样做的好处: l 易于管理 - 在 ...
- 深入理解HashMap+ConcurrentHashMap的扩容策略
前言 理解HashMap和ConcurrentHashMap的重点在于: (1)理解HashMap的数据结构的设计和实现思路 (2)在(1)的基础上,理解ConcurrentHashMap的并发安全的 ...
- 使用Wisdom RESTClient自动化测试REST API,如何取消对返回的body内容的校验?
使用Wisdom RESTClient V1.1 自动化测试API,默认是对返回HTTP状态码和body内容进行校验的. 如果您的API返回body内容是变化的,可以通过设置来取消对body内容的校验 ...
- dropout——gluon
https://blog.csdn.net/lizzy05/article/details/80162060 from mxnet import nd def dropout(X, drop_prob ...