Spring实战篇

1、Spring的xml配置文件头信息

<beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         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.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.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">

<beans/>


2、通过Spring的配置文件配置数据库连接池(set方法注入)

<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
</bean>

<!--读取db.properties的信息-->
<context:property-placeholder location="classpath:db.properties"/>

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
jdbc.username=OrangeMan
jdbc.password=&&&

<!-- 把交给IOC管理 SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="dataSource" />

          <!-- 传入PageHelper的插件 -->
          <property name="plugins">
                    <array>
                            <!-- 传入插件的对象 -->
                            <bean class="com.github.pagehelper.PageInterceptor">
                                    <property name="properties">
                                            <props>
                                                    <prop key="helperDialect">mysql</prop>
                                                    <prop key="reasonable">true</prop>
                                            </props>
                                     </property>
                              </bean>
                     </array>
           </property>
</bean>


3、注解生效,并且开启注解扫描

<context:annotation-config/>

<!-- 开启注解扫描,管理service和dao -->
<context:component-scan base-package="edu.gdkm.ssm.service" />
<context:component-scan base-package="edu.gdkm.ssm.dao" />


4、扫描持久层接口

<!-- 扫描dao接口 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="edu.gdkm.ssm.dao"/>
</bean>


5、配置Spring的声明式事务管理

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。