Eclipse Maven项目搭建SSM

0.工作用到的大多都是SSM框架 一直想独立搭建一个自己的框架。翻了很多资料,经历各种Bug,终于搭建出了自己的框架
1. 环境配置
    apache-tomcat-8.0.37  
      jdk1.8.0_101
     Eclipse  Version: Neon Release (4.6.0)

2.Maven配置不做记录

3.创建Maven项目

   File→New→Next

  Eclipse Maven项目搭建SSM_第1张图片

Next>

Eclipse Maven项目搭建SSM_第2张图片

查找web,选择Version 1.0 Next

Eclipse Maven项目搭建SSM_第3张图片

填写GroupID、ArtifactID。 点击Finish

Eclipse Maven项目搭建SSM_第4张图片

创建完毕后你的项目结构大概是这样的,错误描述是这样的。如果项目结构不是这样的。在eclipse的右上角,就是有JavaEE  Java  Debug  三个按钮的地方,如果你选择的是JavaEE,改成Java就可以了。如果你的没有Java,那么打开windows -》Perspective-》打开透视图-》选择Java 

Eclipse Maven项目搭建SSM_第5张图片Eclipse Maven项目搭建SSM_第6张图片

Windows→Service→Runtime→Add→选择tomcat8→Installed JREs→Add→Standard VM 

添加你的jdk   选择完毕点击Finish

Eclipse Maven项目搭建SSM_第7张图片

选择JDK→Apply

Eclipse Maven项目搭建SSM_第8张图片

选择JDK1.8→Finish→OK

Eclipse Maven项目搭建SSM_第9张图片

在工程上右键,查看工程属性,找到Java Build Path,Add Library→Server Runtime为Tomcat 


Eclipse Maven项目搭建SSM_第10张图片

点击Finish后,项目工程变为下图

Eclipse Maven项目搭建SSM_第11张图片

项目右键→properties→Project Facets    java 默认为1.5改为1.8  Dynamic Web Module 默认为2.5 Tomcat 8 需要3.1

Eclipse Maven项目搭建SSM_第12张图片

修改方法:

找到Maven 的Setting 文件

在maven工程目录下找到  org.eclipse.wst.common.project.facet.core.xml  改为





  

  

  

  

项目工程下的web.xml文件修改为



 pom.xml文件中修改build节点,添加如下内容


       
              org.apache.maven.plugins
              maven-compiler-plugin
              
                     1.8
                     1.8
              
       

修改后,在项目上右键,找到Maven属性下的Update Project,勾选Force Update of Snapshots/Releases→ok

更新maven项目,再观察项目属性,Module已经变为3.1

Eclipse Maven项目搭建SSM_第13张图片

4.配置内容

pox.xml配置依赖的内容:


	4.0.0
	com.ssm
	Maven_ssm
	war
	0.0.1-SNAPSHOT
	Maven_ssm Maven Webapp
	http://maven.apache.org

	
	
		4.0.2.RELEASE
		3.2.8
		1.7.12
		1.2.17
	

	
	
		
		
			junit
			junit
			3.8.1
			test
		
		
		
			javax
			javaee-api
			7.0
		
		
		
			org.springframework
			spring-test
			${srping.version}
		
		
			org.springframework
			spring-core
			${srping.version}
		
		
			org.springframework
			spring-oxm
			${srping.version}
		
		
			org.springframework
			spring-tx
			${srping.version}
		
		
			org.springframework
			spring-jdbc
			${srping.version}
		
		
			org.springframework
			spring-aop
			${srping.version}
		
		
			org.springframework
			spring-context
			${srping.version}
		
		
			org.springframework
			spring-context-support
			${srping.version}
		
		
			org.springframework
			spring-expression
			${srping.version}
		
		
			org.springframework
			spring-orm
			${srping.version}
		
		
			org.springframework
			spring-web
			${srping.version}
		
		
			org.springframework
			spring-webmvc
			${srping.version}
		
		
		
		
			org.mybatis
			mybatis
			${mybatis.version}
		
		
			org.mybatis
			mybatis-spring
			1.2.2
		
		
		
		
			mysql
			mysql-connector-java
			5.1.35
		
		
		
			commons-dbcp
			commons-dbcp
			1.4
		
		
		
			jstl
			jstl
			1.2
		
		
		
			log4j
			log4j
			${log4j.version}
		
		
			org.slf4j
			slf4j-api
			${slf4j.version}
		
		
			org.slf4j
			slf4j-log4j12
			${slf4j.version}
		
		
		
		
		
			com.alibaba
			fastjson
			1.2.6
		
		
			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.10
		
		
	
	
	
		Maven_ssm
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
				
			
		
	

在src/main/resources下添加配置文件:applicationContext.xml




	
	

	
	

	
	

	
	

	
	

    

配置数据库连接池:jdbc.properties。  我是用的Navicat Mysql 数据库   url 和 username password 注意修改

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.**.***:3306/pension?autoReconnect=true&useUnicode=true&characterEncoding=utf8
jdbc.username=*****
jdbc.password=*****
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
jdbc.initialSize=0
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
jdbc.maxActive=20
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
jdbc.maxIdle=20
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
jdbc.minIdle=1
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
jdbc.maxWait=60000

配置日志:log4j.properties

#\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B    
log4j.rootLogger=INFO,Console,File    
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0    
log4j.appender.Console=org.apache.log4j.ConsoleAppender    
log4j.appender.Console.Target=System.out    
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F    
log4j.appender.Console.layout = org.apache.log4j.PatternLayout    
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n    
    
