(一)Spring与MyBatis集成
- db.properites
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=admin
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!--
db.properties -> datasource -> SqlSessionFactory ->mapper(接口,xml)->service->controller
Spring集成MyBatis (MyBatis操作数据库的对象交给Spring管理)
1.需要把SqlSessionFactory让Spring来管理
2.数据源(四大金刚) 3.别名的问题 4.可以找到mapper.xml
-->
<context:component-scan base-package="cn.itsource.ssm.service" />
<!--引入外部的db.properties-->
<context:property-placeholder location="classpath:db.properties" />
<!--配置dbcp连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--
还记得以前JPA是怎么搞的? EntityManagerFactoryBean
猜:MyBatis:是否有一个SqlSessionFactoryBean的对象
-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource" />
<!--配置别名-->
<property name="typeAliasesPackage" value="cn.itsource.ssm.domain" />
<!--扫描 mapper.xml-->
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*.xml" />
</bean>
<!--配置对应的MapperBean:只能配置一个Mapper,太多的话就很麻烦-->
<!--
<bean id="departmentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sessionFactory" />
<property name="mapperInterface" value="cn.itsource.ssm.mapper.DepartmentMapper" />
</bean>
-->
<!--一劳永逸的配置Mapper的方案-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描的包的配置-->
<property name="basePackage" value="cn.itsource.ssm.mapper" />
<property name="sqlSessionFactoryBeanName" value="sessionFactory" />
</bean>
<!--配置一个事务对象-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--事务的注解支持-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
(二)Spring与SpringMVC的集成
- applicationContext-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!--扫描相应的controller-->
<context:component-scan base-package="cn.itsource.ssm.controller" />
<!--静态资源放行-->
<mvc:default-servlet-handler />
<!--springMVC的注解支持-->
<mvc:annotation-driven />
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!--
如果我们是在SpringMVC中来引入Spring的xml,那么普通的集成运行没有问题
但是到后期和其它的一些框架集成就无法成功(比如说shiro)
-->
<!--<import resource="classpath:applicationContext.xml" />-->
</beans>
- web.xml
<!--启动Spring的配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--启动SpringMVC的配置-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
注意:不要在mvc的配置文件中去引入spring的核心配置文件(有些框【shiro】架就无法使用)