SSM整合流程、脑图

近日学习了SSM框架,并使用牛客网上的项目实战练了练手,为了方便日后学习,所以将SSM整合流程整理如下:

整体流程

在这里插入图片描述

编写实体类

注意事项:构造函数,get,set方法等,使用IDEA自带的generator选项生成

在这里插入图片描述

DAO层

编写Dao类

注意,要在dao类上方加入@Component注解。dao类的实现要写成接口类,贴合编程规范。

Spring整合Mybatis:

配置全局:

<?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>
    <!-- 配置全局属性 -->
    <settings>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键的值 -->
        <setting name="useGeneratedKeys" value="true"/>

        <!-- 使用列别名替换列名 默认:true -->
        <setting name="useColumnLabel" value="true"/>

        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

在这里插入图片描述
编写Mapper文件:
id为dao函数名称,<insert></insert>为插入类型操作,同理还有<select></select><update></update>操作,resultType为返回类型。

<insert id="insertAppointment"></insert>
<select id="queryAll" resultType="Book"></select>

Service层

编写Service类

编写Service类的接口,然后建同级包impl(编程规范),一定要注意要在web类上加上@Service注解。

编写Service配置文件

在这里插入图片描述

编写Aop类

Aop可以对Service层方法进行增强,即在基础功能的基础上,增加新的功能。

public class TransactionManagerImpl{
	//确定切面类,连接点等
	//第一个*代表,impl下的所有类
	//第二个*代表,每个类中的所有方法
	//(..)通配任何传入参数类型
    @Pointcut("execution(* com.ykc.service.impl.*.*(..))")
    public void pointCut(){}

//    @Before("pointCut()")
    public void before() {
        System.out.println("前置通知");
    }
//    @AfterReturning("pointCut()")
    public void afterReturning(){
        System.out.println("后置通知");
    }

//    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        System.out.println("异常通知");
    }

//    @After("pointCut()")
    public void after(){
        System.out.println("最终通知");
    }

    @Around("pointCut()")
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
        //pjp即当前拦截的切点即方法
        //ProceedingJoinPoint仅用在环绕通知中
        Object rtValue = null;
        try{
            Object[] args=pjp.getArgs();//获取方法执行所需要的参数,即获取方法输入的参数

            before();

            rtValue=pjp.proceed(args);//执行切点方法并返回输出值
//            System.out.println(args[0]+","+args[1]);
//            System.out.println(rtValue);
            afterReturning();

            return rtValue;
        }catch (Throwable e){
            afterThrowing();
            throw new RuntimeException(e);
        }finally {
            after();
        }
    }
}

Web层(Controller层)

编写web类

编写web类,一定要注意要在web类上加上@Controller注解。

整合Spring与SpringMVC

在这里插入图片描述

web类测试

使用MockMvc,进行测试,可以避开编写前端代码,实现快速测试。

@RunWith(SpringJUnit4ClassRunner.class)
//配置事务回滚
@TransactionConfiguration(transactionManager = "transactionManager",defaultRollback = true)
@Transactional
public class BookControllerTest extends AbstractContextControllerTests{
    private MockMvc mockMvc;
    private String listUrl="/book/list";
    private String detailUrl="/book/{bookId}/detail";
    private String appointUrl="/book/{bookId}/appoint";
    private long bookId = 1000;

    @Before
    public void setup(){
        this.mockMvc=webAppContextSetup(this.wac).alwaysExpect(status().isOk()).alwaysDo(print()).build();
    }

    @Test
    //测试dao.getAllBook()
    public void list() throws Exception {
        this.mockMvc.perform(get(listUrl)).andExpect(view().name("list"));
    }

    @Test
    public void existDetail() throws Exception {
        this.mockMvc.perform(get(detailUrl,bookId)).andExpect(view().name("detail"))
                .andExpect(model().attributeExists("book"));
    }

    @Test
    public void notExistDetail() throws Exception{
        this.mockMvc.perform(get(detailUrl,1100)).andExpect(forwardedUrl("/book/list"));
    }

    @Test
    public void appointTest() throws Exception{
        this.mockMvc.perform(post(appointUrl,bookId).param("studentId","1").accept(MediaType.APPLICATION_JSON))
                .andExpect(content().contentType("application/json;charset=utf-8"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本文档压缩了SSM三大框架的学习思维导图,可以帮您更方便更高效的学习,资源制作过程相当繁琐,好资源且下且珍惜吧。 SSMSpring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。 Spring   Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。   Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。   SpringMVC   SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。   mybatis   mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。 页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值