千家信息网

SpringMVC与mybatis整合

发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,一、逆向工程生成基础信息
千家信息网最后更新 2025年12月01日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;    }}


参数 配置 文件 整合 商品 环境 转换器 生成 代码 位置 信息 内存 前端 名称 基础 对象 射器 将对 属性 工程 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 为什么管家婆显示服务器出错 长宁区一站式软件开发经验丰富 o2o软件开发工程师 小度看网络安全手抄报又简单 清理sql数据库 温州企业进销存软件开发 有奖互动我为网络安全助力 使用密钥管理服务服务器 巴彦淖尔商城分销软件开发费用 盈环网络技术两江 拱墅区管理软件开发 联通网络技术研究院 面试 中学生网络安全基础知识学习资料 网警开展网络安全检查的举措 康保县委网络安全 网络安全深入解读 网络安全存储服务器机箱生产 自己的笔记本做服务器 计算机网络技术专业开设课程 网络安全靠大家新闻稿 农行数据库中无记录 软件开发5年后还能考研吗 数据库字段为null oracle数据库日期查询格式 it和软件开发哪个好 安徽正规软件开发价格服务标准 现在挖矿要租服务器吗 什么叫数据库的安全管理制度 浪潮服务器默认管理口 一个游戏服务器能同时在线多少人
0