Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Integration of SpringMVC and mybatis

2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

I. reverse engineering to generate basic information

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);}

Second, springMVC and Mybatis integrate various configuration files

1. Project structure

2. The core code of each file

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

# the log level in the development environment should be set to debug, the build environment should be set to info or errorlog4j.rootLogger=debug, and 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;/** * merchandise * @ author PC003 * conterver parameter converter springMvc provides many parameter converters * / @ Controller@RequestMapping ("/ items") / / narrowing request mapping 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}) / / restrict the Http request method / / @ RequestParam bind the request parameter to the form parameter required: specify that the attribute must pass a value defaultValue: set the default value 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 ("goods do not exist");} model.addAttribute ("itemsCustom", itemsCustom); return "editItems";} @ RequestMapping (value= "/ updateItems") public String editItemsSubmit (Integer id,ItemsCustom itemsCustom,MultipartFile itemsPic) throws Exception {String uploadFileName=itemsPic.getOriginalFilename () / / get the name of the uploaded file if (itemsPictures: null & & uploadFileNameplate null &! uploadFileName.equals ("")) {String paired picpathsPathology = "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) / / write data in memory to disk itemsCustom.setPic (newFileName);} itemsService.updateItemsById (id, itemsCustom); return "redirect:findItemsList.do" / / redirect} / / the use of JSON @ ResponseBody: transfer the object to json output @ RequestBody: transfer the request parameters to java object @ RequestMapping (value= "/ jsonRequest") public @ ResponseBody ItemsCustom jsonRequest (@ RequestBody ItemsCustom itemsCustom) throws Exception {return itemsCustom } / / RestFul style programming / restFulRequest/ {id}: means to pass the parameters of this location to the name specified by @ PathVariable @ RequestMapping (value= "/ restFulRequest/ {id}") public @ ResponseBody ItemsCustom restFulRequest (@ PathVariable ("id") Integer id) throws Exception {ItemsCustom itemsCustom=itemsService.findItemsById (id); return itemsCustom;}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report