ssm框架整合——整体流程

一、当服务器构建的时候父容器加载

当父容器加载完毕,我们ssm框架中所有的service以及数据库配置和链接就已经配置好了。
我们看一下父容器配置文件中相应的配置代码

1)首先是扫描service
<context:component-scan base-package="jee.pk1">
    <!-- 不扫描带有@controller注解的bean -->
    	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

这时候我们ssm框架中的带有@service注解的类就会放入父容器中。
如下面这个service

package jee.pk1;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {
	
	private SqlSession sqlSession;
	//注入sqlsession
	@Autowired
	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}
	
	public User getUser (int id ) {
		return sqlSession.getMapper(UserMapper.class).get(id);
	}
	//声明事务
	@Transactional
	public int delete(int id )
	{
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		userMapper.delete(1);
		if(1==1) {
			throw new RuntimeException("自定义异常");
		}
		userMapper.delete(1);
		return 1;
	}

}

服务类需要的数据库服务

首先是映射接口类

package jee.pk1;

public interface UserMapper {
	public User get(int id );
	
	public int delete (int id );
}

下面是映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
<mapper  namespace="jee.pk1.UserMapper">

	<resultMap type="jee.pk1.User" id="userResultMap">
		<id property="id" column="u_id"/>
		<result property="name" column="u_name"/>
		<result property="age" column="u_age"></result>
	</resultMap>
	<select id="get" parameterType="int" resultMap="userResultMap">
		select *  from t_users where u_id =#{id}
	</select>
	
	<delete id="delete">
		delete from t_users where u_id=#{id}
	</delete>
</mapper>
2)在父容器(xmlWebApplicationContext)中加入sqlsession类的bean
<!-- 配置SqlSessionTemplate -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	
二、当外部访问服务的时候
1)子容器加载的时候扫描@controller注解
<!-- 配置controller层的bean,以及viewResolver等等 -->
    <context:component-scan base-package="jee.pk1">
    <!--对扫描类型做限制 , 不扫描带有@Service注解的bean-->
    	<context:exclude-filter  type="annotation" expression="org.springframework.stereotype.Service"/>	
    </context:component-scan>
    
    <!-- 激活Controller中的注解 -->
    <mvc:annotation-driven></mvc:annotation-driven>
2)子容器拦截访问

从web.xml配置文件中可以看出子容器拦截所有的访问

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
3)子容器根据访问的地址分配给相应的控制器(controller)

假如我们访问的地址是
localhost:8080/day14/user/get?id=1
则会调取如下服务

package jee.pk1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {
	private UserService userService;
	//先从子容器中寻找UserService,如果未在子容器中找到,则从父容器中寻找
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	@GetMapping("/get")
	public String get(int id ,Model model) {
		User user=userService.getUser(id);
		model.addAttribute("user",user);
		return "userDetail";
	}
}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值