Spring集成Mybatis

来源:互联网 发布:易语言在线播放器源码 编辑:程序博客网 时间:2024/06/06 03:57

基本配置:

    <!-- 扫描包下的注解,使用exclude-filter来将过滤到的注解不注册到spring管理的bean。  这里的例子为将扫描@Controller注解的类,不进行spring管理,因为springMVC会管理 -->    <context:component-scan base-package="controller" >        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />    </context:component-scan>  <context:component-scan base-package="service.impl" />    <!-- 引入配置文件 -->   <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">      <property name="location" value="classpath:jdbc.properties" />  </bean>      <!-- 配置c3p0的连接池 -->    <bean id="c3p0dataSource" 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.username}"></property>        <property name="password" value="${jdbc.password}"></property>     <!-- 指定连接池中保留的最大连接数. Default:15-->      <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>      <!-- 指定连接池中保留的最小连接数-->      <property name="minPoolSize" value="${jdbc.minPoolSize}"/>      <!-- 指定连接池的初始化连接数  取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->      <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>      <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->      <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>      <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->      <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>      <property name="maxStatements" value="${jdbc.maxStatements}"/>      <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->      <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>    </bean>    <!-- 集成mybatis -->    <!--                 mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。                MyBatis- Spring 中,使用了SqlSessionFactoryBean来替代。                SqlSessionFactoryBean有一个必须属性 dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)    -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="c3p0dataSource" />         <property name="configLocation" value="classpath:SqlMapConfig.xml" /> <!-- 指定sqlMapConfig总配置文件,订制的environment在spring容器中不在生效-->      </bean>      <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">           <constructor-arg index="0" ref="sqlSessionFactory">    </constructor-arg>    </bean>     <!-- DAO接口所在包名,Spring会自动查找其下的类 动态代理实现 不用写dao的实现类,通过扫描的方式给dao包下所有dao接口注册,然后在service中就可以@Autowired注入dao了-->      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">          <property name="basePackage" value="dao" />          <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate" />     </bean>       <!-- 配置事物管理器:真正管理事物的类。 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 给事物管理模板注入连接池,去获得connection对象才能进行事物管理 -->        <property name="dataSource" ref="c3p0dataSource"></property>    </bean>       <!-- 开启注解式管理事物 -->    <tx:annotation-driven transaction-manager="transactionManager"/>    <!-- 加载邮件配置文件 -->    <context:property-placeholder location="classpath:mail.properties"/>    <!--邮件服务开始-->    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">        <property name="host" value="smtp.163.com" />        <property name="port" value="25"/>        <property name="username" value="18390237197@163.com" />        <property name="password" value="123654enzo" />        <property name="defaultEncoding" value="UTF-8"/>        <property name="javaMailProperties">            <props>                <prop key="mail.smtp.auth">true</prop>                <prop key="mail.smtp.timeout">25000</prop>            </props>        </property>    </bean>    <!--用于异步执行发送邮件的线程池-->    <bean id="mailTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">        <property name="corePoolSize" value="10"/>        <property name="maxPoolSize" value="30"/>    </bean>    <!--邮件服务结束-->    <!--<aop:config>        <aop:pointcut expression="execution(* service.StudentServiceImpl.*(..))" id="servicePoint"/>        <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePoint"/>    </aop:config>        <tx:advice id="txAdvice" transaction-manager="transactionManager">         <tx:attributes>            <tx:method name="save*" propagation="REQUIRED" />              <tx:method name="delete*" propagation="REQUIRED" />              <tx:method name="update*" propagation="REQUIRED" />              <tx:method name="get*" propagation="REQUIRED" read-only="true" />          </tx:attributes>    </tx:advice> -->

原创粉丝点击