更新时间:2022-01-10 10:22:46 来源:极悦 浏览1310次
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。
(1)项目结构
(2)PageBean.java
package cn.hwd.paging.bean;
import java.util.List;
public class PageBean<T> {
private int pc;// 当前页码page code
// private int tp;// 总页数total page
private int tr;// 总记录数total record
private int ps;// 每页记录数page size
private List<T> list;// 当前页的记录
public int getPc() {
return pc;
}
public void setPc(int pc) {
this.pc = pc;
}
/**
* 计算总页数
* @return
*/
public int getTp() {
// 通过总记录数和每页记录数来计算总页数
int tp = tr / ps;
return tr%ps==0 ? tp : tp+1;
}
// public void setTp(int tp) {
// this.tp = tp;
// }
public int getTr() {
return tr;
}
public void setTr(int tr) {
this.tr = tr;
}
public int getPs() {
return ps;
}
public void setPs(int ps) {
this.ps = ps;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
(3)User.java
package cn.hwd.paging.bean;
import java.util.Date;
public class User {
private Integer id;
private String name;
private Integer age;
private Date birth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer id, String name, Integer age, Date birth) {
super();
this.id = id;
this.name = name;
this.age = age;
this.birth = birth;
}
}
(4)UserController.java
package cn.hwd.paging.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.hwd.paging.bean.PageBean;
import cn.hwd.paging.bean.User;
import cn.hwd.paging.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/index.action")
public String index(
@RequestParam(value="pc", required=true, defaultValue="1") Integer pc,
Model model) throws Exception {
PageBean<User> pb = new PageBean<User>();
pb.setPc(pc);
int ps = 10;
pb.setPs(ps);
int tr = userService.count();
pb.setTr(tr);
Map<String, Object> map = new HashMap<String, Object>();
map.put("m", (pc - 1) * ps);
map.put("n", ps);
List<User> users = userService.findAll(map);
pb.setList(users);
model.addAttribute("pb", pb);
return "index";
}
}
(5)UserMapper.java
package cn.hwd.paging.mapper;
import java.util.List;
import java.util.Map;
import cn.hwd.paging.bean.User;
public interface UserMapper {
public Integer count() throws Exception;
public List<User> findAll(Map<String, Object> map) throws Exception;
}
(6)UserMapper.xml
<?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="cn.hwd.paging.mapper.UserMapper">
<resultMap id="userResultMap" type="user">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="birth" property="birth"/>
</resultMap>
<select id="count" resultType="int">
select count(*) from user
</select>
<select id="findAll" parameterType="map" resultMap="userResultMap">
select id,name,age,birth from user limit #{m}, #{n}
</select>
</mapper>
(7)UserService.java
package cn.hwd.paging.service;
import java.util.List;
import java.util.Map;
import cn.hwd.paging.bean.User;
public interface UserService {
public Integer count() throws Exception;
public List<User> findAll(Map<String, Object> map) throws Exception;
}
(8)UserServiceImpl.java
package cn.hwd.paging.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hwd.paging.bean.User;
import cn.hwd.paging.mapper.UserMapper;
import cn.hwd.paging.service.UserService
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public Integer count() throws Exception {
return userMapper.count();
}
@Override
public List<User> findAll(Map<String, Object> map) throws Exception {
return userMapper.findAll(map);
}
}
(9)jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/hwd
jdbc.username=root
jdbc.password=mysql
(10)log4j.properties
# 定义log输出级别
log4j.rootLogger=DEBUG, Console
# 定义log输出为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
# 指定输出格式,其中%p表示日志级别 ,%t表示当前线程名,%m表示日志内容,%n表示换行
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%5p [%t] - %m%n
(11)mybatis-config.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>
<typeAliases>
<package name="cn.hwd.paging.bean" />
</typeAliases>
</configuration>
(12)spring-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="cn.hwd.paging" />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/page/" />
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.hwd.paging.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
(13)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.hwd</groupId>
<artifactId>paging</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>paging Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>paging</finalName>
</build>
(14)index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>paging</title>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>birth</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4">
第${pb.pc }页/共${pb.tp }页
<a href="<c:url value="/user/index.action" />?pc=1">首页</a>
<c:if test="${pb.pc > 1 }">
<a href="<c:url value="/user/index.action" />?pc=${pb.pc - 1 }">上一页</a>
</c:if>
<%-- 计算begin、end --%>
<c:choose>
<%-- 如果总页数不足10页,那么把所有的页数都显示出来! --%>
<c:when test="${pb.tp <= 10 }">
<c:set var="begin" value="1" />
<c:set var="end" value="${pb.tp }" />
</c:when>
<c:otherwise>
<%-- 当总页数>10时,通过公式计算出begin和end --%>
<c:set var="begin" value="${pb.pc-5 }" />
<c:set var="end" value="${pb.pc+4 }" />
<%-- 头溢出 --%>
<c:if test="${begin < 1 }">
<c:set var="begin" value="1" />
<c:set var="end" value="10" />
</c:if>
<%-- 尾溢出 --%>
<c:if test="${end > pb.tp }">
<c:set var="begin" value="${pb.tp - 9 }" />
<c:set var="end" value="${pb.tp }" />
</c:if>
</c:otherwise>
</c:choose>
<%-- 循环遍历页码列表 --%>
<c:forEach var="i" begin="${begin }" end="${end }">
<c:choose>
<c:when test="${i eq pb.pc }">
[${i }]
</c:when>
<c:otherwise>
<a href="<c:url value="/user/index.action" />?pc=${i}">[${i }]</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${pb.pc < pb.tp }">
<a href="<c:url value="/user/index.action" />?pc=${pb.pc + 1 }">下一页</a>
</c:if>
<a href="<c:url value="/user/index.action" />?pc=${pb.tp }">尾页</a>
</td>
</tr>
</tfoot>
<tbody>
<c:forEach items="${pb.list }" var="user">
<tr>
<td>${user.id }</td>
<td>${user.name }</td>
<td>${user.age }</td>
<td><fmt:formatDate value="${user.birth }" pattern="yyyy/MM/dd HH:mm:ss" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
(15)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
(16)运行结果
以上就是关于“SSM框架实例”的介绍,大家如果对此比较感兴趣,可以关注一下极悦的SSM视频教程,里面的课程内容全面细致,由浅到深,通俗易懂,适合没有基础的小白学习,希望对大家能够有所帮助。
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习