spring和mybatis整合

来源:互联网 发布:贝格大数据 编辑:程序博客网 时间:2024/06/08 18:01

记录,帮助梳理一下.

二者的整合,其实实质就是将mybatis用到的组件交给spring来管理.

1. 与mybatis相关.

与mybatis相关的主要有:

  1. 数据库连接.这里面与数据库密码,用户名等有关.
  2. 会话工厂,需要用到数据库连接,基于mybatis的配置,mapper扫描路径等.
  3. mapper代理相关.负责从mapper包中扫描接口,并自动创建代理对象,在spring容器中注册.

下面是一个典型的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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"    xsi:schemaLocation="                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd      http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd      http://www.springframework.org/schema/task        http://www.springframework.org/schema/task/spring-task-3.0.xsd">    <!--1定义一个jdbc数据源,创建一个驱动管理数据源的bean -->    <context:annotation-config/>    <task:annotation-driven/>     <context:annotation-config/>      <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>      <context:component-scan base-package="com"/>      <bean id="dataSource"        class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx:3306/xx" />        <property name="username" value="xxx" />        <property name="password" value="xxxx" />    </bean>    <!--创建一个sql会话工厂bean,指定数据源 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 1指定数据源 -->        <property name="dataSource" ref="dataSource" />        <property name="mapperLocations" value="classpath:com/xxx/email/test/*.xml"></property>                 <!-- 加载mybatis的全局配置文件,放在classpath下的mybatis文件夹中了 -->        <property name="configLocation" value="classpath:mybatis-config.xml" />    </bean>    <!-- mapper批量扫描包,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.xx.email.test" />        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />    </bean></beans> 

可以看到,spring负责了数据源的配置,会话工厂的配置,mapper的扫描以及代理类的实例化;

2. spring相关

搭建spring框架就是导入包就可以.再就是获取bean的时候,需要显式地引入applicationContext.xml文件(换个文件名也可以)

原创粉丝点击