springMvc+Mybatis整合

项目结构

springMvc+Mybatis整合_第1张图片

springMvc+Mybatis整合_第2张图片

1、导入相关的jar包

ant-1.9.6.jar

ant-launcher-1.9.6.jar

asm-5.2.jar

aspectjweaver-1.8.11.jar

cglib-3.2.5.jar

commons-dbcp-1.4.jar

commons-fileupload-1.3.1.jar

commons-io-2.5.jar

commons-logging-1.1.3.jar

commons-logging-1.2.jar

commons-pool-1.6.jar

jackson-core-asl-1.9.2.jar

jackson-mapper-asl-1.9.2.jar

javassist-3.22.0-CR2.jar

json-lib-2.3-jdk15.jar

jstl-1.2.jar

log4j-1.2.17.jar

log4j-api-2.3.jar

log4j-core-2.3.jar

mybatis-3.4.5.jar

mybatis-spring-1.3.1.jar

mysql-connector-java-5.1.10-bin.jar

ognl-3.1.15.jar

pagehelper-5.1.2.jar

quartz-2.3.0.jar

slf4j-api-1.7.25.jar

slf4j-log4j12-1.7.25.jar

spring-aop-4.3.10.RELEASE.jar

spring-aspects-4.3.10.RELEASE.jar

spring-beans-4.3.10.RELEASE.jar

spring-context-4.3.10.RELEASE.jar

spring-context-support-4.3.10.RELEASE.jar

spring-core-4.3.10.RELEASE.jar

spring-expression-4.3.10.RELEASE.jar

spring-instrument-4.3.10.RELEASE.jar

spring-instrument-tomcat-4.3.10.RELEASE.jar

spring-jdbc-4.3.10.RELEASE.jar

spring-jms-4.3.10.RELEASE.jar

spring-messaging-4.3.10.RELEASE.jar

spring-orm-4.3.10.RELEASE.jar

spring-oxm-4.3.10.RELEASE.jar

spring-test-4.3.10.RELEASE.jar

spring-tx-4.3.10.RELEASE.jar

spring-web-4.3.10.RELEASE.jar

spring-webmvc-4.3.10.RELEASE.jar

spring-webmvc-portlet-4.3.10.RELEASE.jar

spring-websocket-4.3.10.RELEASE.jar


2、配置web.xml文件



  
  	org.springframework.web.context.ContextLoaderListener
  
  
  		contextConfigLocation
		
			classpath:config/applicationContext.xml,
		
  
  
  
  	hello
  	org.springframework.web.servlet.DispatcherServlet
  	
  		contextConfigLocation
  		classpath:config/hello-servlet.xml
  	
  	1
  
  
  	hello
  	/
  
  
    
        encodingFilter  
        org.springframework.web.filter.CharacterEncodingFilter  
          
            encoding  
            UTF-8  
          
      
      
        encodingFilter  
        /*  
    
2、配置hello-servlet.xml文件



        
      
      	
      
      
      
      
      
      
      	
      	
      	
      
      
      
      	
       
      
       


3.Shop.java类

public class Shop {
	private int id;
	private String name;
	private String img;
	private float price;
	private Date addTime;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getImg() {
		return img;
	}
	public void setImg(String img) {
		this.img = img;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public Date getAddTime() {
		return addTime;
	}
	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}
}

4、ShopDao接口类

public interface ShopDao {
	/**
	 * 根据id查找
	 * @param id
	 * @return
	 */
	public Shop getShopById(String str, int id);
	/**
	 * 添加
	 * @param orderDao
	 * @return
	 */
	public int add(String str,Object ob);
	
	/**
	 * 查找list集合
	 * @return
	 */
	public List getAllShop(String str);

}

5、ShopDaoImpl实现类

@Repository("shopDao")
public class ShopDaoImpl implements ShopDao{
	
	@Resource(name="sqlSessionTemplate")
	private SqlSessionTemplate sqlSession;

