博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC与mybatis整合
阅读量:4339 次
发布时间:2019-06-07

本文共 9567 字,大约阅读时间需要 31 分钟。

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

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 imagesPath="E:\\develop\\upload\\images\\"; String newFileName=UUID.randomUUID()+uploadFileName.substring(uploadFileName.lastIndexOf("."),uploadFileName.length()); File newFile=new File(imagesPath+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; }}

 

转载于:https://www.cnblogs.com/jalja/p/5481530.html

你可能感兴趣的文章
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
新部署到服务器 报 The requested URL /home/profession was not found on this server. 错误
查看>>
hadoop从非HA转到NAMENODE HA时需要注意的一个问题
查看>>
KnockoutJs学习笔记(十一)
查看>>
访问修饰符public、private、protect、default范围
查看>>
jQuery实现布局高宽自适应
查看>>
《学习》10函数的建立与使用
查看>>
去除TB二合一页面弹窗
查看>>
算法第四章实践报告
查看>>
牛客练习赛29 B
查看>>