Spring+Mybatis整合全流程

第一步:

导入Spring+Mybatis需求的jar包

其中Spring包需求:

  aop,asm,aspects,beans,context,core,expression,jdbc,test,transaction,tx,web,webmvc包

  com.springsource.org.aopalliance

  com.springsource.org.apache.commons.logging

  com.springsource.org.apache.log4j

其中mybatis需求包:

  mybatis

其中数据库需求包:

  mysql-connector-java

其中整合Spring+mybatis包需求:

  mybatis-spring-1.2.3.jar

大概就这些,如有继续需求,后续进行查询后添加。

第二步,填写Spring+mybatis配置

  创建一个applicationContext.xml为名的xml文件

  为其添加命名空间。

<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:mvc="http://www.springframework.org/schema/mvc"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  添加标签

 <context:annotation-config/>

  作用是告知编译器Spring使用注解的方式

  接着配置Spring中用到的数据源

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     	<property name="driverClassName">
     		<value>com.mysql.jdbc.Driver</value>
     	</property>
     	<property name="url">
     		<value>jdbc:mysql://localhost:3306/****?useUnico=true&characterEncoding=UTF-8</value>
     	</property>
     	<property name="username">
     		<value>root</value>
     	</property>
     	<property name="password">
     		<value>root</value>
     	</property>
     </bean>

  其中org.springframework.jdbc.datasource.DriverManagerDataSource是Spring自带的数据源,并为该数据源配置属性信息,使其能够连接到指定数据库

 

  配置sqlSessionFacory对象,mybatis中,使用SqlSessionFactoryBuilder来创建SqlSessionFactory对象,用来获取SqlSession对象,一旦获取到SqlSession对象,可以用来执行映射语句,提交或回滚连接,最后当不需要的时候,可以关闭SqlSession

  Mybatis-Spring社区自己开发了一个Mybatis-Spring用来满足用户整合Spring需求。

  使用Mybatis-Spring之后,会使用SqlSessionFactoryBean来替代SqlSessionFactoryBuilder创建SqlSessionFactory

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     	<property name="typeAliasesPackage" value="cn.eden.pojo" />
     	<property name="dataSource" ref="dataSource" />
     	<property name="mapperLocations" value="classpath:cn/eden/mapper/*.xml"></property>
 </bean>

  dataSource属性

  该属性必须配置,多数据源时会有多个dataSource,同时也需要配置多个sqlSessionFactory来对应。

  mapperLocations属性

  配置该属性后,sqlSessionFactory会自动扫描该路径下的所有文件并解析。该路径支持多个,可以用,;\t\n进行分割。每一个路径都可以用直接的包路径,或者Ant风格的表达式。

  configLocation属性

  上面例子中并没有使用该属性,当SqlSessionFactoryBean提供的配置不能满足使用时,你可以使用mybatis-config.xml配置文件配置其他属性,然后通过configLocation属性指定该配置的路径,SqlSessionFactoryBean会使用该配置文件创建Configuration

  typeAliasesPackage属性

  该属性可以给包中的类注册别名,注册后可以直接使用类名,而不用使用全限定的类名(就是不用包含包名)。该属性可以配置多个,可以用,;\t\n进行分割。但是不支持Ant风格的路径

  plugins属性

  该属性可以配置MyBatis的拦截器,拦截器的配置顺序会影响拦截器的执行顺序。从上往下的拦截器,实际的执行顺序是这样,第一个拦截器会最后执行,最后一个会首先执行。然后出拦截器的顺序和配置的顺序一致,第一个最先返回,最后一个最后返回。

 

  配置MapperScannerConfigurer

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="cn.eden.mapper" />
  </bean>

  为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现:MapperFactoryBean。这个类 可以让你直接注入数据映射器接口到你的 service 层 bean 中。当使用映射器时,你只需要想调用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring 将会为你创建代理。

  所以在service层中可以直接注入Dao对象,通过使用Dao直接调用方法即可

 

posted @ 2018-05-02 15:32  不再孤单丶mata陈  阅读(127)  评论(0)    收藏  举报