Maven+ssm框架简单增删改查

语录:在开发一个项目时,一定要注意细节,谨慎严密,多查多问,保持足够耐心。

         世上无难事,只怕有心人


一:创建一个Maven项目

      Maven+ssm框架简单增删改查_第1张图片

    注意: 1.Build Path-Configure Build Path   修改成对应的jdk版本

二:pom.xml导入相应的包


	4.0.0
	com.test
	String_fate
	0.0.1-SNAPSHOT
	war
	
		
		4.0.2.RELEASE
		
		3.2.6
		
		1.7.7
		1.2.17
	

	
		
			junit
			junit
			4.11

		
		
		
			org.springframework
			spring-core
			${spring.version}
		

		
			org.springframework
			spring-web
			${spring.version}
		
		
			org.springframework
			spring-oxm
			${spring.version}
		
		
			org.springframework
			spring-tx
			${spring.version}
		

		
			org.springframework
			spring-jdbc
			${spring.version}
		

		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
			org.springframework
			spring-aop
			${spring.version}
		

		
			org.springframework
			spring-context-support
			${spring.version}
		

		
			org.springframework
			spring-test
			${spring.version}
		
		
		
			org.mybatis
			mybatis
			${mybatis.version}
		
		
		
			org.mybatis
			mybatis-spring
			1.2.2
		
		
		
			javax
			javaee-api
			7.0
		
		
		
			mysql
			mysql-connector-java
			5.1.30
		
		
		
			commons-dbcp
			commons-dbcp
			1.2.2
		
		
		
			jstl
			jstl
			1.2
		
		
		
		
			log4j
			log4j
			${log4j.version}
		

		
		
			com.alibaba
			fastjson
			1.1.41
		

		
		
			com.google.code.gson
			gson
			2.7
		

		
			org.slf4j
			slf4j-api
			${slf4j.version}
		

		
			org.slf4j
			slf4j-log4j12
			${slf4j.version}
		
		
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
		
			commons-fileupload
			commons-fileupload
			1.3.1
		
		
			commons-io
			commons-io
			2.4
		
		
			commons-codec
			commons-codec
			1.9
		

		
		
			javax.mail
			mail
			1.4
		

		
		
			org.apache.commons
			commons-lang3
			3.4
		
	

	
		SSMDemo
		
			
			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				1.3.2
				
					true
					true
				
			

			
				org.apache.maven.plugins
				maven-compiler-plugin
				2.3.1
				
					1.6
					1.6
					utf8
				
			

		

		
			
				src/main/java
				
					**/*.xml
				
			
			
				src/main/resources
			
		
	




三.spring-mvc.xml文件配置    

 


                        
   
   
       
     
   
   
      
         
            text/html;charset=UTF-8
         
      
   
   
   
      
         
              
         
      
   
   
   
      
      
      
      
      
      
   
   
   
   
    
 


四.创建generatorConfig.xml文件并maven-build





  



    





    
    
    
    
    



五.在eclipse对generatorConfig.xml进行Maven-build,生成表对应的model以及dao接口以及配置文件 

   Maven+ssm框架简单增删改查_第2张图片

       代码就不写下来了

六.整合mabaties,在spring-mabaties.xml文件中配置事务

 

 

 
      
    
    
    


      
      
          
      
  
      
          
          
          
          
          
          
          
          
          
          
          
          
          
         
      
  
      
      
          
          
        
      
  
      
      
        
          
      
  
      
      
          
        
    



七.配置日志文件log4j.properties和common-logging.properties,以及jdbc.properties

1.log4j.properties

   ### set log levels ###
log4j.rootLogger = debug , stdout , D , E


### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
## \u8F93\u51FAINFO\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7
log4j.appender.stdout.Threshold = INFO
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n


### \u8F93\u51FA\u5230\u65E5\u5FD7\u6587\u4EF6 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D:/logs/log.log
log4j.appender.D.Append = true
## \u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n


### \u4FDD\u5B58\u5F02\u5E38\u4FE1\u606F\u5230\u5355\u72EC\u6587\u4EF6 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
## \u5F02\u5E38\u65E5\u5FD7\u6587\u4EF6\u540D
log4j.appender.E.File = D:/logs/error.log
log4j.appender.E.Append = true
## \u53EA\u8F93\u51FAERROR\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7!!!
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout

log4j.rootLogger = debug , stdout , D , E


### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
## \u8F93\u51FAINFO\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7
log4j.appender.stdout.Threshold = INFO
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n


### \u8F93\u51FA\u5230\u65E5\u5FD7\u6587\u4EF6 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D:/logs/log.log
log4j.appender.D.Append = true
## \u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n


### \u4FDD\u5B58\u5F02\u5E38\u4FE1\u606F\u5230\u5355\u72EC\u6587\u4EF6 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
## \u5F02\u5E38\u65E5\u5FD7\u6587\u4EF6\u540D
log4j.appender.E.File = D:/logs/error.log
log4j.appender.E.Append = true
## \u53EA\u8F93\u51FAERROR\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7!!!
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

2.common-logging.properties

 org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

3.jdbc.properties 

 #创建连接
driverClassName=com.mysql.jdbc.Driver
#地址
url=jdbc:mysql://localhost:3306/mysql
#用户名
username=root
#密码
password=199701

##以下可以不配
#初始化连接
initialSize=10
#最大连接数量
maxActive=300
#最大空闲连接
maxIdle=20
#最大空闲连接
minIdle=1
#等待时间

maxWait=60000


八.配置web.xml

 


	spring_fate
	
	
		org.springframework.web.context.ContextLoaderListener
	


	
	
		contextConfigLocation
		
		classpath:spring-mybatis.xml,classpath:spring-mvc.xml

	

	
	
		springMVC
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			classpath:spring-mybatis.xml,classpath:spring-mvc.xml
		
		
		1
	

	
	
		springMVC
		
		/
	

	
	
		org.springframework.web.util.IntrospectorCleanupListener
	

	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
		
			forceEncoding
			true
		
	
	
		encodingFilter
		/*
	
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	



九.建立service以及service实现(Userservice.java UserserviceImpl)

 package com.test.service;
import java.util.List;
import model.User2;

public interface UserService {
	 void insert(User2 user2);
	 boolean update(User2 user2);
	 boolean delete(int user2Id);
	 User2 select(int user2Id);
	 List findAll();

}

 

package com.test.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import dao.User2Mapper;
import model.User2;
@Service  
@Transactional 
//此处不再进行创建SqlSession和提交事务,都已交由spring去管理了。  
public class UserServiceImpl implements UserService {
    @Resource  
    private User2Mapper mapper;  
	
	public void insert(User2 user2) {
		mapper.insert(user2);
	}
	public boolean update(User2 user2) {
		return mapper.update(user2);
	}
	public boolean delete(int user2Id) {
		return mapper.delete(user2Id);
	}
	public User2 select(int user2Id) {
	User2 user2=mapper.select(user2Id);
	return user2;
	}
	public List findAll() {
	List findlist=mapper.findAll();
	return findlist;
	}

}


十.建立控制层(Usercontroller.java)

 

package com.test.controller;

import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.reflection.SystemMetaObject;
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 com.test.service.UserService;

import model.User2;



@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	/**
	 * 获取所有用户列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllUser")
	public String getAlluser(HttpServletRequest request, Model model) {
		List user = userService.findAll();
		System.out.println(user);
		model.addAttribute("userlist", user);
		request.setAttribute("userlist", user);
		return "/allUser";
	}
	/**
	 * 跳转到添加用户界面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddUser")
	public String toAddUser() {
		return "/addUser";
	}
	/**
	 * 添加用户并重定向
	 * 
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/addUser")
	public String addUser(User2 user, Model model) {
		userService.insert(user);
		 return "redirect:/user/getAllUser"; 
	}
	/*
	 * 编辑用户
	 * 
	 * @param user
	 * 
	 * @param request
	 * 
	 * @return
	 */
	@RequestMapping("/updateUser")
	public String updateUser(User2 user2, HttpServletRequest request, Model model) {
		if (userService.update(user2)) {
			user2 = userService.select(user2.getUser2Id());
			request.setAttribute("user", user2);
			model.addAttribute("user", user2);
			return "redirect:/user/getAllUser";
		} else {
			return "/error";
		}
	}

	/*
	 * 根据id查询单个用户
	 * 
	 * @param id
	 * 
	 * @param request
	 * 
	 * @return
	 */
	@RequestMapping("/getUser")
	public String getUser(int id, HttpServletRequest request, Model model) {
		request.setAttribute("user", userService.select(id));
		model.addAttribute("user", userService.select(id));
		return "/editUser";
	}
	/**
	 * 删除用户
	 * 
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delUser")
	public String delUser(int id, HttpServletRequest request, HttpServletResponse response) {
		String result = "/error";  
        if(userService.delete(id)){  
            result = "forward:/user/getAllUser";  
        } 
        return result;
		}	
}


十一.创建视图层(index.jsp,addUser,allUser,editUser)

  1.index.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    String path=request.getContextPath();
    String basePath = request.getScheme() + "://"  
            + request.getServerName() + ":" + request.getServerPort()  
            + path + "/";  
     %>





首页


  

进入用户管理系统


 2.allUser.jsp

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



      
      

用户列表 


      

添加用户

姓名 职位 操作
${user1.userName } ${user1.passWord } 编辑 删除

 3.editUser.jsp 

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



  
     


编辑用户


编辑用户

姓名:

职位:

4.addUser.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path=request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 添加用户

添加用户

姓名:

职位:


以上就是所有代码,目录如下

Maven+ssm框架简单增删改查_第3张图片

需要从外导入的有 jQuery-3.31.min.js

数据库如下:

Maven+ssm框架简单增删改查_第4张图片

展示:




你可能感兴趣的:(项目实战)