SpringMVC学习--springmvc和mybatis整合

Posted ngulc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC学习--springmvc和mybatis整合相关的知识,希望对你有一定的参考价值。

  • 简介

  springMVC是表现层,service充当业务层,mybatis作为持久层,通过spring将这三层整合起来。如下图:

  第一步:整合dao

  mybatisspring整合,通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在spring中进行注册。

  第二步:整合service

  通过spring管理 service接口,使用配置方式将service接口配置在spring配置文件中,实现事务控制。

  第三步:整合springmvc

  由于springmvcspring的模块,不需要整合。

  • 整合Dao层

  也就是Spring与MyBatis的整合

  1、MyBatis的配置:SqlMapConfig.xml

 

1 <configuration>
2     <typeAliases>
3         <!-- 批量别名定义,扫描整个包下的类,别名为类名(首字母大写或小写都可以) -->
4         <package name="com.luchao.pojo" />
5     </typeAliases>    
6 </configuration>

 

  2、Dao的配置:applicationContext-dao.xml

  配置数据源、SqlSessionFactory、Mapper扫描器

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15     <!-- 加载配置文件 -->
16     <context:property-placeholder location="classpath:db.properties" />
17     <!-- 数据源,使用dbcp -->
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
19         destroy-method="close">
20         <property name="driverClassName" value="${jdbc.driver}" />
21         <property name="url" value="${jdbc.url}" />
22         <property name="username" value="${jdbc.username}" />
23         <property name="password" value="${jdbc.password}" />
24         <property name="maxActive" value="10" />
25         <property name="maxIdle" value="5" />
26     </bean>
27     <!-- sqlSessinFactory -->
28     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
29         <!-- 加载mybatis的配置文件 -->
30         <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
31         <!-- 数据源 -->
32         <property name="dataSource" ref="dataSource" />
33     </bean>
34     <!-- 配置mapper扫描器 -->
35     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
36         <property name="basePackage" value="com.luchao.mapper"></property>
37         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
38     </bean>
39 </beans>

  3、定于Po、Mapper接口和Mapper映射文件。

  • 整合Service

  让spring管理service

  定于service接口:

1 public interface ItemsService {
2     // 获取商品列表
3     public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
4             throws Exception;
5 }

  service实现类:

 1 public class ItemsServiceImpl implements ItemsService {
 2 
 3     @Autowired
 4     private ItemsMapperCustom itemsMapperCustom;
 5 
 6     @Override
 7     public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
 8             throws Exception {
 9         // 通过itemsMapperCustom查询数据库
10         return itemsMapperCustom.findItemsList(itemsQueryVo);
11     }
12 }

  在spring容器中中配置service:applicationContext-service.xml

1 <bean id="itemsService" class="com.luchao.service.ItemsServiceImpl"></bean>

  事务控制:applicationContext-transaction.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15     <!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 -->
16     <bean id="transactionManager"
17         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
18         <!-- 数据源 dataSource在applicationContext-dao.xml中配置了 -->
19         <property name="dataSource" ref="dataSource" />
20     </bean>
21     
22     <!-- 通知 -->
23     <tx:advice id="txAdvice" transaction-manager="transactionManager">
24         <tx:attributes>
25             <tx:method name="save*" propagation="REQUIRED"/>
26             <tx:method name="delete*" propagation="REQUIRED"/>
27             <tx:method name="insert*" propagation="REQUIRED"/>
28             <tx:method name="update*" propagation="REQUIRED"/>
29             <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
30             <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
31             <tx:method name="select*" propagation="REQUIRED" read-only="true"/>
32         </tx:attributes>
33     </tx:advice>
34     <!-- aop -->
35     <aop:config>
36         <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.luchao.service.*.*(..))"/>
37     </aop:config>
38 </beans>

  通过AOP将事务织入了符合一定条件的方法上面。

  • 整合springMVC

  配置处理器映射器、适配器、视图解析器:springmvc.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15     <!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->
16     <context:component-scan base-package="com.luchao.controller"/>
17     <mvc:annotation-driven/>
18     <!-- 视图解析器 -->
19     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
21         <property name="prefix" value="/WEB-INF/jsp/"/>
22         <property name="suffix" value=".jsp"/>
23     </bean>
24 </beans>

  配置前端控制器:web.xml中加入:

 1 <servlet>
 2         <servlet-name>springmvc</servlet-name>
 3         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4         <init-param>
 5             <param-name>contextConfigLocation</param-name>
 6             <param-value>classpath:spring/springmvc.xml</param-value>
 7         </init-param>
 8         <load-on-startup>1</load-on-startup>
 9     </servlet>
10     <servlet-mapping>
11         <servlet-name>springmvc</servlet-name>
12         <url-pattern>/</url-pattern>
13     </servlet-mapping>

  编写控制器:

 1 @Controller
 2 @RequestMapping("/items")
 3 public class ItemController {
 4     @Autowired
 5     private ItemsService itemsService;
 6 
 7     // 商品查询
 8     @RequestMapping("/queryItems")
 9     public ModelAndView queryItems() throws Exception {
10         // 调用service查找 数据库,查询商品列表
11         List<ItemsCustom> itemsList = itemsService.findItemsList(null);
12 
13         // 返回ModelAndView
14         ModelAndView modelAndView = new ModelAndView();
15         // 相当 于request的setAttribut,在jsp页面中通过itemsList取数据
16         modelAndView.addObject("itemsList", itemsList);
17 
18         // 指定视图
19         // 下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为
20         // modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
21         // 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀
22         modelAndView.setViewName("items/itemsList");
23 
24         return modelAndView;
25     }
26 }

  加载spring容器:

  将mapperservicecontroller加载到spring容器中。web.xml中,添加spring容器监听器,加载spring容器。

1 <!-- 加载spring容器 -->
2     <context-param>
3         <param-name>contextConfigLocation</param-name>
4         <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
5     </context-param>
6     <listener>
7         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
8     </listener>

以上是关于SpringMVC学习--springmvc和mybatis整合的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVC框架详解(学习总结)

SpringMVC学习--springmvc和mybatis整合

springMVC学习总结 --springMVC表单处理和静态文件处理

SpringMVC源码分析2:SpringMVC设计理念与DispatcherServlet

SpringMVC学习--springmvc和mybatis整合

SpringMVC学习04SpringMyBatis和SpringMVC的整合