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 24 4.0.0 5 6com.dengwei 7springdemo 80.0.1-SNAPSHOT 9jar 10 11springdemo 12Demo project for Spring Boot 13 1415 20 21org.springframework.boot 16spring-boot-starter-parent 172.0.1.RELEASE 1819 22 26 27UTF-8 23UTF-8 241.8 2528 63 6429 32org.springframework.boot 30spring-boot-starter-jdbc 3133 36org.springframework.boot 34spring-boot-starter-web 3537 41 42org.mybatis.spring.boot 38mybatis-spring-boot-starter 391.3.2 4043 47mysql 44mysql-connector-java 45runtime 4648 52 53 54org.springframework.boot 49spring-boot-starter-test 50test 5155 58org.springframework.boot 56spring-boot-starter-tomcat 5759 62org.apache.tomcat.embed 60tomcat-embed-jasper 6165 72 73 7466 7167 70org.springframework.boot 68spring-boot-maven-plugin 69
我们先建一个controller层,写一个简单的类访问一下:
HelloSpringBootController:
1 package com.dengwei.springdemo.controller; 2 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 import java.util.HashMap; 7 import java.util.Map; 8 9 @RestController10 public class HelloSpringBootController {11 @RequestMapping("/index")12 public String 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 return map;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 classAction: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 package com.dengwei.springdemo.model; 2 3 public class User { 4 private Integer id; 5 private String userName; 6 private String password; 7 8 public Integer getId() { 9 return id;10 }11 12 public void setId(Integer id) {13 this.id = id;14 }15 16 public String getUserName() {17 return userName;18 }19 20 public void setUserName(String userName) {21 this.userName = userName;22 }23 24 public String getPassword() {25 return password;26 }27 28 public void setPassword(String password) {29 this.password = password;30 }31 32 @Override33 public String toString() {34 return "User{" +35 "id=" + id +36 ", userName='" + userName + '\'' +37 ", password='" + password + '\'' +38 '}';39 }40 }
2:建mapper层:
注意:我们这里的sql语句是通过注解的形式和接口写在一起的,也可以通过xml的形式配置,可以见另外一篇博客:
1 package com.dengwei.springdemo.mapper; 2 3 4 import com.dengwei.springdemo.model.User; 5 import org.apache.ibatis.annotations.Param; 6 import org.apache.ibatis.annotations.Select; 7 8 9 public interface IUserMapper {10 11 @Select("SELECT id,user_name userName, pass_word password FROM user WHERE id = #{id}")12 User queryById(@Param("id") Integer id);13 }
3:建Service层:
1 package com.dengwei.springdemo.Service; 2 3 4 import com.dengwei.springdemo.mapper.IUserMapper; 5 import com.dengwei.springdemo.model.User; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 @Service10 public class UserService {11 @Autowired12 private IUserMapper userMapper;13 public User queryUser(Integer id){14 return userMapper.queryById(id);15 }16 }
4:控制层访问:
1 package com.dengwei.springdemo.controller; 2 3 4 import com.dengwei.springdemo.Service.UserService; 5 import com.dengwei.springdemo.model.User; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.ResponseBody;10 11 @Controller12 @RequestMapping("/controller")13 public class UserController {14 @Autowired15 private UserService userService;16 17 @RequestMapping("/user")18 @ResponseBody19 public User getUser(Integer id){20 User user = userService.queryUser(id);21 return user;22 }23 24 }
数据库连接配置文件:
1 spring.mvc.view.prefix=/WEB-INF/jsp/2 spring.mvc.view.suffix=.jsp3 #jdbc相关4 spring.datasource.url=jdbc:mysql://localhost:3306/floor_shop5 spring.datasource.username=root6 spring.datasource.password=admin7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
启动springBoot:
注意:前面我们排除了对数据库相关的自动配置,现在我们配置了数据库实体配置,所以把之前的排除要删掉,并且多添加@MapperScan("mapper映射文件的地址")
1 package com.dengwei.springdemo; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 import org.springframework.boot.autoconfigure.SpringBootApplication; 7 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 8 9 @SpringBootApplication10 @EnableAutoConfiguration11 @MapperScan("com.dengwei.springdemo.mapper")12 public class SpringdemoApplication {13 14 public static void main(String[] args) {15 SpringApplication.run(SpringdemoApplication.class, args);16 }17 }
好的直接启动即可。上面我们简单实现了数据库的查询,增删改就自己取写一写吧。
下面我们看看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 package com.dengwei.springdemo.controller; 2 3 import org.springframework.web.bind.annotation.ControllerAdvice; 4 import org.springframework.web.bind.annotation.ExceptionHandler; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 @ControllerAdvice 8 public class GlobalExceptionHandler { 9 @ExceptionHandler(RuntimeException.class)10 @RequestMapping11 public String errorPage(){12 return "index";13 }14 }
好了,就先到这儿吧!