摘要:在實戰一書中前面兩部分分別介紹了和的高級特性,并且基于類配置有一套層的,但是沒有將層整合層,于是我試著整合了下,也方便以后寫測試。
在《springBoot實戰》 一書中前面兩部分分別介紹了spring 和 springMVC的高級特性,并且基于java類配置有一套web層的demo,但是沒有將web層整合dao層,于是我試著整合了下,也方便以后寫測試demo。下面是我的整理
pom.xml(一些基礎的jar包,可能有些在這個demo中不會用到)
4.0.0 com.tao springmvc_demo_simple 1.0-SNAPSHOT war 1.7 UTF-8 UTF-8 2.2 1.2 3.1.0 4.1.5.RELEASE 1.0.13 1.7.5 javax javaee-web-api 7.0 provided org.springframework spring-webmvc ${spring-framework.version} javax.servlet jstl ${jstl.version} javax.servlet javax.servlet-api ${servlet.version} provided javax.servlet.jsp jsp-api ${jsp.version} provided org.springframework spring-tx ${spring-framework.version} org.slf4j slf4j-api ${slf4j.version} log4j log4j 1.2.16 org.slf4j jcl-over-slf4j ${slf4j.version} ch.qos.logback logback-classic ${logback.version} ch.qos.logback logback-core ${logback.version} ch.qos.logback logback-access ${logback.version} com.fasterxml.jackson.dataformat jackson-dataformat-xml 2.5.3 com.alibaba fastjson 1.1.41 commons-fileupload commons-fileupload 1.3.1 commons-io commons-io 2.3 org.springframework spring-test ${spring-framework.version} test junit junit 4.11 test org.mybatis mybatis 3.2.6 org.mybatis mybatis-spring 1.2.2 mysql mysql-connector-java 5.1.6 commons-dbcp commons-dbcp 1.2.2 org.springframework spring-jdbc ${spring-framework.version} org.springframework spring-context-support ${spring-framework.version} aspectj aspectjweaver 1.5.3 org.apache.maven.plugins maven-compiler-plugin 2.3.2 ${java.version} org.apache.maven.plugins maven-war-plugin 2.3 false
配置類(類似于springMVC.xml)
@Configuration @EnableWebMvc @EnableScheduling @ComponentScan(basePackages = { "com.tao.demo.web" }) public class MyMvcConfig extends WebMvcConfigurerAdapter{ @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/classes/views/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(JstlView.class); return viewResolver; } @Bean public MultipartResolver multipartResolver(){ CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); commonsMultipartResolver.setMaxUploadSize(1000000); return commonsMultipartResolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //靜態資源 registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/"); } @Bean public DemoInterceptor demoInterceptor(){ return new DemoInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(demoInterceptor()); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("/index"); } @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseSuffixPatternMatch(false); } @Override public void extendMessageConverters(List> converters) { converters.add( new MyMessageConverter()); } }
攔截器
public class DemoInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { long startTime = System.currentTimeMillis(); request.setAttribute("startTime",startTime); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { long startTime = (Long)request.getAttribute("startTime"); request.removeAttribute("startTime"); long endTime = System.currentTimeMillis(); System.out.println("本次請求處理時間"+new Long(endTime-startTime)+"ms"); request.setAttribute("handingTime",endTime-startTime); } }
自定義消息轉換器
public class MyMessageConverter extends AbstractHttpMessageConverter{ public MyMessageConverter() { super(new MediaType("application","x-tao", Charset.forName("UTF-8"))); } @Override protected boolean supports(Class> aClass) { return DemoObj.class.isAssignableFrom(aClass); } @Override protected DemoObj readInternal(Class extends DemoObj> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { String s = StreamUtils.copyToString(httpInputMessage.getBody(), Charset.forName("UTF-8")); String[] split = s.split("-"); return new DemoObj(Long.valueOf(split[0]),split[1]); } @Override protected void writeInternal(DemoObj obj, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException { String s = "hello:" + obj.getId() + "-" + obj.getName(); httpOutputMessage.getBody().write(s.getBytes()); } }
初始化(相當于web.xml)
public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(MyMvcConfig.class); context.setServletContext(servletContext); ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(context)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); servlet.setAsyncSupported(true); // 整合dao層的時候需要 // servletContext.setInitParameter("contextConfigLocation","classpath:spring-mybatis.xml"); // servletContext.addListener(ContextLoaderListener.class); } }
控制層
@Controller public class HelloController { @RequestMapping("index") public String hello(){ System.out.println("hello world"); return "index"; } }
view
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here Welcome to Spring MVC world!
整體目錄結構
運行結果
基于注解方式配置springMVC 并整合mybatis(二)
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/67877.html
摘要:基于注解方式配置并整合一接上篇文章,如下是整合數據層。整合時,如果不加上就無法啟動容器。 基于注解方式配置springMVC 并整合mybatis(一) 接上篇文章,如下是整合數據層。 spring-mybatis.xml ...
摘要:跳槽時時刻刻都在發生,但是我建議大家跳槽之前,先想清楚為什么要跳槽。切不可跟風,看到同事一個個都走了,自己也盲目的開始面試起來期間也沒有準備充分,到底是因為技術原因影響自己的發展,偏移自己規劃的軌跡,還是錢給少了,不受重視。 跳槽時時刻刻都在發生,但是我建議大家跳槽之前,先想清楚為什么要跳槽。切不可跟風,看到同事一個個都走了,自己也盲目的開始面試起來(期間也沒有準備充分),到底是因為技...
ssm整合 開發環境ide,mysql數據庫,Spring+SpringMVC+Mybatis,tomcat8.5,jdk使用的是1.7版本。 第一步:導入jar包 Spring+ SpringMVC + MyBatis + Mybatis-spring整合包 AOP聯盟+織入 + c3p0 數據庫連接池 + MySQL連接驅動 + jstl 鏈接:https://pan.baidu.com/...
閱讀 2902·2023-04-26 02:14
閱讀 3751·2019-08-30 15:55
閱讀 1843·2019-08-29 16:42
閱讀 2757·2019-08-26 11:55
閱讀 2846·2019-08-23 13:38
閱讀 480·2019-08-23 12:10
閱讀 1308·2019-08-23 11:44
閱讀 2790·2019-08-23 11:43