ssm框架学习---springmvc和mybatis整合

12 篇文章 0 订阅
3 篇文章 0 订阅

1.前端控制器(DispatcherServlet)

2.处理器映射器(handlermapping)

3.处理器适配器(handlerAdaper)

4.视图解析器(ViewerResolver)

需要使用spring的jar包,springmvc的jar包

目录结构如下


首先配置前端控制器:在web.xml中

配置如下:

<!-- springmvc前端控制器 ,里面配置处理器映射器和处理器适配器-->
    <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/springmvc.xml</param-value>
      </init-param>
    </servlet>	
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
里面指定了和它打交道的处理器映射器,处理器适配器和视图解析器的配置文件在springmvc.xml中,同时设置了url的格式

接下来配置springmvc,中间包括处理器映射器等和待会要编写的handler的bean,也就是目录中controller包下的类

如下图所示

<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 ">
		
		
		<!-- 配置controller的bean,这里使用组件扫描 -->
		<context:component-scan base-package="com.zcj.ssm.controller"></context:component-scan>
		
		<!-- 配置处理器映射器和处理器适配器 -->
		<mvc:annotation-driven></mvc:annotation-driven>
		
		<!-- 配置视图解析器 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		     <!-- 配置前缀和后缀 -->
		     <property name="prefix" value="/WEB-INF/jsp/"></property>
		     <property name="suffix" value=".jsp"></property>
		</bean>
</beans>
从配置文件中可以看出已经将整个controller 配置到里面去了,并且这里使用了注解的处理器和映射器,因此controller类的编写时基于注解的

如下:

package com.zcj.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zcj.ssm.po.User;
import com.zcj.ssm.service.UserService;

@Controller
public class UserController {
     @Autowired
     UserService userService;
     @RequestMapping("/userslist")
     public ModelAndView findUserById(){
    	 User user3= userService.findUserById(1);
    	 List<User> list = new ArrayList<User>();
    	 User user1= new User();
    	 user1.setUsername("zcj");
    	 User user2 = new User();
    	 user2.setUsername("test");
    	 list.add(user1);
    	 list.add(user2);
    	 list.add(user3);
    	 ModelAndView modelAndView = new ModelAndView();
    	 modelAndView.addObject("userslist",list);
    	 modelAndView.setViewName("userinfo/usersList");
    	 return modelAndView;
     }
}
上面的配置文件中使用了扫描方式,因此这里@Controller注解用于指定该类是一个handler,路由的配置使用@RequestMapping,从该类中可以看出使用到了service层

如下:

package com.zcj.ssm.service;

import org.springframework.stereotype.Service;

import com.zcj.ssm.po.User;

public interface UserService {
    public User findUserById(int id);
}
实现类:

package com.zcj.ssm.serviceimpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcj.ssm.mapper.UserMapper;
import com.zcj.ssm.po.User;
import com.zcj.ssm.service.UserService;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
	public User findUserById(int id) {
		// TODO Auto-generated method stub
		User user = userMapper.selectByPrimaryKey(id);
		return user;
	}

}
同样这里的service类也需要放到spring容器中,使用配置文件applicationContext-service.xml如下

<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 ">
    <!-- 可以扫描controller、service、...
	-->
	<context:component-scan base-package="com.zcj.ssm.serviceimpl"></context:component-scan>	
</beans>
同样的使用了扫描器,因此类的编写使用到了@Service注解,从实现类中可以看到使用了持久层中的mapper,再向下走和前一篇文章就一致了,这里不再写了,为了增加事务管理,为事务管理配置applicationContext-transaction.xml如下

<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 ">
		<!-- 配置事务管理器 -->
		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		     <!--数据源已经在applicationContext-dao中进行了配置 -->
		     <property name="dataSource" ref="dataSource"></property>
		</bean>
		<!-- 配置通知 -->
		<tx:advice id="txAdvice" transaction-manager="transactionManager">
		    <tx:attributes>
				<!-- 传播行为 -->
				<tx:method name="save*" propagation="REQUIRED"/>
				<tx:method name="delete*" propagation="REQUIRED"/>
				<tx:method name="insert*" propagation="REQUIRED"/>
				<tx:method name="update*" propagation="REQUIRED"/>
				<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
				<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
				<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
			</tx:attributes>
		</tx:advice>
		<!-- aop -->
		<aop:config>
		    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zcj.ssm.serviceimpl.*.*(..))"/>
		</aop:config>
</beans>
最后我们需要将页面和spring整合起来,再web.xml中将中间的配置文件放入spring容器中如下:

<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

到这里整个整合过程就完成了,下面具体去调试,消除里面的错误,

看到成功结果如下:


从上面编写的controller代码中,我自己手动添加了二条数据,最后一条上图中红色部分为从数据库中读出来的,由此看出整个过程配置成功







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值