Maven +spring+springmvc+mybatis简单实例

spring+springmvc+mybatis简单实例

1、项目结构如下

Maven +spring+springmvc+mybatis简单实例_第1张图片
2、pom.xml文件中的以来配置



	
		commons-logging
		commons-logging
		1.2
	

	

	
		org.springframework
		spring-aop
		4.1.4.RELEASE
	
	
		org.springframework
		spring-beans
		4.1.4.RELEASE
	

	
		org.springframework
		spring-context
		4.1.4.RELEASE
	


	
		org.springframework
		spring-core
		4.1.4.RELEASE
	

	
		org.springframework
		spring-expression
		4.3.14.RELEASE
	


	
		org.springframework
		spring-web
		4.1.4.RELEASE
	

	
		org.springframework
		spring-webmvc
		4.1.4.RELEASE
	

	
		org.springframework
		spring-jdbc
		4.1.4.RELEASE
	

	

	
		org.mybatis
		mybatis
		3.4.5
	

	
		org.mybatis
		mybatis-spring
		1.3.2
	


	
		javax.servlet
		servlet-api
		2.5
		provided
	

	
		com.alibaba
		druid
		1.1.10
	

	
		mysql
		mysql-connector-java
		5.1.39
	






	src/main/java
	
		
		
			org.apache.maven.plugins
			maven-compiler-plugin
			3.1
			
				1.8
				1.8
			
		

		

		
			org.apache.tomcat.maven
			tomcat7-maven-plugin
			2.2
			
				
				/${project.artifactId}
				8888
				UTF-8 
				http://localhost:8888/
				tomcat7
			
		

	

3、mysql.properties 配置如下

mybatis.driverClassName=com.mysql.jdbc.Driver
mybatis.url=jdbc:mysql://localhost:3306/spring
mybatis.username=root
mybatis.password=Gepoint

4、UserMapper.xml 配置如下

    
 
    


   
        INSERT INTO user(name) VALUES(#{username})
   


5、web.xml配置如下

    
 

 
         springmvcbd003
         
          		default.jsp
         

  		
     		   contextConfigLocation
			  /WEB-INF/applicationContext.xml,/WEB-INF/springmvc-servlet.xml
        
		 
     		   org.springframework.web.context.ContextLoaderListener
	     

	  
   			  springmvc
   			 org.springframework.web.servlet.DispatcherServlet
	  
		 
     			  springmvc
        	     /
    	

6、applicationContext.xml 配置如下

				
			
           
          
	
		
		    
			
			
			
		    
		
	
		
		
			
			
			
			
			
		
	
	
		
			
			
		
			
			
		
	
		
	

7、springmvc-servlet.xml 配置如下

     
           

	    
         
 	    
  	    
  
   
      
        
        
        
    

8、src目录结构
Maven +spring+springmvc+mybatis简单实例_第2张图片
9、各个类文件的代码如下

  1. UserController

     package com.iweb.controller;
     import java.util.HashMap;
     import java.util.Map;
     
     import org.mybatis.spring.SqlSessionTemplate;
     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.RequestMethod;
     import org.springframework.web.bind.annotation.RequestParam;
     
     import com.iweb.service.UserService;
     
     @Controller
     @RequestMapping("/user")
     public class UserController {
     		@Autowired
     		UserService us;
    
     @RequestMapping(value = "/insert", method = { RequestMethod.GET,
     		RequestMethod.POST })
     public String userInsert(@RequestParam("username") String username) {
     	Map map = new HashMap();
     	map.put("username", username);
     	us.insert(map);
     	return "/usermanage/success";
     }
    
         SqlSessionTemplate sssSessionTemplate;
     }
    

2)UserMapper代码如下

package com.iweb.dao;

import java.util.Map;

import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
	public int insert(Map map);
	/*
	 * public int update(Map map); public int delete(Map map); public int search();
	 */
}

3)UserService代码如下

    package com.iweb.service;
   import java.util.Map;
	public interface UserService {
		public int insert(Map map);	
	}

4)UserServiceImple代码如下

 package com.iweb.service.imp;

import java.util.Map;

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

import com.iweb.dao.UserMapper;
import com.iweb.service.UserService;

@Service
public class UserServiceImp implements UserService{
	
	@Autowired
	UserMapper userMapper;
	public int insert(Map map){
		return userMapper.insert(map);
	}

}

10、default.jsp的代码如下

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




Insert title here


	新增


你可能感兴趣的:(java,spring,mybatis)