mybatis-spring整合

使用原生Spring API接口

  注意xml文件一定要放到resources层(建个包)

aop约束

<aop:config>
    <aop:pointcut id="point" expression="execution(* com.openlab.Service.*.*(..))"/>
    <!--        配置切入点-->
    <aop:advisor advice-ref="txadvice" pointcut-ref="point"></aop:advisor>
    </aop:config>
    <tx:advice transaction-manager="transactionManager" id="txadvice">
        <tx:attributes>
            <tx:method name="*"/>
            <tx:method name="find*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

mybatis-bean xml文件

<context:component-scan base-package="com.openlab"></context:component-scan>
    <context:property-placeholder location="classpath:druid.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:com/openlab/Dao/*.xml"></property>
<!--        绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
<!--    SqlSessionTemplate就是 我们使用的sqlsession-->
    <bean class="org.mybatis.spring.SqlSessionTemplate" id="sessionTemplate">
<!--        只能使用构造器注入sqlsessionFactory,因为它没有set方法-->
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.openlab.Dao"></property>
        <property name="sqlSessionTemplateBeanName" value="sessionTemplate"></property>
    </bean>

将实现层注入方法,

  stuMappper注入属性,私有化属性

测试类

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值