(废弃)整合springboot+MVC+Mybatis(废弃)

*该整合方法过于复杂,本文废弃。新方法:https://blog.csdn.net/liboyang71/article/details/73459909

没有使用thymeledf之类的模板,但springboot是推荐使用模板,不推荐使用jsp的。并且springboot本身不支持jsp,我们使用jsp时需要导入额外的包进行支持。

n1:g创建一'q个mavien工程

2并且:引入所需的所有jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cn.itcast.springboot</groupId>
	<artifactId>springboot-mybatis-mvc</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>war</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>

	<properties>
		<!-- Generic properties -->
		<webVersion>3.0</webVersion> 
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
		<java.version>1.8</java.version>
		 <!-- Web -->
		 <jsp.version>2.2</jsp.version>
		 <jstl.version>1.2</jstl.version>
		 <servlet.version>3.1.0</servlet.version>
		 <!-- spring -->
		 <spring-framework.version>4.1.5.RELEASE</spring-framework.version>
		 <!-- Logging -->
		 <logback.version>1.0.13</logback.version>
		 <slf4j.version>1.7.5</slf4j.version>
	</properties>
	
	<dependencies>
		<!-- spingboot核心 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!-- springboot整合测试 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- springboot整合web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- springboot整合mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!-- springboot整合通用mapper(包含了通用mapper的jar包) -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper-spring-boot-starter</artifactId>
			<version>1.1.7</version>
		</dependency>
		
		<!-- web-api 与tomcattomcat-embed-jasper冲突 -->
	<!-- 	<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-web-api</artifactId>
			<version>7.0</version>
			<scope>provided</scope>
		</dependency> -->
		<!-- springMVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			</dependency>
		<!-- 其他web依赖 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- 与tomcattomcat-embed-jasper冲突 -->
		<!-- <dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.version}</version>
		</dependency> -->
		<!-- Spring and Transactions -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			</dependency>
		<!-- 使用SLF4J和LogBack作为日志 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-access</artifactId>
			</dependency> 
		
		<!-- tomcat&jsp支持 -->
		<dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.tomcat.embed</groupId>
           <artifactId>tomcat-embed-jasper</artifactId>
           <scope>provided</scope>
       </dependency>
		
		
		
		<!-- mysql连接 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- junit测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		
		
		 
	</dependencies>
	<build>
		<plugins>
			<!-- 设置maven工程jdk版本的插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
			<!-- 把工程打包成jar包的插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		
		
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

3:创建包结构

4:整合mybatis和通用mapper

见我的另一篇博文:https://blog.csdn.net/xiaoshuo566/article/details/84560937

5:整合springMVC

5.1:springmvc配置类

package cn.itcast;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
 * springMVC配置类
 * @author Mr.Tong
 *
 */
@Configuration
@EnableWebMvc //会开启一些默认配置,如ViewResolver和MessageConverter等
@Component("cn.itcast")
public class MyMvcConfig {
	@Bean
	public InternalResourceViewResolver viewResolver(){
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
		//一般springboot的web开发使用模板,如thymeleaf模板,此时就不需要这样配置。
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		viewResolver.setViewClass(JstlView.class);
		return viewResolver;
	}
}

5.2:Web容器初始化类:

package cn.itcast;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/*WebApplicationInitializer 是spring提供用来配置servlet3.0+ 的接口,从而实现了代替web.xml的位置。实现此接口的
 * 类将会自动被springServletContainerInitializer(用来启动servlet3.0容器)获取到。 
*/
public class WebInitializer implements WebApplicationInitializer{

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		//新建WebApplicationContext
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.register(MyMvcConfig.class); //注册springMVC配置类
		ctx.setServletContext(servletContext);//与当前servletContext关联
		
		//注册Springmvc的DispatchServlet
		Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
		servlet.addMapping("/");//设置拦截路径
		servlet.setLoadOnStartup(1);//
	}
	
}

这个类代替了原本的web.xml

6:编写日志文件logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置日志 --> 
<configuration  debug="false" scan="true" scanPeriod="30 second">
    
    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
    	<resetJUL>true</resetJUL>
    </contextListener>
	<jmxConfigurator/>
	<!-- 日志输出的通道 -->
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>logback: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
		</encoder>
	</appender>
	<!-- 指定某一个包或者某一个类的打印级别以及是否传入root进行打印 -->
    <!-- addtivity:是否向上级loger传递打印信息。默认是true。-->
    <!-- <loger>可以包含零个或多个<appender-ref>元素,标识这个appender将会添加到这个loger。-->
    <!-- name:用来指定受此loger约束的某一个包或者具体的某一个类。-->
    <!-- level:
        用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,还有一个特俗值INHERITED或者同义词NULL,代表强制执行上级的级别。
        如果未设置此属性,那么当前loger将会继承上级的级别。--> 
	<logger name="org.springframework.web" level="DEBUG"/>
	 <!-- 也是<loger>元素,但是它是根loger。只有一个level属性,应为已经被命名为"root". -->
	<root level="info">
		<appender-ref ref="console"></appender-ref>
	</root>
</configuration>

7.编写jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<pre>
			Welcome to spring mvc world!11
							------WEB-INF/views
		</pre>
		<div style="border: 1;border-color: black">
			<form action="/hello">
			要查询的id:<input type="text" name="id"/>
			<input type="submit" value="查询"/>
			</form>
		</div>
</body>
</html>

这个页面要放到webapp/WEB-INF/views/ 下。

8.编写controller

package cn.itcast.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.pojo.User;
import cn.itcast.service.UserService;

@Controller
public class PageController {
	@Autowired
	private UserService userService;
	
	 //测试jsp页面跳转
    @RequestMapping("/index")
    public String toIndex(){
    	return "index";
    }
    //获取表单数据
    @RequestMapping("hello")
    @ResponseBody
    public String getMsg(@RequestParam(name="id")String id){
    	Long key = Long.valueOf(id);
    	
    	User user = userService.selectByPrimaryKey(key);
    	return user.toString();
    }
}

9.运行入口类,进行测试

在入口类进行run,然后打开浏览器输入 localhost:8080/index

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值