springBoot整合mybatis、jsp
Spring Boot的主要优点:
1: 为所有Spring开发者更快的入门;
2:开箱即用,提供各种默认配置来简化项目配置;
3: 内嵌式容器简化Web项目;
4: 没有冗余代码生成和XML配置的要求
本项目使用到的工具:
开发工具:Intellij IDEA 2018.1.4
springboot:2.0.1.RELEASE
jdk:1.8.0_40
maven:3.3.9
开始搭建:
项目创建
finish即可。
建好后的 项目结构:
pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
5
6 com.dengwei
7 springdemo
8 0.0.1-SNAPSHOT
9 jar
10
11 springdemo
12 Demo project for Spring Boot
13
14
15 org.springframework.boot
16 spring-boot-starter-parent
17 2.0.1.RELEASE
18
19
20
21
22 UTF-8
23 UTF-8
24 1.8
25
26
27
28
29 org.springframework.boot
30 spring-boot-starter-jdbc
31
32
33 org.springframework.boot
34 spring-boot-starter-web
35
36
37 org.mybatis.spring.boot
38 mybatis-spring-boot-starter
39 1.3.2
40
41
42
43 mysql
44 mysql-connector-java
45 runtime
46
47
48 org.springframework.boot
49 spring-boot-starter-test
50 test
51
52
53
54
55 org.springframework.boot
56 spring-boot-starter-tomcat
57
58
59 org.apache.tomcat.embed
60 tomcat-embed-jasper
61
62
63
64
65
66
67 org.springframework.boot
68 spring-boot-maven-plugin
69
70
71
72
73
74
我们先建一个controller层,写一个简单的类访问一下:
HelloSpringBootController:
1 packagecom.dengwei.springdemo.controller;2
3 importorg.springframework.web.bind.annotation.RequestMapping;4 importorg.springframework.web.bind.annotation.RestController;5
6 importjava.util.HashMap;7 importjava.util.Map;8
9 @RestController10 public classHelloSpringBootController {11 @RequestMapping("/index")12 publicString hello(){13 return "hello springBoot";14 }15
16 @RequestMapping("/hello")17 public MapgetMap(){18 HashMap map = new HashMap();19 map.put("key1","姓名");20 map.put("key2","年龄");21 map.put("key3","性别");22 returnmap;23 }24
25 }
下面我们启动一下:
每一个springBoot项目中都有一个XXXAplication类,这个类就是springBoot的启动类。
注意:因为我们前面添加了数据库相关的依赖,但是我们还没有具体配置,如果直接运行的话会报错:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
解决:
在@EnableAutoConfiguretion中添加 exclude= {DataSourceAutoConfiguration.class},排除此类的autoconfig。启动以后就可以正常运行。
好的,启动没问题,我们下面正式进入mybatis与jsp的整合:
建model层:
1 packagecom.dengwei.springdemo.model;2
3 public classUser {4 privateInteger id;5 privateString userName;6 privateString password;7
8 publicInteger getId() {9 returnid;10 }11
12 public voidsetId(Integer id) {13 this.id =id;14 }15
16 publicString getUserName() {17 returnuserName;18 }19
20 public voidsetUserName(String userName) {21 this.userName =userName;22 }23
24 publicString getPassword() {25 returnpassword;26 }27
28 public voidsetPassword(String password) {29 this.password =password;30 }31
32 @Override33 publicString toString() {34 return "User{" +
35 "id=" + id +
36 ", userName='" + userName + '\'' +
37 ", password='" + password + '\'' +
38 '}';39 }40 }
View Code
2:建mapper层:
注意:我们这里的sql语句是通过注解的形式和接口写在一起的,也可以通过xml的形式配置,可以见另外一篇博客:
1 packagecom.dengwei.springdemo.mapper;2
3
4 importcom.dengwei.springdemo.model.User;5 importorg.apache.ibatis.annotations.Param;6 importorg.apache.ibatis.annotations.Select;7
8
9 public interfaceIUserMapper {10
11 @Select("SELECT id,user_name userName, pass_word password FROM user WHERE id = #{id}")12 User queryById(@Param("id") Integer id);13 }
View Code
3:建Service层:
1 packagecom.dengwei.springdemo.Service;2
3
4 importcom.dengwei.springdemo.mapper.IUserMapper;5 importcom.dengwei.springdemo.model.User;6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.stereotype.Service;8
9 @Service10 public classUserService {11 @Autowired12 privateIUserMapper userMapper;13 publicUser queryUser(Integer id){14 returnuserMapper.queryById(id);15 }16 }
View Code
4:控制层访问:
1 packagecom.dengwei.springdemo.controller;2
3
4 importcom.dengwei.springdemo.Service.UserService;5 importcom.dengwei.springdemo.model.User;6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.stereotype.Controller;8 importorg.springframework.web.bind.annotation.RequestMapping;9 importorg.springframework.web.bind.annotation.ResponseBody;10
11 @Controller12 @RequestMapping("/controller")13 public classUserController {14 @Autowired15 privateUserService userService;16
17 @RequestMapping("/user")18 @ResponseBody19 publicUser getUser(Integer id){20 User user =userService.queryUser(id);21 returnuser;22 }23
24 }
View Code
数据库连接配置文件:
1 spring.mvc.view.prefix=/WEB-INF/jsp/
2 spring.mvc.view.suffix=.jsp3 #jdbc相关4 spring.datasource.url=jdbc:mysql://localhost:3306/floor_shop
5 spring.datasource.username=root6 spring.datasource.password=admin7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
启动springBoot:
注意:前面我们排除了对数据库相关的自动配置,现在我们配置了数据库实体配置,所以把之前的排除要删掉,并且多添加@MapperScan("mapper映射文件的地址")
1 packagecom.dengwei.springdemo;2
3 importorg.mybatis.spring.annotation.MapperScan;4 importorg.springframework.boot.SpringApplication;5 importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;6 importorg.springframework.boot.autoconfigure.SpringBootApplication;7 importorg.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;8
9 @SpringBootApplication10 @EnableAutoConfiguration11 @MapperScan("com.dengwei.springdemo.mapper")12 public classSpringdemoApplication {13
14 public static voidmain(String[] args) {15 SpringApplication.run(SpringdemoApplication.class, args);16 }17 }
View Code
好的直接启动即可。上面我们简单实现了数据库的查询,增删改就自己取写一写吧。
下面我们看看springBoot整合jsp:
1、在原来的依赖中添加依赖:
org.apache.tomcat.embed
tomcat-embed-jasper
2、跳转页面:
springBoot整合thymeleaf:
org.springframework.boot
spring-boot-starter-thymeleaf
跳转页面:
不用加前后缀,可以直接跳转页面:
关于thymeleaf的基础使用参考:
在我们第一次新建的HelloSpringBootController中 新建一个helloJsp()方法,注意:我们之前用的注解是@RestController ,这个注解相当于@Controller + @ResponseBody
表示标注的类或则方法返回的都是json格式的,而我们这次需要访问jsp页面所以需要换成@Controller注解。在需要的返回json格式的方法上添加@ResponseBody,而不是整个类的所有方法都返回json格式。
下面我们看一下springBoot中的全局异常捕获:
异常捕获的核心标签:@ControllerAdvice + @ExceptionHandler(RuntimeException.class)
1 packagecom.dengwei.springdemo.controller;2
3 importorg.springframework.web.bind.annotation.ControllerAdvice;4 importorg.springframework.web.bind.annotation.ExceptionHandler;5 importorg.springframework.web.bind.annotation.RequestMapping;6
7 @ControllerAdvice8 public classGlobalExceptionHandler {9 @ExceptionHandler(RuntimeException.class)10 @RequestMapping11 publicString errorPage(){12 return "index";13 }14 }
View Code
好了,就先到这儿吧!