SSM框架搭建实例

SSM框架搭建简单实例 - 搭建步骤


需求分析:

   实现用户表信息的增删改查


1. 开发环境

  • 环境  : JDK  1.8         
  • 软件 : myeclipse       
  • 数据库 : MySql

2. 创建数据库

  • 数据库名称:ssm           
  • 字符集:UTF-8         
  • 表 : user

3. 工程搭建

  •   工程使用Springmvc、spring、mybatis框架整合完成。
  • 项目完整目录截图
  • SSM框架搭建实例_第1张图片

4. 创建web工程

  • 准备需要的jar包
  1. spring(包括springmvc)
  2. mybatis
  3. mybatis-spring整合包
  4. 数据库驱动
  5. 第三方连接池。
  6. Json依赖包(springmvc 自带,可不加)
  • web.xml  文件

       需要配置的有  : Spring监听器, SpringMVC 核心前端控制器, 字符集过滤器



  SSM
  
    index.jsp
  
  
  
  
  	contextConfigLocation
  	classpath:applicationContext.xml
  
  
  	org.springframework.web.context.ContextLoaderListener
  
  
  
  
  	springmvcDispatcherServlet
  	org.springframework.web.servlet.DispatcherServlet
  	
  		contextConfigLocation
  		classpath:springmvc.xml
  	
  
  
  	springmvcDispatcherServlet
  	*.action
  
  
  
  
  	characterEncodingFilter
  	org.springframework.web.filter.CharacterEncodingFilter
  	
  		encoding
  		UTF-8
  	
  
  
  	characterEncodingFilter
  	/*
  
  
  

三个框架的配置文件,统一放在 conf 文件夹下(source folder 文件夹)

  • applicationContext.xml    【Spring 的 配置文件】



	
	
	
	
	
	
	
	
	
	
			
			
			
			
	
	
	
	
		
		
		
		
		
		
	
	
	
	
		
		
		
		
	
		
	
	
		
	
	
	
	
		
		
			
			
			
			
			
			
			
			
			
			
		
	
	
	
	
		
				
	
	
  • springmvc.xml    【SpringMVC 的 配置文件】




	
	
	
	
	
	
	
	
	
	
	
	
	
	
		
		
		
		
	

  • SqlMapConfig.xml    【MyBatis 的 配置文件】




	
	
         
		
         
		
		
		
	
	
	
		
	
	

  • db.properties        【数据库配置文件】

driverClass=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/ssm
user=root
pwd=root
  • log4j.properties    【日志配置文件】

# Global logging configuration
#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216error
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

  • User.java     【实体类 POJO,     所在包:top.vkiss.ssm.domain】

package top.vkiss.ssm.domain;

public class User {
    private Integer uid;
    private String uname;
    private String phone;
    private String address;
    // 这里省略了getter / setter  方法
}
  • UserMapper.java    【dao层接口,     所在包:top.vkiss.ssm.mapper】

package top.vkiss.ssm.mapper;

import java.util.List;

import top.vkiss.ssm.domain.User;

public interface UserMapper {
	
	public void addUser(User user) throws Exception;
	public void delUser(Integer id) throws Exception;
	public void editUser(User user) throws Exception;
	public User getUser(Integer id) throws Exception;
	public List queryAll() throws Exception;

}
  • UserMapper.xml  【dao层mybatis 映射文件,可逆向生成   所在包:top.vkiss.ssm.mapper】





  
    
    
    
    
  
  
  
    uid, uname, phone, address
  
  
  
  
  
  
  
  
  
  
    delete from user
    where uid = #{uid,jdbcType=INTEGER}
  
  
		  
		  
  
  
    insert into user
    
      
        uid,
      
      
        uname,
      
      
        phone,
      
      
        address,
      
    
    
      
        #{uid,jdbcType=INTEGER},
      
      
        #{uname,jdbcType=VARCHAR},
      
      
        #{phone,jdbcType=VARCHAR},
      
      
        #{address,jdbcType=VARCHAR},
      
    
  
  
  
  
    update user
    
      
        uname = #{uname,jdbcType=VARCHAR},
      
      
        phone = #{phone,jdbcType=VARCHAR},
      
      
        address = #{address,jdbcType=VARCHAR},
      
    
    where uid = #{uid,jdbcType=INTEGER}
  

  • UserService.java  【Servioce 层接口 ,     所在包:top.vkiss.ssm.service】

package top.vkiss.ssm.service;

import java.util.List;

import top.vkiss.ssm.domain.User;

public interface UserService {
	public void addUser(User user) throws Exception;
	public void delUser(Integer id) throws Exception;
	public void editUser(User user) throws Exception;
	public User getUser(Integer id) throws Exception;
	public List queryAll() throws Exception;
}
  • UserServiceImpl.java   【Servioce 层实现类 ,     所在包:top.vkiss.ssm.service】

package top.vkiss.ssm.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.vkiss.ssm.domain.User;
import top.vkiss.ssm.mapper.UserMapper;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper ud;
	@Override
	public void addUser(User user) throws Exception {
		// TODO Auto-generated method stub
		ud.addUser(user);
	}

	@Override
	public void delUser(Integer id) throws Exception {
		// TODO Auto-generated method stub
		ud.delUser(id);
	}

	@Override
	public void editUser(User user) throws Exception {
		// TODO Auto-generated method stub
		ud.editUser(user);
	}

	@Override
	public User getUser(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return ud.getUser(id);
	}

	@Override
	public List queryAll() throws Exception {
		// TODO Auto-generated method stub
		return ud.queryAll();
	}

}
  • UserController.java   【Controller 层 接收前端请求 ,     所在包:top.vkiss.ssm.controller】

package top.vkiss.ssm.controller;

import java.util.List;

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 top.vkiss.ssm.domain.User;
import top.vkiss.ssm.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService us;
	
	@RequestMapping("/queryAll")
	public String queryAll(Model model) throws Exception{
		model.addAttribute("uList", us.queryAll());
		return "shouye";
	}
	
	@RequestMapping("/addUser")
	public String addUser(User user) throws Exception {
		System.out.println("addUser:"+user);
		us.addUser(user);
		return "redirect:queryAll.action";
	}
	
	@RequestMapping("/delUser")
	public String delUser(Integer id) throws Exception {
		System.out.println("del_id:"+id);
		us.delUser(id);
		return "redirect:queryAll.action";
	}
	
	@RequestMapping("/delS")
	public String delS(int[] ids) throws Exception {
		System.out.println("del_id:"+ids);
		for (int i : ids) {
			us.delUser(i);
		}
		return "redirect:queryAll.action";
	}
	
	@RequestMapping("/editUser")
	public String editUser(User user) throws Exception {
		System.out.println(user.getUname());
		us.editUser(user);
		return "redirect:queryAll.action";
	}
	
	@RequestMapping("/getUser")
	public String getUser(Model model,Integer id) throws Exception {
		System.out.println(id);
		System.out.println("...."+us.getUser(id));
		model.addAttribute("u", us.getUser(id));
		return "edit";
	}
	
	@RequestMapping("/showAdd")
	public String showAdd() throws Exception {
		return "addUs";
	}
}
  • myTask.java   【其他  层   SpringMVC 定时器 ,     所在包:top.vkiss.ssm.task】

package top.vkiss.ssm.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 *  本项目中没有用到,仅作学习使用
 */


@Component("task")
public class myTask {
	@Scheduled(cron="* * * * * *")
	public void zhh(){
		System.out.println("999999999999999");
	}
}

页面资源

index.jsp   项目启动用于重定向跳转

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
response.sendRedirect("queryAll.action");
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
    This is my JSP page. 

shouye,jsp    列表页 用于展示用户数据

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
	
	
  
  
  
    This Is My First SSM Application. 
UID 姓名 性别 地址 操作
${u.uid } ${u.uname } ${u.phone } ${u.address } 【修改】【删除】

点击添加

addUs.jsp    添加用户页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
	
  
  
  
      

姓名:

性别:

地址:

edit.jsp        修改页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
      

姓名:

性别:

地址:

至此简单SSM项目整合完毕

附带项目源码:SSM项目整合   

所用Jar包 截图

SSM框架搭建实例_第2张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(SSM项目整合)