您好,登錄后才能下訂單哦!
這篇文章主要介紹“SSM框架如何使用”,在日常操作中,相信很多人在SSM框架如何使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SSM框架如何使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
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/hwdjdbc.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>
到此,關于“SSM框架如何使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。