springmvc详解(二):springmvc和mybatis整合
目录
springmvc详解(二):springmvc和mybatis整合
3.1.2、dao层spring配置文件applicationContext-dao.xml
3.2.1、service层spring配置文件applicationContext-dao.xml
3.2.2、spring管理事务的配置文件applicationContext-transaction.xml
一、环境准备
1.1、案例需求
使用springmvc和mybatis完成商品列表查询。
1.2、导入jar包
参照springmvc详解(一):入门程序这篇博客中的2.2部分。
1.3、工程结构
二、整合思路
springmvc+mybaits的系统架构:
Spring在进行管理时,是很有条理的,每个层都由Spring管理。对于表现层,通过spring管理表现层Handler;对于业务层,通过spring管理业务层service;对于持久层,通过spring管理持久层的mapper。同时遵循外层向里的调用逻辑:Handler中可以调用service接口,service中可以调用mapper接口。
整合步骤为:
第一步:整合dao层
mybatis和spring整合,通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
第二步:整合service层
通过spring管理 service接口。使用注解或配置方式将service接口配置在spring配置文件中。
实现事务控制
第三步:整合springmvc
由于springmvc是spring的模块,不需要整合。
三、三层整合
3.1、整合dao层
3.1.1、mybatis配置文件
在mybatis自己的配置文件sqlMapConfig.xml中实现别名定义、缓存设置等配置。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局setting配置,根据需要添加 -->
<!-- 配置别名 -->
<typeAliases>
<!-- 批量扫描别名 -->
<package name="cn.itcast.ssm.po"/>
</typeAliases>
<!-- 配置mapper
由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
必须遵循:mapper.xml和mapper.java文件同名且在一个目录
-->
</configuration>
3.1.2、dao层spring配置文件applicationContext-dao.xml
在applicationContext-dao.xml中完成数据源、SqlSessionFactory、mapper扫描器的配置。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<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.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>
<!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中
自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描的包名
如果扫描多个包,每个包中间使用半角逗号分隔
注意:jdk1.7和spring3的jar包兼容,jdk1.8及以上和spring3的jar包不兼容!和spring4的jar包兼容
-->
<property name="basePackage" value="cn.itcast.ssm.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
3.2、整合service层
3.2.1、service层spring配置文件applicationContext-service.xml
在applicationContext-service.xml中管理service接口实现类的bean
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
h