	@Override
	public Shop getShopById(String str, int id) {
		// TODO Auto-generated method stub
		return sqlSession.selectOne(str, id);
	}

	@Override
	public int add(String str, Object ob) {
		// TODO Auto-generated method stub
		return sqlSession.insert(str, ob);
	}

	@Override
	public List getAllShop(String str) {
		// TODO Auto-generated method stub
		return sqlSession.selectList(str);
	}

}

6、ShopService接口类

public interface ShopService {
	
	public int addShop(Shop shop);

	public Shop findById(int id);
	
	public List getAllShop();
}

7、ShopServiceImpl实现类

@Service
public class ShopServiceImpl implements ShopService{
	
	@Resource(name="shopDao")
	private ShopDao shopDao ;

	@Override
	public int addShop(Shop shop) {
		// TODO Auto-generated method stub
		return shopDao.add("com.mvn.dao.ShopDao.add", shop);
	}

	@Override
	public Shop findById(int id) {
		// TODO Auto-generated method stub
		return shopDao.getShopById("com.mvn.dao.ShopDao.getShopById", id);
	}

	@Override
	public List getAllShop() {
		// TODO Auto-generated method stub
		return shopDao.getAllShop("com.mvn.dao.ShopDao.getAllShop");
	}

}

8、ShopController类

@Controller
@RequestMapping("/shop")
public class ShopController {
	
	@Autowired(required=true)
	private ShopService shopService;
	
	@RequestMapping("/shopData")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView("shop/shopList");
		List shopList=shopService.getAllShop();
		mv.addObject("shopList", shopList);
		return mv;
	}
	@RequestMapping("/addShop")
	public ModelAndView add(){
		ModelAndView mv=new ModelAndView("shop/shopAdd");
		return mv;
	}
	@RequestMapping(value="/saveShop",method=RequestMethod.POST)
	public ModelAndView save(Shop shop,MultipartFile file,HttpServletRequest request) throws Exception{
		ModelAndView mv=new ModelAndView("redirect:/shop/shopData");
		shop.setAddTime(new Date());
		String path=null;
		//原始名称  
        String originalFilename = file.getOriginalFilename();  
        //上传图片  
        if(file!=null && originalFilename!=null && originalFilename.length()>0){
			// 存储图片的路径
			String realPath=request.getSession().getServletContext().getRealPath("/")+"/images";
			// 自定义的文件名称
			path=realPath+"/"+originalFilename;
			// 转存文件到指定的路径
			file.transferTo(new File(path));
			System.out.println("文件成功上传到指定目录下");
        }
        shop.setImg(originalFilename);
		shopService.addShop(shop);
		return mv;
	}

}

9、applicationContext.xml配置



   
   
   
   
   	
   
   
   
   
   
   
   		
   		
   		
   		
   
   
   
   		
   		
   		
   
   
   
   		
   			
   				
   				
   			
   		
   
   
     
          
          
          
          
     
    
    
    	
    	
    	
    	
    	
    	
    
    
	
		
	
    
    
    	
    
      
      
          
              
              
              
              
              
              
              
              
              
              
              
          
      
      
          
          
             
          
              
          
            

10、Configure.xml配置




	
		
		
		
	

11、ShopMapper.xml配置





	
        
        
        
        
        
    
	
	
	
	
		insert into `shop`(id,name,img,price,addTime) 
		values(#{id},#{name},#{img},#{price},#{addTime})
	
	
		delete from `shop` where id=#{sid}
	


12、shopList.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>





订单商品列表页面



	

商品列表

id 商品名称 商品图片 商品价格 添加时间 操作
${sp.id} ${sp.name} ${sp.price} 删除 编辑

添加


13、shopAdd.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>





添加商品




	

新增商品

商品

列表页面:

springMvc+Mybatis整合_第3张图片

添加页面:

springMvc+Mybatis整合_第4张图片
以上代码只展示了部分,源码下载地址:http://download.csdn.net/download/u011936251/10123851


你可能感兴趣的:(框架整合)