一.前情回顾
通过上一节,我们知道在SpringBoot项目中可以有两种SSM整合的方式:
- 1️⃣. XML方式;
- 2️⃣. 注解两种方式.
其中以xml方式进行整合实现的过程,比较麻烦,接下来我们讲解一下注解方式的实现过程。
我们重新创建一个新的案例demo09,具体的创建过程及其依赖包等内容,请参考上一篇文章!
二. 在SpringBoot中以注解方式进行SSM整合
1. 在StudentMapper类中添加注解
以注解方式在SpringBoot中整合ssm时,可以把StudentMapper.xml文件删除掉,但是也得在pom.xml文件中设置允许Java文件的xml可编译,也就是仍然需要配置如下项:
src/main/java**/*.xml
2. 修改application.properties文件
#数据源配置spring.datasource.username=rootspring.datasource.password=sycspring.datasource.url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=falsespring.datasource.driver-class-name=com.mysql.jdbc.Driver#mybatis配置mybatis.type-aliases-package=com.yyg.boot.domain
3. 编写StudentMapper接口方法
package com.yyg.boot.mapper;import com.yyg.boot.domain.Student;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import java.util.List;public interface StudentMapper { /** * 以注解的方式实现ssm整合 */ @Select("SELECT * FROM student") List getAll(); @Select("SELECT * FROM student where id=#{id}") Student getById(@Param("id") int id);}
4. 其他service,controller及入口类都与第一部分一样.
5. 验证结果
仍然可以实现SSM整合,以注解的方式实现明显更简单一些!
那么接下来,我们继续深入研究在Spring Boot中对Spring MVC框架的支持,学习Spring Boot中的web应用开发细节。
一.Web应用开发
Spring Boot中内嵌了Tomcat,Jetty或Undertow等服务器,使得我们可以非常轻松的创建出一个HTTP服务器,而大多数的web应用都可以使用spring-boot-starter-web模块进行快速搭建和运行。
1.SpringMVC简介
SpringMVC框架是一个“模型,视图,控制器”的web框架,允许用户创建特定的@Controller或@RestController类来处理传入的HTTP请求,通过@RequestMapping注解可以将控制器中的方法映射到相应的HTTP请求。
2.Spring MVC自动配置特性
Spring Boot为Spring MVC提供的auto-configuration适用于大多数应用,并在Spring默认功能上添加了以下特性:
- 引入ContentNegotiatingViewResolver和BeanNameViewResolver beans。
- 对静态资源的支持,包括对WebJars的支持。
- 自动注册Converter,GenericConverter,Formatter beans。
- 对HttpMessageConverters的支持。
- 自动注册MessageCodeResolver。
- 对静态index.html的支持。
- 对自定义Favicon的支持。
- 自动使用ConfigurableWebBindingInitializer bean。
你可以添加自己的WebMvcConfigurerAdapter类型的@Configuration类,而不需要@EnableWebMvc注解。
如果想全面控制Spring MVC,你可以添加自己的@Configuration,并使用@EnableWebMvc注解。