这几天学习了下Spring Boot,想记录下来以备以后复习查看用。Spring Boot极度简化了以往XML配置的复杂繁琐,只需简单的几行配置就可以搭建一个Web工程,大大节省了开发时间。
环境准备
一个最简单的Web应用
首先新建一个Maven工程,下面是pom.xml的配置
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.4.1.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
dependency>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-jasperartifactId>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.21version>
dependency>
dependencies>
然后新建一个Application.java
//此注解表示SpringBoot启动类
@SpringBootApplication
public class Application {
private static Logger logger = Logger.getLogger(Application.class);
public static void main(String[] args) {
logger.info("=================开始成功=================");
SpringApplication.run(Application.class, args);
logger.info("=================启动成功=================");
}
}
然后写一个Controller类
@Controller
public class IndexController {
private static Logger logger = Logger.getLogger(IndexController.class);
@RequestMapping("/")
@ResponseBody
public String index() {
logger.info("进入index方法");
return "Hello Spring Boot !";
}
}
因为Spring Boot内置了Tomcat,这时我们从Application.java的main方法启动就可以启动Spring Boot的项目了。
这时我们的第一个Spring Boot项目就启动成功了,在浏览器输入http://localhost:8080/就可以访问到刚才Controller里面的Index方法了。
下面开始整合MyBatis,因为上面已经引入了Maven依赖,所以这里我们直接开始整合
在resources目录下新建config文件夹,新建application.properties配置文件
#设置Tomcat端口,默认8080
server.port=8080
#设置项目ContextPath
server.context-path=/
#设置Tomcat编码
server.tomcat.uri-encoding=UTF-8
#设置视图解析器路径
spring.mvc.view.prefix=/WEB-INF/views/
#设置视图解析器后缀
spring.mvc.view.suffix=.jsp
#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/dd?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#配置.xml文件路径
mybatis.mapper-locations=classpath:/com/dd/wx/mapper/*.xml
#配置模型路径
mybatis.type-aliases-package=com.dd.wx.model
附上工程结构,代码结构和普通SpringMVC工程结构一样
再Application.java方法中加上注解
//此注解表示动态扫描DAO接口所在包
@MapperScan("com.dd.wx.dao")
//此注解表示SpringBoot启动类
@SpringBootApplication
public class Application {
private static Logger logger = Logger.getLogger(Application.class);
public static void main(String[] args) {
logger.info("=================开始成功=================");
SpringApplication.run(Application.class, args);
logger.info("=================启动成功=================");
}
}
编写测试代码
@Autowired
private IUserService userService;
@RequestMapping("/getItem")
public String getItem(HttpServletRequest request) {
String id = request.getParameter("id");
logger.info("进入getItem方法,id:"+id);
User user = userService.getUser(id);
request.setAttribute("user", user);
return "/item";
}
在WEB-INF/views/下新建item.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>详情title>
head>
<body>
获取成功:${user.name }
body>
html>
启动工程。访问http://localhost:8080/getItem?id=1
数据库数据
这时Spring Boot + MyBatis的项目就搭建好了。
源码地址:http://download.csdn.net/download/qq_17029285/10150551