基于SSM框架的开发过程及体会

一.SpringMVC和Mybatis整合

1.所需jar包

1.1 Spring(包括SpringMVC)所需jar包

1.2 MyBatis所需jar包

1.3 mybatis-spring整合包

1.4数据库驱动包

1.5第三方数据库连接池

2.整合Dao层

2.1配置

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ehr
jdbc.username=root
jdbc.password=203629

2.2配置

#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = D\:logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
#输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

2.3配置mybatis全局配置文件




	
    
    

2.4配置spring配置文件



	
	
	
	
	 
		
		
		
		
	 
	
	
		
		
		 
		
	
	
		
		
	  	
	
	
	
		
	
	
	

3.整合Service层

3.1配置




3.2配置





	
	




	
		
		
		
		
		
		
		
		
	



	


4.整合表现层

4.1配置处理器映射器、处理器适配器和视图解析




	
	
	

	
	

	
	 
	
	
	
	
	
	
	
	
	
		
		
		
		
	

4.2配置前端控制器



  ehr
  
    RSGL_index.html
    RSGL_index.htm
    RSGL_index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
  	org.springframework.web.context.ContextLoaderListener
  
  
  
  	contextConfigLocation
  	
  	classpath:config/applicationContext.xml
  
  
    
  	org.springframework.web.util.IntrospectorCleanupListener
  
  	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet

		
			contextConfigLocation
			classpath:config/spring-mvc.xml
		
		1
	

	
		springmvc
		/
	

	
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
	
	
		CharacterEncodingFilter
		/*
	

二.具体代码编写流程(以部门管理为例)

1.Dao层代码的编写,生成pojo类及mapper

1.1

package com.neuedu.pojo;

public class Department {
	  private Integer  dp_id;
	  private String dp_name;
	  private Integer dp_tele;
	  private String dp_fd;
	public Department() {
		super();
	}
	public Department(Integer dp_id, String dp_name, Integer dp_tele, String dp_fd) {
		super();
		this.dp_id = dp_id;
		this.dp_name = dp_name;
		this.dp_tele = dp_tele;
		this.dp_fd = dp_fd;
	}
	public Integer getDp_id() {
		return dp_id;
	}
	public void setDp_id(Integer dp_id) {
		this.dp_id = dp_id;
	}
	public String getDp_name() {
		return dp_name;
	}
	public void setDp_name(String dp_name) {
		this.dp_name = dp_name;
	}
	public Integer getDp_tele() {
		return dp_tele;
	}
	public void setDp_tele(Integer dp_tele) {
		this.dp_tele = dp_tele;
	}
	public String getDp_fd() {
		return dp_fd;
	}
	public void setDp_fd(String dp_fd) {
		this.dp_fd = dp_fd;
	}
	
}

1.2

1.2.1 

package com.neuedu.mapper;

import com.neuedu.pojo.Department;
import com.neuedu.pojo.Post;
import com.neuedu.pojo.Staff;

public interface UserMapper {
	
	//部门管理
	public Department findDepartmentListByIdAndName(Integer dp_id,String dp_name)throws Exception;
	public int insertDepartment(Department department)throws Exception;
	public int updateDepartment(Department department)throws Exception;
	public Department findDepartmentListById(Integer dp_id)throws Exception;
	public int deleteDepartment(Integer dp_id)throws Exception;
}

1.2.2 



  
 
 
  
 	 
		INSERT INTO department(dp_id,dp_name,dp_tele,dp_fd) VALUES(#{dp_id},#{dp_name},#{dp_tele},#{dp_fd})
	
 
 UPDATE department 
 SET dp_name=#{dp_name},dp_tele=#{dp_tele},dp_fd=#{dp_fd} 
 WHERE dp_id=#{dp_id}
 
 
 DELETE FROM department 
 WHERE dp_id=#{dp_id}
 

2.Service层代码的编写

2.1

package com.neuedu.service;

import com.neuedu.pojo.Department;
public interface UserService {
	//部门管理
	public Department findDepartmentListByIdAndName(Integer dp_id,String dp_name)throws Exception;
	public int insertDepartment(Department department)throws Exception;
	public int updateDepartment(Department department)throws Exception;
	public Department findDepartmentListById(Integer dp_id)throws Exception;
	public int deleteDepartment(Integer dp_id)throws Exception;	
}

2.2

package com.neuedu.service;

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

import com.neuedu.mapper.UserMapper;
import com.neuedu.pojo.Department;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Override
	public Department findDepartmentListByIdAndName(Integer dp_id,String dp_name)throws Exception{
		return userMapper.findDepartmentListByIdAndName(dp_id,dp_name);
	}
	public int insertDepartment(Department department)throws Exception{
		return userMapper.insertDepartment(department);
	}
	public int updateDepartment(Department department)throws Exception{
		return userMapper.updateDepartment(department);
	}
	@Override
	public Department findDepartmentListById(Integer dp_id) throws Exception {
		// TODO Auto-generated method stub
		return userMapper.findDepartmentListById(dp_id);
	}
	public int deleteDepartment(Integer dp_id)throws Exception{
		return userMapper.deleteDepartment(dp_id);
	}
}

3.Web层代码的编写 

package com.neuedu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.neuedu.pojo.Department;
import com.neuedu.service.UserService;

@Controller
@RequestMapping(value="/user")
public class controller {
	@Autowired
	private UserService userService;
	//查询Department
	@RequestMapping(value="/findDepartmentListByIdAndName")
	public ModelAndView findDepartmentListByIdAndName(Integer dp_id,String dp_name)throws Exception {
	    Department department = userService.findDepartmentListByIdAndName(dp_id, dp_name); 
	    ModelAndView mv = new ModelAndView();
	    mv.addObject("department",department);
	    mv.setViewName("bumenguanli/save2_index2");
	    return mv;
	}
	@RequestMapping(value="/findDepartmentListById")
	public ModelAndView findDepartmentListById(Integer dp_id)throws Exception {
	    Department department = userService.findDepartmentListById(dp_id); 
	    ModelAndView mv = new ModelAndView();
	    mv.addObject("department",department);
	    mv.setViewName("bumenguanli/save2_index2");
	    return mv;
	}
	//插入Department
	@RequestMapping(value="/addDepartment")
	public ModelAndView addDepartment(Department department)throws Exception{
		userService.insertDepartment(department);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("save2_index1");
		return mv;
	}
	//更新Department
	@RequestMapping(value="/toupdateDepartment")
	public ModelAndView toupdateDepartment(Integer dp_id)throws Exception{
		Department department = userService.findDepartmentListById(dp_id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("department",department);
		mv.setViewName("bumenguanli/alter_index");
		return mv;
	}
	@RequestMapping(value="/updateDepartment")
	public ModelAndView updateDepartment(Department department)throws Exception{
		int deptid = userService.updateDepartment(department);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("bumenguanli/save2_index1");
		return mv;
	}
	//删除Department
	@RequestMapping(value="/deleteDepartment")
	public ModelAndView deleteDepartment(Integer dp_id)throws Exception{
		int count = userService.deleteDepartment(dp_id);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("bumenguanli/save2_index1");
		return mv;
	}
}

4.前台jsp页面代码的编写

4.1

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





Greeny Grass by Free CSS Templates









部门管理

 

新增部门详细

* 部门编号:
* 部门名称:
* 移动电话:
* 成立日期:
      

 

 

4.2

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





Greeny Grass by Free CSS Templates








部门管理

 

* 部门编号:
* 部门名称:
* 移动电话:
* 成立日期:
         

 

 

4.3

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





Greeny Grass by Free CSS Templates








部门管理

xxx部门下的员工列表

序号编号
员工编号
员工姓名
岗位类型
所属部门
其他
1
3423324
abc
经理
人事部
2
3423423
efh
部长
东软集团

4.4

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  





save2_index1









部门管理

 

编号    名称    

     

4.5

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  





save2_index1









部门管理

编号    名称    

     

部门编号: 部门名称: 部门电话 建立时间: 修改 删除
${department.dp_id} ${department.dp_name} ${department.dp_tele} ${department.dp_fd}

三.开发体会

    通过实际项目的开发,我对ssm框架有了更深入的理解,比如说对象之间的依赖关系由spring控制,减小了耦合,简化了开发;再比如说mybatis支持动态的sql,数据库的操作采用xml文件配置,解除了sql和代码的耦合等。

      随着由浅入深的学习,从对ssm框架一无所知到有所涉猎,我能体会到该框架的优势,以后也会继续学习。

 

你可能感兴趣的:(基于SSM框架的开发过程及体会)