Spring整合Mybatis的三种形式

    xiaoxiao2022-07-14  190

    首先是jar包

    aopalliance-1.0.jar aspectjweaver-1.6.9.jar commons-dbcp-1.4.jar commons-logging-1.2.jar commons-pool-1.6.jar mybatis-3.5.1.jar mybatis-spring-1.3.2.jar mysql-connector-java-5.1.0-bin.jar spring-aop-4.3.9.RELEASE.jar spring-aspects-4.2.0.RELEASE.jar spring-beans-4.3.9.RELEASE.jar spring-context-4.3.9.RELEASE.jar spring-context-support-4.3.9.RELEASE.jar spring-core-4.3.9.RELEASE.jar spring-expression-4.3.9.RELEASE.jar spring-jdbc-4.3.9.RELEASE.jar spring-tx-4.3.9.RELEASE.jar

    1.需要将mybatis.xml的配置使用Spring注入方式

    <?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.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 整合mybatis流程: 1.配置数据库信息 2.将mybatis的配置转为Spring的配置方式(数据库和Mapper.xml配置) ->sqlSessionFactory 3.将Mapper.xml和Mapper接口建立映射关联(Mapper接口和sqlSessionFactory的关联)-> studentMapper 4.通过Service层注入调用 --> <!-- 自动扫描 使用注解时使用--> <context:component-scan base-package="com.zy" /> <!-- 引入properties文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:database.properties" /> </bean> <!-- 配置数据库信息 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapper.xml文件,**表示迭代查找 批量处理 <property name="mapperLocations" value="classpath:com/zy/mapper/StudentMapper.xml" /> --> <property name="mapperLocations" value="classpath:com/zy/mapper/*.xml" /> </bean> <!-- 第一种方式 通过dao层继承接口 SqlSessionDaoSupport <bean id="studentMapper" class="com.zy.dao.impl.StudentDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> --> <!-- 第二种方式 通过 MapperFactoryBean这个类直接将接口和mapper映射 这样就不需要dao层来实现查询了--> <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.zy.mapper.StudentMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 第三种方式 在第二种方式上做批量处理 mapper接口所在包名,Spring会自动查找其下的类 没有了studentMapper这个bean 约定(接口名 =id值) 通过这个MapperScannerConfigurer类来批量处理mapper接口 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zy.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> --> <!-- (事务管理)transaction manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>

     

    最新回复(0)