mybatis与spring整合
一、导入依赖
spring的相关jar
mybatis相关jar
mybatis、mybatis-spring整合的jar以及数据库驱动、数据源
框架配置
mybatis配置
- 全局配置文件(可以留下一些经常变动的配置,其他在spring配置SqlSessionFactoryBean的时候创建)
- mapper映射文件(以前mapper.xml须跟dao接口文件必须放在同一目录下,通过spring可以指定映射路径)
web.xml
- spring IOC容器跟随web一起启动,配置contextloaderlistener
<!--Spring配置: needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 在类路径下创建applicationContext.xml
- 前端控制器,配置dispatcherservlet
<!-- SpringMVC配置 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- 在WEB-INF里创建spring-servlet.xml
springMVC配置文件spring-servlet.xml
- 扫描组件;既然是springMVC,只要扫描控制器就行了
- 视图解析器
<!-- 只扫描控制器 -->
<context:component-scan base-package="com.atguigu.mybatis" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler/>
spring配置文件applicationContext.xml
- 扫描业务组件,过滤控制器
<context:component-scan base-package="com.atguigu.mybatis">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
- 配置数据源、事务、aop
<!-- 引入数据库的配置文件 -->
<context:property-placeholder location="classpath:dbconfig.properties" />
<!-- Spring用来控制业务逻辑。数据源、事务控制、aop -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- spring事务管理 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启基于注解的事务 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
整合mybatis
- spring管理所有组件。mapper的实现类
- spring用来管理事务,spring声明式事务
1、创建出SqlSessionFactory对象
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation指定全局配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--mapperLocations: 指定mapper文件的位置-->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
</bean>
2、配置一个可以进行批量执行的sqlSession
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
3、扫描所有的mapper接口的实现,让这些mapper能够自动注入
<!-- base-package:指定mapper接口的包名 -->
<mybatis-spring:scan base-package="com.atguigu.mybatis.dao"/>
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.atguigu.mybatis.dao"></property>
</bean> -->
-
原生mybatis方式
读文件流(全局配置文件)的方式通过SqlSessionFactoryBuilder构建SqlSessionFactory
Session session=sqlSessionFactory.getSession();
session.getMapper("mapper.class")获取接口对象
-
spring来接管
通过org.mybatis.spring.SqlSessionFactoryBean构建SqlSessionFactory
通过org.mybatis.spring.SqlSessionTemplate构建SqlSession
通过<mybatis-spring:scan base-package="com.dao">扫描所有mapper接口,跟sqlSessionFactoryBean定义好的mapperLocations映射上,通过@Autowired获取接口对象