ssm整合后的使用

一. ssm中添加分页控件

1.添加jar包

<!-- 添加分页插件的依赖包 -->
<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>5.1.2</version>
</dependency>

2.配置分页拦截器

mybatis.xml文件中添加

<!--在版本5之前的配置方式 -->
<!-- 
<plugins>
    <plugin interceptor="com.github.pagehelper.PageHelper">
         <property name="dialect" value="mysql" />
    </plugin>
</plugins>
-->
<!-- 在版本5之后的配置方式-->
<plugins>
   <plugin interceptor="com.github.pagehelper.PageInterceptor" />
</plugins> 

3.在service层添加一个分页方法

//分页查询
@Override
public PageInfo<Items> selectItems(int pageindex, int pagesize) {

    //开启分页  在后面调用查询语句的时候会加入limit
    PageHelper.startPage(pageindex,pagesize);

    //调用查询所有的方法  (会被加入limit)
    List<Items> itemsList=  itemsMapper.selectItems();

    //组装pageinfo对象返回
    PageInfo<Items> pageInfo=new PageInfo<Items>(itemsList);

    return pageInfo;


}

4.在控制层调用分页方法

@RequestMapping("/selectItemsListByPage")
public ModelAndView selectItemsListByPage(@RequestParam(value ="pageindex",defaultValue ="1" ) int pageindex)
{

    PageInfo<Items> pageInfo = itemsService.selectItems(pageindex,3);

    ModelAndView modelAndView=new ModelAndView();

    modelAndView.addObject("pageInfo",pageInfo);

    modelAndView.setViewName("showItemsByPage");

    return modelAndView;



}

5.创建showItemsByPage.jsp显示

<c:forEach var="items" items="${pageInfo.list}">
    ${items.name}<br/>
</c:forEach>

总共查询到<span>${pageInfo.total}</span>条记录,
共<span>${pageInfo.pages}</span>页
当前是第<span>${pageInfo.pageNum}</span>页

<c:forEach var="i" begin="1" end="${pageInfo.pages}" >
    <a href="selectItemsListByPage?pageindex=${i}">${i}</a>&nbsp;&nbsp;
</c:forEach>

二 .ssm中添加aop

1.导入jar

<!-- 导入aop jar -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.10</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>4.1.3.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>4.1.3.RELEASE</version>
</dependency>

2.创建切面类

package com.test.aspects;

import net.sf.jsqlparser.statement.execute.Execute;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.beans.Expression;
import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
public class TimeAspects {

    @Pointcut("execution(* com.test.service.impl.ItemsService.select*(..))")
    public void point()
    {}

    @Before("TimeAspects.point()")
    public void before()
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:ms");

        System.out.println("开始执行的时间:"+simpleDateFormat.format(new Date()));
    }

    @AfterReturning("execution(* com.test.service.impl.ItemsService.select*(..))")
    public void afterReturn()
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:ms");

        System.out.println("成功执行的时间:"+simpleDateFormat.format(new Date()));
    }

}

3.创建aop的配置文件

applicationContext-aop.xml

因为扫描包的配置已经在application-ioc.xml文件中配置过,这里不需要重复

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 创建切面类对象 -->
    <bean id="timeAspects" class="com.test.aspects.TimeAspects" />

    <!-- 开启 aop 自动代理
        做织入操作
     -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


</beans>

4.修改web.xml文件中的spring配置文件的导入

改为applicationContext-*.xml

<!-- 配置spring...xml文件的路径 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext-*.xml</param-value>
</context-param>

<!-- 配置context 加载的监听器   -->
<!-- 加载spring 的容器 -->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

5.运行

http://localhost:8080/testssm/items/selectItemsListByPage

因为这个方法中会调用service层的selectItemsListByPage方法,符合切点,所以会织入进来

。。。

成功执行的时间:2019-06-26 04:13:12:1312

posted @ 2022-10-30 23:10  呆萌老师  阅读(38)  评论(0编辑  收藏  举报