Spring Boot + MyBatis 快速入门
这几天学习了下Spring Boot,想记录下来以备以后复习查看用。Spring Boot极度简化了以往XML配置的复杂繁琐,只需简单的几行配置就可以搭建一个Web工程,大大节省了开发时间。
环境准备
- Eclipse
- JDK 1.7
- Maven
- MySql
一个最简单的Web应用
首先新建一个Maven工程,下面是pom.xml的配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--启动时启动内置tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!--对Jsp支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- 支持jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- MySql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
然后新建一个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("=================启动成功=================");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
然后写一个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 !";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
因为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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
附上工程结构,代码结构和普通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("=================启动成功=================");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
编写测试代码
@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";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
在WEB-INF/views/下新建item.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>详情</title>
</head>
<body>
获取成功:${user.name }
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
启动工程。访问http://localhost:8080/getItem?id=1
数据库数据
这时Spring Boot + MyBatis的项目就搭建好了。