springmvc整合mybatis

导入所需要的jar包

  • spring的jar包
  • mybatis的jar包
    • mybatis-3.4.5.jar
    • mybatis-spring-1.3.1.jar
  • springmvc的jar包
    • spring-webmvc-5.0.0.RELEASE.jar

配置mybatis: mybatis.xml




  

配置spring dao层: spring-dao.xml

db.properties:

jdbcUrl=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.jdbc.Driver
user=root
password=root
minPoolSize=5 
maxPoolSize=30
initialPoolSize=10
maxIdleTime=60
acquireIncrement=5
idleConnectionTestPeriod=60
acquireRetryAttempts=30

spring-dao.xml:




    
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
         
        
        
        
    
    
    
    
        
        
        
        
    
    
    
    
        
    


配置service层: spring-service.xml

spring-service.xml:




    
    
    

配置事务层: spring-tx.xml

spring-tx.xml:



    
    
    
        
        
    
    
    
    
        
            
            
            
            
            
            
        
    
    
    
    
        
    
    

配置springmvc: springmvc.xml

springmvc.xml:




    
    
    
    
    
    
    
    
        
        
    

配置web.xml

web.xml:



    springmvc-mybatis
    
        index.html
        index.htm
        index.jsp
        default.html
        default.htm
        default.jsp
    
    
    
    
        contextConfigLocation
        classpath:spring/spring-*.xml
    
    
        org.springframework.web.context.ContextLoader
    
    
    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            
            contextConfigLocation
            classpath:spring/springmvc.xml
        
    
    
        springmvc
        *.action
    



数据绑定

  • springmvc默认支持的数据类型: 请求可以直接使用的对象, 以及返回的类型
    @RequestMapping("/itemEdit") //方法中的这些参数都是由框架传递, 需要使用哪个就定义哪个
    public String editItem(HttpServletRequest request, 
            HttpServletResponse response, HttpSession session, Model model) {
        //从request中取参数
        String strId = request.getParameter("id");
        int id = new Integer(strId);
        //调用服务
        Items items = itemService.getItemById(id);
        //把结果传递给页面
        //ModelAndView modelAndView = new ModelAndView();
        //modelAndView.addObject("item", items);
        //设置逻辑视图
        //modelAndView.setViewName("editItem");
        //return modelAndView;
        //设置返回结果
        model.addAttribute("item", items);
        //返回逻辑视图
        return "editItem";
    }
  • 简单类型的数据绑定
    // 直接写Integer id就可以接收到客户端发送过来的id参数, 
    //不过方法上的参数名必须要与客户端的参数名相同!
    @RequestMapping("/itemEdit") 
    public String editItem(Integer id, Model model) {
        Items items = itemService.getItemById(id);
        //把数据传递给页面
        model.addAttribute("item", items);
        //返回逻辑视图
        return "editItem";
    }
  • 当请求参数名称与方法参数名称不一致时, 可以使用注解进行映射:
    @RequestMapping("/itemEdit")
    public String editItem(@RequestParam(value="id",defaultValue="1",required=true)Integer ids, Model model) {
        Items items = itemService.getItemById(ids);
        //把数据传递给页面
        model.addAttribute("item", items);
        //返回逻辑视图
        return "editItem";
    }

@RequestParam(value="id",defaultValue="1",required=true)Integer ids
意思是把请求参数中的id映射到ids上, 如果没有该参数, 可以使用默认值: defaultValue="1"
required=true的意思是该参数必须要有值

  • 当需要接收一个pojo类型的参数时, 要求表单中的name属性与pojo的属性名相同!
    @RequestMapping("/updateitem")
    public String updateItem(Items items) {
        itemService.updateItem(items);
        //返回成功页面
        return "success";
    }
  • 绑定包装pojo
    包装对象定义如下:
Public class QueryVo {
  private Items items;
}

页面定义:



Controller方法定义如下:

    @RequestMapping("/queryitem")
    public String queryItem(QueryVo queryVo) {
        //打印绑定结果
        System.out.println(queryVo.getItems().getId());
        System.out.println(queryVo.getItems().getName());
        return "success";
    }
  • 自定义参数绑定
    自定义Converter
public class DateConverter implements Converter {

    @Override
    public Date convert(String source) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

配置Converter

    
    
    
    
        
            
                
            
        
    

配置方式2(了解)


    
        
            
                
            
        
    
    
    
        
    
    
    
          
    
    
    

解决POST乱码的问题

在web.xml文件中加入以下内容:

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

你可能感兴趣的:(springmvc整合mybatis)