您好,登錄后才能下訂單哦!
這篇文章主要介紹“SSM框架如何使用”,在日常操作中,相信很多人在SSM框架如何使用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”SSM框架如何使用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三個(gè)開源框架整合而成,常作為數(shù)據(jù)源較簡(jiǎn)單的web項(xiàng)目的框架。
(1)項(xiàng)目結(jié)構(gòu)
(2)PageBean.java
package cn.hwd.paging.bean;import java.util.List;public class PageBean<T> { private int pc;// 當(dāng)前頁(yè)碼page code// private int tp;// 總頁(yè)數(shù)total page private int tr;// 總記錄數(shù)total record private int ps;// 每頁(yè)記錄數(shù)page size private List<T> list;// 當(dāng)前頁(yè)的記錄 public int getPc() { return pc; } public void setPc(int pc) { this.pc = pc; } /** * 計(jì)算總頁(yè)數(shù) * @return */ public int getTp() { // 通過(guò)總記錄數(shù)和每頁(yè)記錄數(shù)來(lái)計(jì)算總頁(yè)數(shù) 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輸出級(jí)別log4j.rootLogger=DEBUG, Console# 定義log輸出為控制臺(tái)log4j.appender.Console=org.apache.log4j.ConsoleAppender# 指定輸出格式,其中%p表示日志級(jí)別 ,%t表示當(dāng)前線程名,%m表示日志內(nèi)容,%n表示換行l(wèi)og4j.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" /> <!-- 數(shù)據(jù)源 --> <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 }頁(yè)/共${pb.tp }頁(yè) <a href="<c:url value="/user/index.action" />?pc=1">首頁(yè)</a> <c:if test="${pb.pc > 1 }"> <a href="<c:url value="/user/index.action" />?pc=${pb.pc - 1 }">上一頁(yè)</a> </c:if> <%-- 計(jì)算begin、end --%> <c:choose> <%-- 如果總頁(yè)數(shù)不足10頁(yè),那么把所有的頁(yè)數(shù)都顯示出來(lái)! --%> <c:when test="${pb.tp <= 10 }"> <c:set var="begin" value="1" /> <c:set var="end" value="${pb.tp }" /> </c:when> <c:otherwise> <%-- 當(dāng)總頁(yè)數(shù)>10時(shí),通過(guò)公式計(jì)算出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> <%-- 循環(huán)遍歷頁(yè)碼列表 --%> <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 }">下一頁(yè)</a> </c:if> <a href="<c:url value="/user/index.action" />?pc=${pb.tp }">尾頁(yè)</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>
到此,關(guān)于“SSM框架如何使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。