|
|
<?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:context="http://www.springframework.org/schema/context"
|
|
|
xmlns:tx="http://www.springframework.org/schema/tx"
|
|
|
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
|
|
|
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
|
|
|
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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
|
|
|
|
|
|
<!-- 1.自动扫描包:希望Spring管理所有业务逻辑组件、Bean等,SpringMVC负责网站跳转逻辑的控制(Controller)
|
|
|
注意:①当springmvc.xml配置了只扫描@Controller,此处就要配:
|
|
|
除了标了@Controller注解的控制器以外,都扫描,即配置子标签
|
|
|
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
|
|
|
<context:component-scan base-package="com.ssm">
|
|
|
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
|
|
|
</context:component-scan>
|
|
|
|
|
|
<!-- 2.引入db.properties
|
|
|
注意:①方式二:<bean id="" class="PropertyPlaceholderConfigurer"> -->
|
|
|
<context:property-placeholder location="classpath:db.properties"/>
|
|
|
|
|
|
<!-- 3.引入数据库的数据源配置:
|
|
|
注意:①还可以配置事务控制、AOP等
|
|
|
②当使用c3p0连接池时,配置如下:
|
|
|
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
|
|
|
<property name="driverClass" value="${driver}"></property>
|
|
|
<property name="jdbcUrl" value="${url}"></property>
|
|
|
<property name="user" value="${user}"></property>
|
|
|
<property name="password" value="${password}"></property>
|
|
|
</bean> -->
|
|
|
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
|
|
|
<!-- 这里报错说明类中不存在这个属性,需要改成规定的属性名 -->
|
|
|
<property name="driverClassName" value="${driver}"></property>
|
|
|
<property name="url" value="${url}"></property>
|
|
|
<property name="username" value="${user}"></property>
|
|
|
<property name="password" value="${password}"></property>
|
|
|
</bean>
|
|
|
|
|
|
<!-- 事务管理器DataSourceTransactionManager,该类在spring-jdbc包中,指定这个事务管理器管理配置的dataSource数据源 -->
|
|
|
<!-- <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
|
|
<property name="dataSource" ref="dataSource"></property>
|
|
|
</bean> -->
|
|
|
|
|
|
<!-- 开启基于注解的事务 -->
|
|
|
<!-- <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> -->
|
|
|
|
|
|
<!-- 4.整合mybatis配置:创建MyBatis核心对象SqlSessionFactory(整合关键步骤)
|
|
|
目的:(1)想让Spring管理所有组件,包括mapper
|
|
|
以后Service层要调用Dao层时,只需使用@Autowired注解自动注入即可。
|
|
|
这样就避免了每次操作增删改查之前,需要先获取到SqlSessionFactory->SqlSession->getMapper方法获得动态代理对象,
|
|
|
即接口类的对象,然后才能操作具体增删改查操作。
|
|
|
(2)Spring声明式事务非常强大,想让Spring管理事务。
|
|
|
注意:①SqlSessionFactoryBean类能创建出SqlSessionFactory对象,意味着容器一启动让容器帮我们创建SqlSessionFactory-->
|
|
|
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
|
|
|
<!-- 访问数据库必然需要数据源,管理数据库的连接,提高数据库性能,在数据源中配置使用的连接池 -->
|
|
|
<property name="dataSource" ref="dataSource"></property>
|
|
|
<!-- 加载MyBatis全局配置文件config.xml,如果config.xml中没有东西可以删掉,
|
|
|
但一般建议留下,可以放一些不太常用的配置,如全局参数settings、数据库提供厂商等配置 -->
|
|
|
<!-- <property name="configLocation" value="classpath:config.xml"></property> -->
|
|
|
<!-- 指定SQL映射文件的位置,当SQL映射文件和接口名不一致时,使用该方法指定。
|
|
|
(不讲)?如果名字一致,则可以使用<mybatis:scan base-package=""/>扫描所有mapper SQL映射文件(报错,mybatis前缀未绑定)? -->
|
|
|
<property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"></property>
|
|
|
<!-- 此处还可以配置别名处理器等,这些以前都是在全局配置文件中定义的,现在都变成sqlSessionFactoryBean的一个属性,
|
|
|
相当于mybatis全局配置文件中的内容都拿到Spring配置文件来配置。 -->
|
|
|
</bean>
|
|
|
|
|
|
<!-- 5.扫描所有的mapper接口类,让这些mapper能够自动注入
|
|
|
base-package:指定mapper接口的包名
|
|
|
注意:① 查找类路径下的映射器并自动将它们创建成MapperFactoryBean,
|
|
|
即扫描所有的mapper接口类,让这些mapper能够自动注入
|
|
|
② 如果有红叉报错也可以用,是约束引入有问题,不影响;
|
|
|
③ 以前还有这种写法(老版的项目一般这么做):
|
|
|
a.使用MapperScannerConfigurer
|
|
|
<bean class="MapperScannerConfigurer">
|
|
|
<property name="basePackage" value="Dao接口包名"/>
|
|
|
<bean> 或者
|
|
|
b.采用实体DAO调用方式,采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate
|
|
|
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
|
|
|
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
|
|
|
</bean>
|
|
|
-->
|
|
|
<mybatis-spring:scan base-package="com.ssm.mapper"/>
|
|
|
</beans>
|