4.SpringBoot实战 接下来,我们来看看如何用SpringBoot来玩转以前的SSM,我们沿用之前讲解SSM用到的数据库tb_user和实体类User
4.1.创建工程
4.2.编写基本代码
pom.xml:
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 <?xml version="1.0" encoding="UTF-8"?> <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.user</groupId > <artifactId > itcast-user</artifactId > <version > 1.0-SNAPSHOT</version > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.0.6.RELEASE</version > </parent > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > </dependencies > </project >
参照上边的项目,编写引导类:
1 2 3 4 5 6 7 @SpringBootApplication public class UserApplication { public static void main (String[] args) { SpringApplication.run(UserApplication.class ) ; } }
编写UserController:
1 2 3 4 5 6 7 8 9 @RestController @RequestMapping ("user" )public class UserController { @GetMapping ("hello" ) public String test () { return "hello ssm" ; } }
4.3.整合SpringMVC 虽然默认配置已经可以使用SpringMVC了,不过我们有时候需要进行自定义配置。
4.3.1.修改端口 添加全局配置文件:application.properties
端口通过以下方式配置
重启服务后测试:
4.3.2.访问静态资源 现在,我们的项目是一个jar工程,那么就没有webapp,我们的静态资源该放哪里呢?
回顾我们上面看的源码,有一个叫做ResourceProperties的类,里面就定义了静态资源的默认查找路径:
默认的静态资源路径为:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
只要静态资源放在这些目录中任何一个,SpringMVC都会帮我们处理。
我们习惯会把静态资源放在classpath:/static/
目录下。我们创建目录,并且添加一些静态资源:
重启项目后测试:
4.3.3.添加拦截器 拦截器也是我们经常需要使用的,在SpringBoot中该如何配置呢?
拦截器不是一个普通属性,而是一个类,所以就要用到java配置方式了。在SpringBoot官方文档中有这么一段说明:
If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration
class of type WebMvcConfigurer
but without @EnableWebMvc
. If you wish to provide custom instances of RequestMappingHandlerMapping
, RequestMappingHandlerAdapter
, or ExceptionHandlerExceptionResolver
, you can declare a WebMvcRegistrationsAdapter
instance to provide such components.
If you want to take complete control of Spring MVC, you can add your own @Configuration
annotated with @EnableWebMvc
.
翻译:
如果你想要保持Spring Boot 的一些默认MVC特征,同时又想自定义一些MVC配置(包括:拦截器,格式化器, 视图控制器、消息转换器 等等),你应该让一个类实现WebMvcConfigurer
,并且添加@Configuration
注解,但是千万不要 加@EnableWebMvc
注解。如果你想要自定义HandlerMapping
、HandlerAdapter
、ExceptionResolver
等组件,你可以创建一个WebMvcRegistrationsAdapter
实例 来提供以上组件。
如果你想要完全自定义SpringMVC,不保留SpringBoot提供的一切特征,你可以自己定义类并且添加@Configuration
注解和@EnableWebMvc
注解
总结:通过实现WebMvcConfigurer
并添加@Configuration
注解来实现自定义部分SpringMvc配置。
实现如下:
首先我们定义一个拦截器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle method is running!" ); return true ; } @Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle method is running!" ); } @Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion method is running!" ); } }
然后定义配置类,注册拦截器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Configuration public class MvcConfiguration implements WebMvcConfigurer { @Autowired private HandlerInterceptor myInterceptor; @Override public void addInterceptors (InterceptorRegistry registry) { registry.addInterceptor(myInterceptor).addPathPatterns("/**" ); } }
接下来运行并查看日志:
1 2 3 preHandle method is running! postHandle method is running! afterCompletion method is running!
你会发现日志中只有这些打印信息,springMVC的日志信息都没有,因为springMVC记录的log级别是debug,springboot默认是显示info以上,我们需要进行配置。
SpringBoot通过logging.level.*=debug
来配置日志级别,*填写包名
1 2 logging.level.org.springframework =debug
再次运行查看:
4.4.整合连接池 jdbc连接池是spring配置中的重要一环,在SpringBoot中该如何处理呢?
答案是不需要处理,我们只要找到SpringBoot提供的启动器即可:
在pom.xml中引入jdbc的启动器:
1 2 3 4 5 6 7 8 9 10 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-jdbc</artifactId > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > </dependency >
SpringBoot已经自动帮我们引入了一个连接池:
HikariCP应该是目前速度最快的连接池了,我们看看它与c3p0的对比:
因此,我们只需要指定连接池参数即可:
1 2 3 4 5 6 7 8 9 10 spring.datasource.url =jdbc:mysql://localhost:3306/heima spring.datasource.username =root spring.datasource.password =root spring.datasource.driverClassName =com.mysql.jdbc.Driver spring.datasource.hikari.idle-timeout =60000 spring.datasource.hikari.maximum-pool-size =30 spring.datasource.hikari.minimum-idle =10
当然,如果你更喜欢Druid连接池,也可以使用Druid官方提供的启动器:
1 2 3 4 5 6 <dependency > <groupId > com.alibaba</groupId > <artifactId > druid-spring-boot-starter</artifactId > <version > 1.1.6</version > </dependency >
而连接信息的配置与上面是类似的,只不过在连接池特有属性上,方式略有不同:
1 2 3 4 5 6 7 8 9 10 spring.datasource.druid.initial-size =1 spring.datasource.druid.min-idle =1 spring.datasource.druid.max-active =20 spring.datasource.druid.test-on-borrow =true spring.datasource.druid.stat-view-servlet.allow =true
4.5.整合mybatis 4.5.1.mybatis SpringBoot官方并没有提供Mybatis的启动器,不过Mybatis官方 自己实现了:
1 2 3 4 5 6 <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 1.3.2</version > </dependency >
配置,基本没有需要配置的:
1 2 3 4 mybatis.type-aliases-package =cn.itcast.pojo mybatis.mapper-locations =classpath:mappers/*.xml
需要注意,这里没有配置mapper接口扫描包,因此我们需要给每一个Mapper接口添加@Mapper
注解,才能被识别。
1 2 3 @Mapper public interface UserMapper {}
user对象参照课前资料,需要通用mapper的注解:
接下来,就去集成通用mapper。
4.5.2.通用mapper 通用Mapper的作者也为自己的插件编写了启动器,我们直接引入即可:
1 2 3 4 5 6 <dependency > <groupId > tk.mybatis</groupId > <artifactId > mapper-spring-boot-starter</artifactId > <version > 2.0.2</version > </dependency >
不需要做任何配置就可以使用了。
1 2 3 @Mapper public interface UserMapper extends tk .mybatis .mapper .common .Mapper <User > {}
4.6.整合事务 其实,我们引入jdbc或者web的启动器,就已经引入事务相关的依赖及默认配置了
至于事务,SpringBoot中通过注解来控制。就是我们熟知的@Transactional
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Service public class UserService { @Autowired private UserMapper userMapper; public User queryById (Long id) { return this .userMapper.selectByPrimaryKey(id); } @Transactional public void deleteById (Long id) { this .userMapper.deleteByPrimaryKey(id); } }
4.7.启动测试 在UserController中添加测试方法,内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @RestController @RequestMapping ("user" )public class UserController { @Autowired private UserService userService; @GetMapping ("{id}" ) public User queryUserById (@PathVariable("id" ) Long id) { return this .userService.queryById(id); } @GetMapping ("hello" ) public String test () { return "hello ssm" ; } }
我们启动项目,查看:
4.8.完整项目结构
完整的pom.xml:
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 <?xml version="1.0" encoding="UTF-8"?> <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.user</groupId > <artifactId > itcast-user</artifactId > <version > 1.0-SNAPSHOT</version > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.0.6.RELEASE</version > </parent > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-jdbc</artifactId > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > </dependency > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 1.3.2</version > </dependency > <dependency > <groupId > tk.mybatis</groupId > <artifactId > mapper-spring-boot-starter</artifactId > <version > 2.0.2</version > </dependency > </dependencies > </project >
完整的application.properties:
1 2 3 4 5 6 7 8 9 10 11 12 server.port =80 logging.level.org.springframework =debug spring.datasource.url =jdbc:mysql://localhost:3306/heima spring.datasource.username =root spring.datasource.password =root mybatis.type-aliases-package =cn.itcast.pojo