首页 > 技术文章 > ssm的基础配置文件介绍

Index-D 2020-06-05 00:12 原文

<?xml version="1.0" encoding="UTF-8"?>
<!--beans中不仅要有给出源数据的位置、相关参数的值,还要给出spring-context等项目的位置-->
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--开启@ Resource 、@ PostConstruct、@ PreDestroy、@PersistenceContext、@Autowired、@Required等注解-->
    <!--此句也可不加,因为<context:compoent-scan>配置也有开启这些注解的作用-->
    <context:annotation-config/>
    <!--上下文组件扫描,通过搜索标注的类,把各种对象托管给Spring-->
    <context:component-scan base-package="com.myssm"/>

    <!--设置连接数据库的数据源到对象连接池-->
    <!--数据源包括驱动位置,库地址,用户名和密码-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.56.100:3306/mydemo"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--配置数据源会话工厂,整合spring和mybatis-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--绑定数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--配置实体别名-->
        <property name="typeAliasesPackage" value="main.pro.pojo"/>
        <!--配置mapper.xml文件夹-->
        <property name="mapperLocations" value="classpath:mapping/*Mapper.xml"/>
    </bean>

    <!--映射dao接口,会创建所有的对应接口对象-->
    <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定数据库会话工厂-->
        <property name="sqlSessionFactoryBeanName" value="SessionFactory"/>
        <!--指定Mapper接口包路径-->
        <property name="basePackage" value="com.myssm.dao"/>
    </bean>

    <!--开启自动事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--启用事务的注解方式,添加事务对应的bean对象-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--启用mvc的注解方式,开启ajax json返回数据-->
    <mvc:annotation-driven/>
</beans>

 

推荐阅读