• Spring框架集成mybatis框架的配置(笔记)



    <!-- 0.注解扫描 -->
    <!-- 1.导入外部文件 -->
    <!-- 2.数据源 -->
    <!-- 3.session Factory -->
    <!-- 4.事务模板 -->
    <!-- 5.AOP相关配置 -->
    <!-- Mapper扫描 -->

    <?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:jee="http://www.springframework.org/schema/jee"
    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/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 0.注解扫描 上下文 构成 扫描 -->
    <context:component-scan base-package="com.wxzj.crm"></context:component-scan>
    <!-- 1.导入外部文件 上下文 属性 占位 位置-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 2.数据源 ctrl shift T 搜索datasource 告诉spring初始化时初始化链接,销毁时关闭链接
    同时需要配置参数,驱动的名字,链接,账号,密码 name value ${}(在尖括号里写)

    -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">


    <property name="driverClassName" value="${jdbc.driverClassName}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 3.session Factory mybatis获取session的工场是SqlSessionFactoryBean
    里面需要配置一个数据库源
    和一个 配置config 源location 指向mybatis的主配置文件
    第三个给每一个类起一个别名,这是为啥
    找mapper 这次找的是路径 选择路径下的所有mapper (映射)
    -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
    <property name="typeAliasePackage" value="com.wxzj.crm.domain"></property>
    <property name="mapperLocations" value="com/wxzj/crm/mapper/*Mapper.xml"></property>
    </bean>
    <!-- 事务管理器 transaction事务的意思 给他配置数据源,让他在这个数据源上建立事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 4.事务模板 他自己没有事务,得用别人的,这次用阿里巴巴druid 他需要的事务管理器的名字叫做transaction-manager,默认引用

    advice: n. 建议;忠告;劝告;通知 -->
    <tx:advice id="advice" transaction-manager="transactionManager">
    <!-- 配置每一个方法的专属配置 attributes 属性 -->
    <tx:attributes>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="query*" read-only="true"/>
    <tx:method name="select*" read-only="true"/>
    <tx:method name="list*" read-only="true"/>
    <tx:method name="*" />
    </tx:attributes>
    </tx:advice>
    <!-- 5.AOP 切面相关配置 和事务一块使用的,他是面向切面编程的配置
    事务(id=advice)配置的是怎么管理事务,AOP配置的是在哪执行事务
    如果只配置事务不配置aop的话应该是没用的
    pointcut切入点 execution执行-->
    <aop:config>
    <aop:pointcut expression="execution(* com.xyz.myapp.service.*.*(..))" id="pointCut"/>
    <aop:advisor advice-ref="advice" pointcut-ref="pointCut"/>
    </aop:config>
    <!-- Mapper扫描 配置个mapper扫描的对象,让他去扫描mapper ,这应该是spring整合其他框架的手法-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.wxzj.crm.mapper"></property>
    </bean>

    </beans>

  • 相关阅读:
    真正的Java学习从入门到精通
    Java学习从入门到精通(1) [转载]
    Java Learning Path(三)过程篇
    Java Learning Path(五)资源篇
    Java Learning Path(四) 方法篇
    浅析Java语言中两种异常的差别
    JDK,JRE,JVM区别与联系
    JAVA敏捷开发环境搭建
    谈谈WEB开发中的苦大难字符集问题
    java读取clob字段的几种方法
  • 原文地址:https://www.cnblogs.com/work396/p/7135999.html
Copyright © 2020-2023  润新知