整合mybatis和spring,配置spring注解

首先看工程目录:

整合mybatis和spring,配置spring注解_第1张图片

接下来分别看每个文件:

pom.xml


  4.0.0
  lmcdemo2
  lmcdemo2
  war
  0.0.1-SNAPSHOT
  lmcdemo2 Maven Webapp
  http://maven.apache.org
  
  	3.1.1.RELEASE
  	1.6.11
  
  
     
       org.springframework
       spring-aop
       ${org.springframework.version}
    
    
       org.springframework
       spring-aspects
       ${org.springframework.version}
    
    
       org.aspectj
       aspectjrt
       ${aspectj.version}
    
    
       org.aspectj
       aspectjweaver
       ${aspectj.version}
    
    
			commons-dbcp
			commons-dbcp
			1.4
	
	  
		    org.mybatis  
		    mybatis  
		    3.1.1  
	  
    
			mysql
			mysql-connector-java
			5.1.6
	
	
	    org.springframework
	    spring-jdbc
	    3.1.1.RELEASE
    
    
            org.mybatis
            mybatis-spring
            1.2.2
        
    
		javax.servlet
		javax.servlet-api
		3.1-b02
		jar
		provided
	
	
		javax.servlet
		jstl
		1.2
		runtime
	
    
      junit
      junit
      3.8.1
      test
    
    	
  		org.springframework
  		spring-webmvc
  		${org.springframework.version}
  	
  
  
    lmcdemo2
    
			
				src/main/resources
				
					**/*.properties
					**/*.xml
				
			
			
				src/main/java
				
					**/*.properties
					**/*.xml
				
			
   
  


注意最后的, 使maven 打包source文件夹下面的 xml等配置文件

web.xml文件:





	lmcdemo2
	
		contextConfigLocation
		classpath*:applicationContext.xml
	
	
	
		characterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
		
			forceEncoding
			true
		
	
	
		characterEncodingFilter
		/*
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
		mvcServlet
		org.springframework.web.servlet.DispatcherServlet
		
		contextConfigLocation
		classpath*:/servlet-context.xml
		
		1
	
	
	
	mvcServlet
	*.action
	
	


servlet-context.xml文件:



 
 
 
 
  
  
   
  
  
 

MyBatis-Configuration.xml文件:




    
        
    

UserDaoMapper.xml文件:

  
 

    

applicationContext.xml文件:








    
        
        
        
        
        
        
        
        
    

    
        
        
    

    
        
        
    

    
        
    


UserDao.java文件:

package lmcdemo.dao;
public interface UserDao {
    public int countAll();
}

UserService.java文件:

package lmcdemo.services;

public interface UserService {
    public int countAll();
}

UserServiceImpl.java文件:

package lmcdemo.services.impl;

import lmcdemo.dao.UserDao;
import lmcdemo.services.UserService;

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

@Repository
@SuppressWarnings("rawtypes")
public class UserServiceImpl implements UserService{
	@Autowired
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }
    
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    public int countAll() {
        return this.userDao.countAll();
    }
}

UserController.java测试文件:

package lmcdemo.controller;

import lmcdemo.services.UserService;
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;


@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userInfoSerivce;

	@ResponseBody
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/userlist")
	public String userlist(String test,int count) {
		int a=userInfoSerivce.countAll();
		return String.valueOf(a)+test+String.valueOf(count);
	}
}

http://localhost:8080/lmcdemo2/user/userlist.action?test=oracle&count=88

Helloworld.java测试文件:

package lmcdemo.controller;
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/helloworld")
public class Helloworld {
    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("helloworld");
        return mv;
    }
}

http://localhost:8080/lmcdemo2/helloworld.action


main.java测试文件:
package lmcdemo;

import lmcdemo.services.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class main {
	
	public static void main(String[] args) {
		  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	        UserService userService = (UserService)context.getBean("userService");
	        System.out.println(userService.countAll());
		
	}

}


你可能感兴趣的:(Web开发)