祝各位道友念头通达
GitHub Gitee 语雀 打赏

spring与mybatis的jar包的整合

1.maven新建的web(war)工程的结构如下图,每个包下的都为空

 

 

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

    <!-- 1.引入属性文件  -->
    <context:property-placeholder location="classpath:db.properties"/>
    
    <!-- 2.包扫描 -->
    <context:component-scan base-package="com.zhiyou.han">
        <context:exclude-filter type="annotation" expression="com.zhiyou.han.controller"/>
    </context:component-scan>
    
    <!-- 3.配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
    </bean>
    
    <!-- 4.配置SQLSessionFactory -->
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 
        <property name="configLocation" value="classpath:mybatis.xml"></property>
        <property name="mapperLocations" value="classpath:com/zhiyou/han/mapper/*.xml"></property>
        -->
    </bean>
    
    <!-- 5.配置mybatisDao接口扫描MapperScannerConfigurer -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zhiyou.han.dao"></property>
    </bean>
    
    <!-- 6.事务管理的配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
  <!-- 7.切面通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="query*" read-only="true" /> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 8.切面 --> <aop:config> <aop:pointcut expression="execution(* com.zhiyou.han.service.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/> </aop:config> </beans>

 

3.SQLSessionFactory的配置注意事项

 

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 
        <property name="configLocation" value="classpath:mybatis.xml"></property>
        <property name="mapperLocations" value="classpath:com/zhiyou/han/mapper/*.xml"></property>
        -->
 </bean>

 

因为mybatis.xml没有写,也没有相关的mapper,所以

4.测试代码:

如果SQLSessionFactory和DataSource有输出,怎配置没有问题

 

posted @ 2018-10-11 21:10  韩若明瞳  阅读(6598)  评论(0编辑  收藏  举报