<?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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--SpringMVc的配置文件,包含网站跳转逻辑的控制,配置 -->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<!--只扫描控制器 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 将springmvc不能处理的请求交给tomcat,访问动态资源和静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
上图是springmvc.xml的配置。
下面的代码段是spring-context.xml的配置
<?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: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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- Spring的配置文件,这里主要配置和业务逻辑相关的-->
<!-- 数据源,事务控制,xxx -->
<!-- 加载非controller的包-->
<context:component-scan base-package="com.horse">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--1. 加载数据库配置文件 -->
<context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
<!--2. 配置数据源 -->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3. 配置spring和Mybatis的整合-sqlSessionFactory-->
<!--3.1 配置使用mybatis操作数据库 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--mybatis全局配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。 配置文档的顶层结构如下:
configuration(配置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)
指导着MyBatis进行工作,
但spring整合mybatis时拆分成多部分,包含全局配置,数据源,mappers等 -->
<!--1.全局配置文件的位置: 设置(settings)-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--2. 数据库连接池 properties-->
<property name="dataSource" ref="druidDataSource"></property>
<!--指定mybatis,mapper文件的位置,有此项配置就不用把mapper.xml和Mapper文件配置在统一路径下了 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--3.2 配置扫描器,将mybatis接口实现加入到ioc容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有Dao接口的实现,加入到ioc容器中 -->
<property name="basePackage" value="com.horse.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--4. 配置事务管理器-->
<!--4.1 配置事务控制,配置事务管理器,让它控制住数据源里面的连接的关闭和提交-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制住数据源-->
<property name="dataSource" ref="druidDataSource"></property>
</bean>
<!--4.2 开启基于注解的事务,使用xml配置形式的事务(比较主要的都使用配置式) -->
<aop:config>
<!--切入点表达式 -->
<aop:pointcut expression="execution(* com.horse.service..*(..))" id="txPoint"></aop:pointcut>
<!--配置事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"></aop:advisor>
</aop:config>
<!--4.3 配置事务增强,事务如何切入 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--*代表所有方法都是事务方法 -->
<tx:method name="*"/>
<tx:method name="get*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
</beans>
spring-context.xml 文件 -- Spring的配置文件,这里主要配置和业务逻辑相关的,包括数据源(数据库连接池,整合mybatis),事务控制等等,java 的bean--org.mybatis.spring.SqlSessionFactoryBean,SqlSessionFactoryBean的三个属性又拆分mybatis的全局配置文件为:新的全局配置文件-
classpath:mybatis-config.xml,数据库连接池-
dataSource,以及mapper的位置。
下篇文章就来讲解SqlSessionFactoryBean的三个属性中的mybatis的全局配置文件,以及mapper