千家信息网

SpringMVC与mybatis整合

发表于:2025-01-20 作者:千家信息网编辑
千家信息网最后更新 2025年01月20日,一、逆向工程生成基础信息
千家信息网最后更新 2025年01月20日SpringMVC与mybatis整合

一、逆向工程生成基础信息

                                                                                                                                                                                                                                                                                                

 public static void main(String[] arhs) throws Exception{            List warnings = new ArrayList();            boolean overwrite = true;            File configFile = new File("src.main.resources/generator.xml");            ConfigurationParser cp = new ConfigurationParser(warnings);            Configuration config = cp.parseConfiguration(configFile);            DefaultShellCallback callback = new DefaultShellCallback(overwrite);            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);            myBatisGenerator.generate(null);        }

二、springMVC与Mybatis整合 各个配置文件

1.项目结构

2、各个文件的核心代码

a.web.xml

                  index.jsp                    contextConfigLocation         classpath:spring/applicationContext-*.xml                    org.springframework.web.context.ContextLoaderListener                             log4jConfigLocation          classpath:log4j.properties                      log4jRefreshInterval          3000                       org.springframework.web.util.Log4jConfigListener                           SpringEncodingFilter        org.springframework.web.filter.CharacterEncodingFilter                    encoding            UTF-8                            forceEncoding            true                            SpringEncodingFilter        *.do                            springMvc          org.springframework.web.servlet.DispatcherServlet                                    contextConfigLocation              classpath:spring/springmvc.xml                    1                      springMvc                  *.do                                             30    

b、config/mybatis/applicationContext-mybatis.xml

                                                                                                                                                                 

c、config/spring/applicationContext-dao.xml

                                           ${jdbc_driverClassName}                          ${jdbc_url}                          ${jdbc_username}                          ${jdbc_password}                                  20                                  1                                  60000                                  20                                  3                                  true                                  180                                  clientEncoding=UTF-8                                                                                                                                                                                                                                   

d、config/spring/applicationContext-service.xml

            

e、config/spring/springmvc.xml

                                                                                                                                                                                                                                                                                                                               

f、config/jdbc.properties

jdbc_driverClassName=com.mysql.jdbc.Driverjdbc_url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8jdbc_username=rootjdbc_password=111111

g、config/log4j.properties

#在开发环境下的日志级别 要设置成debug,生成环境设置成info 或errorlog4j.rootLogger=debug, stdoutlog4j.logger.org.apache.ibatis=debuglog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

h、com/jalja/springmvc_mybatis/controller/ItemsController.java

package com.jalja.springmvc_mybatis.controller;import java.io.File;import java.util.List;import java.util.UUID;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import com.jalja.springmvc_mybatis.exception.CustomException;import com.jalja.springmvc_mybatis.model.custom.ItemsCustom;import com.jalja.springmvc_mybatis.service.ItemsService;/** * 商品  * @author PC003 *conterver参数转换器  springMvc提供了很多参数转换器 */@Controller@RequestMapping("/items")  //窄化请求映射public class ItemsController {    @Autowired ItemsService itemsService;        @RequestMapping(value="/findItemsList")    public String findItemsList(Model model) throws Exception{        List itemsList=itemsService.findItemsList(null);        System.out.println(itemsList);        model.addAttribute("itemsList", itemsList);        return "itemsList";    }    @RequestMapping(value="/editItems", method={RequestMethod.POST,RequestMethod.GET}) //限制Http请求方式    //@RequestParam 将请求参数 与 形式参数进行绑定   required:指定属性必须传入值 defaultValue:设置默认值    public String editItems(Model model, @RequestParam(value="id",required=true,defaultValue="0") Integer itemsId) throws Exception{        ItemsCustom itemsCustom=itemsService.findItemsById(itemsId);        if(itemsCustom==null){            throw new CustomException("商品不存在");        }        model.addAttribute("itemsCustom", itemsCustom);        return "editItems";    }    @RequestMapping(value="/updateItems")    public String editItemsSubmit(Integer id,ItemsCustom itemsCustom,MultipartFile itemsPic) throws Exception{        String uploadFileName=itemsPic.getOriginalFilename();//获取上传的文件名        if(itemsPic!=null && uploadFileName!=null && !uploadFileName.equals("")){            String p_w_picpathsPath="E:\\develop\\upload\\p_w_picpaths\\";            String newFileName=UUID.randomUUID()+uploadFileName.substring(uploadFileName.lastIndexOf("."),uploadFileName.length());            File newFile=new File(p_w_picpathsPath+newFileName);            itemsPic.transferTo(newFile);//将内存中的数据写入磁盘            itemsCustom.setPic(newFileName);        }        itemsService.updateItemsById(id, itemsCustom);        return "redirect:findItemsList.do"; //重定向    }    //JSON的使用   @ResponseBody:将对像转json输出   @RequestBody:将请求参数转 java对象    @RequestMapping(value="/jsonRequest")    public @ResponseBody ItemsCustom jsonRequest(@RequestBody ItemsCustom itemsCustom) throws Exception{        return itemsCustom;    }    //RestFul 风格 编程     /restFulRequest/{id}:表示将这个位置的参数传到  @PathVariable 指定的名称中    @RequestMapping(value="/restFulRequest/{id}")    public @ResponseBody ItemsCustom restFulRequest(@PathVariable("id") Integer id) throws Exception{        ItemsCustom itemsCustom=itemsService.findItemsById(id);        return itemsCustom;    }}


0