ssm框架实践(1)搭建框架

1、新建maven-web项目

注意maven和eclipse结合使用需要的配置:https://blog.csdn.net/jiangeeq/article/details/70144191
(1)maven自带compiler组件,默认编译器是JRE1.5版本,略低。可以通过配置setting.xml或者pom.xml文件改变。
注意:配置完后,需要右键项目,maven—update project

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

(2)启动maven,需要JDK1.6版本以上,所以需要配置eclipse中的JDK版本

2、启动web项目,并访问index.jsp

maven自带tomcat组件,默认是tomcat6,可在pom.xml中修改。


			
				org.apache.tomcat.maven
				tomcat7-maven-plugin
				2.2
				
					8080
					/carls
					UTF-8
				
			

配置完成后,右键项目,run as —maven build ,输入goals:tomcat7:run 点击run;服务器启动,即可输入网址,预览index.jsp页面
ssm框架实践(1)搭建框架_第1张图片

3、导入所需jar包,配置pom.xml


		4.3.14.RELEASE
	
	
		
			junit
			junit
			4.12
			test
		

		
		
		
			org.springframework
			spring-test
			${spring.framwork.version}
			test
		
		
		
			org.springframework
			spring-core
			${spring.framwork.version}
		
		
		
			org.springframework
			spring-context
			${spring.framwork.version}
		
		
		
		
			org.springframework
			spring-jdbc
			${spring.framwork.version}
		
		
			mysql
			mysql-connector-java
			5.1.38
		
		
		
			org.springframework
			spring-tx
			${spring.framwork.version}
		
		
		
		
			org.springframework
			spring-aop
			${spring.framwork.version}
		
		
		
			org.aspectj
			aspectjweaver
			1.8.13
		


		
		
			org.mybatis
			mybatis
			3.4.1
		
		
			org.mybatis
			mybatis-spring
			1.3.1
		


		
		
			org.springframework
			spring-web
			${spring.framwork.version}
		
		
			org.springframework
			spring-webmvc
			${spring.framwork.version}
		
		
			org.springframework
			spring-context-support
			${spring.framwork.version}
		

		
		
			com.alibaba
			fastjson
			1.2.3
		
		
			com.fasterxml.jackson.core
			jackson-annotations
			2.9.5
		
		
			com.fasterxml.jackson.core
			jackson-core
			2.9.5
		
		
			com.fasterxml.jackson.core
			jackson-databind
			2.9.5
		

		
		
			javax.servlet
			javax.servlet-api
			3.1.0
			provided
		
		
			javax.servlet
			jsp-api
			2.0
			provided
		
		
	

4、配置spring.xml



	
	


5、配置spring-mybatis.xml和jdbc.properties



	
	
	
	
	
		
		
		
		
	

	
	
		
	

	
	
		
			
		
	

	
	
		
		
		
	


	
	
		
		
		
		
		
	

	
		
		
		
	


driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/test
name=root
password=root

6、配置spring-mvc.xml



	
	

	
	
	
		
			
				text/html;charset=UTF-8
			
		
	
	
	
		
			
				 
			
		
	

7、配置web.xml

  

Archetype Created Web Application
 
    Archetype Created Web Application  
      
      
        contextConfigLocation  
        classpath:spring-mybatis.xml,classpath:spring.xml  
      
   
      
      
        org.springframework.web.context.ContextLoaderListener  
      
      
      
        org.springframework.web.util.IntrospectorCleanupListener  
      
	
	   
    
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
		
			forceEncoding
			true
		
	
	
		encodingFilter
		/*
	
 
      
      
        SpringMVC  
        
        org.springframework.web.servlet.DispatcherServlet  
          
            contextConfigLocation  
            classpath:spring-mvc.xml  
          
        1  
        true  
      
      
        SpringMVC  
          
        *.do  
      
      
        /index.jsp  
      
 
 

8、以login.jsp为例,测试框架是否搭建成功

login.jsp如下

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Insert title here


	
姓名:
密码:

建立入了目录和文件
ssm框架实践(1)搭建框架_第2张图片
User.java如下

package model;
import java.io.Serializable;
	/**
	 *由于对象 是保存在内存中的,要保存到数据库中就需要实现Serializable接口
	 */
	public class User implements Serializable{
		private static final long serialVersionUID = 1L;
		
		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;
		} 
}

UserController.java如下

package controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import model.User;
import service.UserService;
 
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("/save")
	@ResponseBody  
	public void save(User user) {
		userService.saveUser(user);
	}
}

UserService.java接口和类如下

package service;
 
import model.User;
 
public interface UserService {
	public int saveUser(User user);
}

package service.impl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import dao.UserDao;
import model.User;
import service.UserService;
 
 
@Service("userService") //注意这里的别名是为了之后若要重写UserServiceImpl类而相互区分的标记
public class UserServiceImpl implements UserService {
 
	@Autowired
	private UserDao userDao;
	@Override
	public int saveUser(User user) {
		return userDao.saveUser(user);
	}
 
}

UserDao.java和UserMapper.xml如下

package dao;
 
import model.User;
 
public interface UserDao {
	public int saveUser(User user);
}



 

	
		insert into t_user (user_name,pass_word) value (#{userName},#{passWord})
	
 


重新启动maven项目,输入地址如下
ssm框架实践(1)搭建框架_第3张图片
ssm框架实践(1)搭建框架_第4张图片
ssm框架实践(1)搭建框架_第5张图片
ssm框架实践(1)搭建框架_第6张图片

你可能感兴趣的:(ssm框架实践(1)搭建框架)