You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
5.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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>