SpringMVC mybatis整合实例代码详解

网友投稿 255 2023-05-23


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"

passaspku.com/pc/softtech/office/word/" target="_blank">word="jalja">

connectionURL="jdbc:mysql://localhost:3307/mybatis" userId="root"

passaspku.com/pc/softtech/office/word/" target="_blank">word="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 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

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">

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

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">

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

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">

${jdbc_driverClassName}

${jdbc_url}

${jdbc_username}

${jdbc_password}

20

1

60000

20

3

true

180

clientEncoding=UTF-8

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">

${jdbc_driverClassName}

${jdbc_url}

${jdbc_username}

${jdbc_password}

20

1

60000

20

3

true

180

clientEncoding=UTF-8

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 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 imagesPaTczhQipDth="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;

}

}

希望本篇实例对您有所帮助


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java Map 在put值时value值不被覆盖的解决办法
下一篇:微信小程序实战之登录页面制作(5)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~