SSM框架整合思路+PageHelper使用

本文详细介绍了如何整合Spring、SpringMVC和Mybatis(SSM)框架,包括各层配置和web.xml设置。同时,讨论了PageHelper分页插件的使用,包括其工作原理和配置步骤。此外,还提到了EasyUI的分页参数和返回值格式,并分享了在使用maven的tomcat插件进行调试的经验。
摘要由CSDN通过智能技术生成

概述

是时候系统的整理整理零碎了。。。

一、SSM框架整合

1.Dao层

使用mybatis框架。创建SqlMapConfig.xml。

创建一个applicationContext-dao.xml

1、配置数据源

2、需要让spring容器管理SqlsessionFactory,单例存在。

3、把mapper的代理对象放到spring容器中。使用扫描包的方式加载mapper的代理对象。

整合内容 对应工程
Pojo Taotao-mangaer-pojo
Mapper映射文件 Taotao-mangaer-mapper
Mapper接口 Taotao-mangaer-mapper
sqlmapConfig.xml Taotao-manager-web
applicationContext-dao.xml Taotao-manager-web

2.Service层

1、事务管理

2、需要把service实现类对象放到spring容器中管理。这里扫描@Service

整合内容 对应工程
Service接口及实现类 Taotao-mangaer-service
applicationContext-service.xml Taotao-manager-web
applicationContext-trans.xml Taotao-manager-web

3.表现层

1、配置注解驱动

2、配置视图解析器

3、需要扫描controller

4、由于前端控制器会将所有的(包括.css,.js)都进入springmvc,所以我们还需要在springmvc.xml配置资源映射

4.web.xml

1、spring容器的配置

2、Springmvc前端控制器的配置

3、Post乱码过滤器

5.springmvc和spring的父子容器关系

这里很多人都会有疑问,为什么@service扫描用application.xml配置,@controller扫描用springmvc.xml配置,两个为什么非得分开,我们直接扫描一个父包不就好了吗?这里我们需要引入这两个的父子容器关系来解释说明。

image-20200517202839189

如果我们在Spring容器中配置了全局扫描,那么Controller对象就在Spring里面了,即springmvc中没有对象了,当他的处理器映射器工作时找不到注解,报404错误。

二、PageHelper使用

1.介绍

如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

官方网站:https://github.com/pagehelper/Mybatis-PageHelper/tree/master/src/main/java/com/github/pagehelper

2.实现原理

image-20200517202132431

img

3.使用方法

第一步:引入pageHelper的jar包。

第二步:需要在SqlMapConfig.xml中配置插件。

<?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>
	<!-- 配置分页插件 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
        	<property name="dialect" value="mysql"/>
		</plugin>
	</plugins>
</configuration>

第三步:在查询的sql语句执行之前,添加一行代码:PageHelper.startPage(1, 10);

第一个参数是page,要显示第几页。第二个参数是rows,没页显示的记录数。

第四步:取查询结果的总数量。

创建一个PageInfo类的对象,从对象中取分页信息。

三、Extra

1.EasyUI

image-20200517203845550

1、请求的参数:http://localhost:8080/item/list?page=1&rows=30 分页信息。(需要看官方的手册)

2、返回值。Json数据。数据格式:

Easyui中datagrid控件要求的数据格式为:

{total:”2”,rows:[{“id”:”1”,”name”,”张三”},{“id”:”2”,”name”,”李四”}]}

所以我们想返回的话需要建一个pojo,pojo应该放到taotao-common工程中(可能都需要使用)。

private long total;
private List<?> rows;//?代表任意类型

这样加上@ResponseBody就能返回我们想要的json–EasyUIDateGrid支持的数据格式

2.使用maven的tomcat插件时debug

img

image-20200517205157891

3.上文详细的代码

篇幅太少,不符合👴的风度,毕竟前两篇全是水文,索性加上代码看起来不那么水(代码仅供参考)

SqlMapConfig.xml

<?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>
	<!-- 配置分页插件 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
        	<property name="dialect" value="mysql"/>
		</plugin>
	</plugins>
</configuration>

applicationContext-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://w
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值