SSM框架简单实例

1、SSM框架

SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。

2、简单实例

(1)项目结构

SSM框架简单实例_第1张图片

SSM框架简单实例_第2张图片

(2)PageBean.java

package cn.hwd.paging.bean;

import java.util.List;

public class PageBean {
	
	private int pc;// 当前页码page code
//	private int tp;// 总页数total page
	private int tr;// 总记录数total record
	private int ps;// 每页记录数page size
	private List 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 getList() {
		return list;
	}
	public void setList(List 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 pb = new PageBean();
		pb.setPc(pc);
		int ps = 10;
		pb.setPs(ps);
		int tr = userService.count();
		pb.setTr(tr);
		Map map = new HashMap();
		map.put("m", (pc - 1) * ps);
		map.put("n", ps);
		List 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 findAll(Map map) throws Exception; 
	
}

(6)UserMapper.xml

  



	
	    
	    
	    
	      
    

	
  	
	
  	

(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 findAll(Map 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 findAll(Map 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





	
		
	
	

(12)spring-servlet.xml



    
    
    
	
	
		
		
		
		
	
	
	

    
        
        
        
        
    
    
    
        
        
        
        
    
    
    
    	
    	
    
	

(13)pom.xml


  4.0.0
  cn.hwd
  paging
  war
  0.0.1-SNAPSHOT
  paging Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      4.8.1
    
    
	  javax.servlet
	  servlet-api
	  2.5
	
	
  	  org.mybatis
  	  mybatis
  	  3.2.6
  	
  	
	  mysql
	  mysql-connector-java
	  5.1.30
	
  	
  	  log4j
  	  log4j
  	  1.2.16
  	
  	
	  org.springframework
	  spring-webmvc
	  4.0.2.RELEASE
	
	
  	  org.mybatis
  	  mybatis-spring
  	  1.2.2
  	
  	
  	  commons-dbcp
  	  commons-dbcp
  	  1.2.2
  	
  	
  	  org.springframework
  	  spring-jdbc
  	  4.0.2.RELEASE
  	
  	  
      javax.servlet  
      jstl  
      1.2  
      
      
      taglibs  
      standard  
      1.1.2  
    
  
  
    paging
  

(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" %>




paging


	
				
id name age birth
第${pb.pc }页/共${pb.tp }页 ?pc=1">首页 ?pc=${pb.pc - 1 }">上一页 <%-- 计算begin、end --%> <%-- 如果总页数不足10页,那么把所有的页数都显示出来! --%> <%-- 当总页数>10时,通过公式计算出begin和end --%> <%-- 头溢出 --%> <%-- 尾溢出 --%> <%-- 循环遍历页码列表 --%> [${i }] ?pc=${i}">[${i }] ?pc=${pb.pc + 1 }">下一页 ?pc=${pb.tp }">尾页
${user.id } ${user.name } ${user.age }

(15)web.xml

  
  
  Archetype Created Web Application
    
    index.html  
    index.htm  
    index.jsp  
    default.html  
    default.htm  
    default.jsp  
    
    
    
    spring  
    org.springframework.web.servlet.DispatcherServlet 
    
      contextConfigLocation
      classpath:spring-servlet.xml
     
    
    
    spring  
    *.action  
  
 

(16)运行结果

SSM框架简单实例_第3张图片

SSM框架简单实例_第4张图片

SSM框架简单实例_第5张图片

你可能感兴趣的:(SSM)