Flask接口签名sign原理与实例代码浅析
186
2023-07-17
SpringMVC整合mybatis实例代码
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。
一、逆向工程生成基础信息
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
connectionURL="jdbc:mysql://localhost:3307/mybatis" userId="root" password="jalja">
connectionURL="jdbc:mysql://localhost:3307/mybatis" userId="root"
password="jalja">
targetProject=".\src">
targetProject=".\src">
targetProject=".\src">
targetProject=".\src">
targetPackage="com.jalja.springmvc_mybatis.mapper" targetProject=".\src">
targetPackage="com.jalja.springmvc_mybatis.mapper"
targetProject=".\src">
public static void main(String[] arhs) throws Exception{
List
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
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
b、config/mybatis/applicationContext-mybatis.xml
c、config/spring/applicationContext-dao.xml
xmlns:aop="http://springframework.org/schema/aop" xmlns:cache="http://springframework.org/schema/cache" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:tx="http://springframework.org/schema/tx" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.2.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.2.xsd http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.2.xsd">
xmlns:aop="http://springframework.org/schema/aop" xmlns:cache="http://springframework.org/schema/cache"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc" xmlns:tx="http://springframework.org/schema/tx"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.2.xsd
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.2.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.2.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-3.2.xsd
http://springframework.org/schema/cache
http://springframework.org/schema/cache/spring-cache-3.2.xsd">
d、config/spring/applicationContext-service.xml
xmlns:aop="http://springframework.org/schema/aop" xmlns:cache="http://springframework.org/schema/cache" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:tx="http://springframework.org/schema/tx" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.2.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.2.xsd http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.2.xsd">
xmlns:aop="http://springframework.org/schema/aop" xmlns:cache="http://springframework.org/schema/cache"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc" xmlns:tx="http://springframework.org/schema/tx"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.2.xsd
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.2.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.2.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-3.2.xsd
http://springframework.org/schema/cache
http://springframework.org/schema/cache/spring-cache-3.2.xsd">
e、config/spring/springmvc.xml
xmlns:aop="http://springframework.org/schema/aop" xmlns:cache="http://springframework.org/schema/cache" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:tx="http://springframework.org/schema/tx" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.2.xsd">
xmlns:aop="http://springframework.org/schema/aop" xmlns:cache="http://springframework.org/schema/cache"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc" xmlns:tx="http://springframework.org/schema/tx"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.0.xsd
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.0.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-3.0.xsd
http://springframework.org/schema/cache
http://springframework.org/schema/cache/spring-cache-3.2.xsd">
f、config/jdbc.properties
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
jdbc_username=root
jdbc_password=111111
g、config/log4j.properties
#在开发环境下的日志级别 要设置成debug,生成环境设置成info 或error
log4j.rootLogger=debug, stdout
log4j.logger.org.apache.ibatis=debug
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.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
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;
}
}
以上所述是给大家介绍的SpringMVC整合mybatis实例代码,希望对大家有所帮助,如果大家想了解更多资讯敬请关注我们网站!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~