将Mybatis和Spring 框架集成到一起

来源:互联网 发布:javascript数字格式化 编辑:程序博客网 时间:2024/06/06 04:49
  1).集成Spring
     1-1).引入jar包
       spring_home/libs/*.jar(不包含源码和说明jar包)
       commons-logging-1.1.1.jar
       1-2).引入配置文件

         applicationContext.xml


         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:p="http://www.springframework.org/schema/p"     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-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/tx     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd"    default-autowire="no" default-lazy-init="false" >        <context:component-scan base-package="com.spring.pmes.*" />        <!-- 读取外部配置文件的属性,这个配置文件配置了连接数据库的信息 -->    <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="location">    <value>classpath:DBConfig.properties</value>    </property>    </bean><!-- 数据源,连接池 c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property><!-- <property name="initialPoolSize" value="${initialPoolSize}"></property><property name="maxPoolSize" value="${maxPoolSize}"></property><property name="maxIdleTime" value="${maxIdleTime}"></property><property name="checkoutTimeout" value="${checkoutTimeout}"></property> --></bean><!-- 数据源,连接池 DBCP <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"><property name="driverClassName" value="${driver}"></property><property name="url" value="${url}"></property><property name="username" value="${user}"></property><property name="password" value="${password}"></property><property name="initialSize" value="${initialSize}"></property><property name="maxActive" value="${maxActive}"></property><property name="maxIdle" value="${maxIdle}"></property><property name="maxWait" value="${maxWait}"></property></bean>--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="dataSource"></property>    <property name="configLocation" value="classpath:mybatis-config.xml"></property>    <property name="mapperLocations">        <list>            <value>classpath:mapper-user.xml</value>        </list>    </property></bean><!-- 配置事务管理器  --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 事务通知  Advice, Aspect是切面,是一个抽象的概念,Advice是具体存在的Aspect AOP增强器--><tx:advice id="transactionAdvice" transaction-manager="transactionManager"><tx:attributes><!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/><tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/><tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/> --><tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/><tx:method name="query*" read-only="true"/></tx:attributes></tx:advice><!-- 切入* com.spring.service.*.*(..)第一个* 表示的是方法的返回值第二个* 表示的是service包中的所有类第三个*表示的是所有类中的所有方法 --><aop:config><aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.spring.pmes.service.*.*(..))"/></aop:config>    </beans>
 2).集成Mybatis
       2-1). 引入jar包
           MYBATISHOME/mybatis-3.2.1.jar
           MYBATISHOME/lib/*.jar
        
        mysql-connector-java-5.0.6-bin.jar
       2-2).引入框架的配置文件
          1).主配置文件      mybatis-config.xml

          2).映射文件     例如mapper-user.xml


mybatis-config.xml 如下:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!--    <typeAliases>  <typeAlias type="com.spring.pmes.User" alias="UserBean"/>  </typeAliases>--></configuration>

mapper-user.xml如下:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="test">  <!-- 查询标签  id : sql映射ID,可以和SQL进行绑定,当访问这个ID时,等同于访问绑定的sql文  resultType : 将查询结果转换为指定的类型(类的完整名称)。   --><!-- <select id="queryByUserId" resultType="com.spring.pmes.User">select * from t_user where id=#{id}</select> --></mapper>

  3).集成两个框架
     3-1).引入jar包
          mybatis-spring-1.2.0.jar
          C3p0连接池:c3p0-0.9.1.2.jar
          AOP:libs/aopalliance-1.0.jar,libs/aspectjweaver.jar
     3-2).修改spring的配置文件,增加集成的相关配置
     4).增加程序代码,进行测试

3-2).在applicationContext.xml 中已有体现


原创粉丝点击