SpringBoot系列教程20--Web开发06之注解方式实现SSM整合
作者:一一哥
一.前情回顾通过上一节,我们知道在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=root
spring.datasource.password=syc
spring.datasource.url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.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整合,以注解的方式实现明显更简单一些!