#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6    
log4j.appender.File = org.apache.log4j.RollingFileAppender    
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55    
log4j.appender.File.File = logs/ssm.log    
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F    
log4j.appender.File.MaxFileSize = 10MB    
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7    
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 

配置ioc注入:spring-dao.xml





	
	
		
		
		
	

    

spring-db.xml




	
	
		
	

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	

	
	
		
		
		
	

    

配置spring的事务管理器:spring-tx.xml

    
    
    
        
        
            
        
        
    

整合spring mvc:spring-mvc.xml




	
	
		
			
				text/html;charset=UTF-8
			
		
	

	
	
	

	
	

	
	
		
			
				 
			
		
	

	
	
		
		
		
	

	
	
		
		
		
		
		
		
	

  

修改web.xml




	Archetype Created Web Application

	
		/index.jsp
	

	
	
		contextConfigLocation
		classpath:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	

	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		true
		
			encoding
			UTF-8
		
	
	
		encodingFilter
		/*
	

	
	
		SpringMVC
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:applicationContext.xml
		
		1
		true
	
	
		SpringMVC
		/
	


这是我的目录结构

Eclipse Maven项目搭建SSM_第14张图片

User.java

package com.ssm.dto;

public class User {    
    private Integer id;    
    
    private String username;    
    
    private String password;    
    
    
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {    
        return password;    
    }    
    
    public void setPassword(String password) {    
        this.password = password == null ? null : password.trim();    
    }    
}    

IUserService.java

package com.ssm.service;

import java.util.List;    

import com.ssm.dto.User;    
    
public interface IUserService {    
        
    public User getUserById(int userId);    
    
    public void insertUser(User user);    
    
    public void addUser(User user);    
    
    public List getAllUser();    
}    

IUserDao.java

package com.ssm.dao;
import java.util.List;    
import java.util.Map;    
    
import com.ssm.dto.User;    
    
public interface IUserDao {    
        
    public User queryByPrimaryKey(Integer id);    
        
    public List queryUserByBatch(Map params);    
        
    public void insertUser(User user);    
        
    public void insertUserByBatch(List list);    
        
    public void deleteByPrimaryKey(Integer id);    
        
    public void delteUserByBatch(Map params);    
        
    public void updateByPrimaryKey(Integer id);    
    
    public List getAllUser();    
        
}  

UserMapper.xml


    
      
        
            
            
            
        
    
        
        id, username, password    
        
    
        
        
        
        
        
        
        
        
        
        
        
        insert into admin_user      
              
                
            id,      
                
                
            username,      
                
                
            password,      
                
               
              
              
                
            #{id,jdbcType=INTEGER},      
                
                
            #{username,jdbcType=VARCHAR},      
                
                
            #{password,jdbcType=VARCHAR},      
                
               
        
        
        
        
    insert into admin_user    
       (ID,    
        NAME,    
        PASSWORD,    
       )    
    select A.* from    
         (    
             
             select     
             #{user.id,jdbcType=INTEGER},    
             #{user.name,jdbcType=VARCHAR},    
             #{user.password,jdbcType=VARCHAR},    
              from dual    
             
         ) A    
        
    
        
        
        delete from admin_user    
        where id = #{id,jdbcType=INTEGER}      
        
        
        
        
     delete from admin_user    
     where id IN     
             
             #{ids,jdbcType=DECIMAL}    
             
        
    
        
          
        update admin_user      
              
                
            name = #{name,jdbcType=VARCHAR},      
                
                
            password = #{password,jdbcType=VARCHAR},      
                
              
        where id = #{id,jdbcType=INTEGER}     
        
      
    

UserController.java

package com.ssm.controller;

import java.util.List;    

import javax.annotation.Resource;    
import javax.servlet.http.HttpServletRequest;    
    
import org.springframework.stereotype.Controller;    
import org.springframework.ui.Model;    
import org.springframework.web.bind.annotation.RequestMapping;    
    
import com.ssm.dto.User;    
import com.ssm.service.IUserService;    
    
@Controller    
@RequestMapping("/user")    
public class UserController {    
    @Resource    
    private IUserService userService;    
        
    @RequestMapping("/userList")    
    public String userList(HttpServletRequest request,Model model){    
        List uList = userService.getAllUser();    
        model.addAttribute("uList", uList);    
        return "userList";    
    }    
        
    @RequestMapping("/showUser")    
    public String showUser(HttpServletRequest request,Model model){    
        int userId = Integer.parseInt(request.getParameter("id"));    
        User user = userService.getUserById(userId);    
        model.addAttribute("user", user);    
        return "showUser";    
    }    
        
    @RequestMapping("/addUserUI")    
    public String addUserUI(){    
        return "addUser";    
    }    
        
    @RequestMapping("/addUser")    
    public String addUser(HttpServletRequest request,Model model){    
        User user = new User();    
        user.setUsername(String.valueOf(request.getParameter("name"))); 
        user.setPassword(String.valueOf(request.getParameter("password")));    
        userService.addUser(user);    
        return "redirect:/user/userList";    
    }    
}    

index.jsp

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








My JSP 'userinfo.jsp' starting page











	
userName:
password:

测试  对数据库添加数据操作成功。

Eclipse Maven项目搭建SSM_第15张图片

这个时候后台会报错,问题出现在Controller 添加 Return返回的问题。但是框架搭建完成



你可能感兴趣的:(Eclipse Maven项目搭建SSM)