SSM和SpringBoot框架的常用注解总结

SSM框架中用到了许多注解,今天就来总结一下SSM框架中的注解

Spring

声明标注注解:

@Service:用于标注业务层的注解

@Controller:用于标注控制层的注解

@Repository:用于标注数据访问组件,即DAO组件

@Component:泛指组件,当组件不好归类的时候可以使用Component

被上面四个注解标注的类,都会进入Spring容器中进行管理。

bean注入的注解:

@Autowired:最常用的bean自动注入标签,按bytype自动注入,默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用

@Autowired()@Qualifier("StudentDao") private StudentDao studentdao;

@Resource:作用相当于@Autowired,按byname自动注入

@Inject:是根据类型进行自动装配的,如果需要按名称进行装配,则需要配合@Named;

@Value:为属性注入值

配置类相关注解:

@Configuration:声明当前类为配置类

@Bean:注解在方法上,声明当前方法返回值为Bean

@ComponentScan:用于对Component进行扫描

切面相关注解:

@Aspect:声明这是一个切面

@Before:前置通知

@After:后置通知

@AfterReturning:返回通知

@AfterThrowing:异常通知

@Around:环绕通知

@pointcut:定义切点

SpringMVC

@Controller:用于声明这是一个控制层,SpringMVC中的Controller负责处理由DispatcherServlet 分发的请求

@RequestMapping:用来处理请求地址映射,可用在类或方法上

@ModelAttribute:在controller方法调用前,先执行@ModelAttribute方法,可用于注解和方法参数中

@PathVariable:用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数。

@requestParam:用于在SpringMVC后台控制层获取参数,类似于request.getParameter(“name”)

@ResponseBody:用于将Controller的方法返回的对象,转换为指定格式,如json

@RequestHeader:可以把Request请求的header部分的值绑定到方法的参数上。

@CookiesValue:把Request header中关于cookie的值绑定到方法的参数上。

Mybatis

@Insert:插入sql

@Select:查询sql

@Update:更新sql

@Delete:删除sql

@Param:用于传递参数

@Results:对应于XML中的<resultMap>

@Result:对应于XML中的<result>

SpringBoot

SpringBoot中与SSM框架中相同的注解就不再写了

@RestController:这个注解的意思相当于同时实现了@Controller和@ResponseBody

@SpringBootApplication:该注解集合了@Configuration,

@EnableAutoConfiguration,@ComponentScan三个注解

以上只是对常用注解的一个简单总结,具体的使用方法我会另写一个博客来总结,欢迎补充!

SSM(Spring + SpringMVC + MyBatis)和Spring Boot都是Java Web应用程序开发的框架,它们可以被整合在一起使用。下面是将SSM整合Spring Boot的基本步骤: 1. 创建一个Spring Boot项目,可以使用Spring Initializr或者手动创建。 2. 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` 3. 创建Spring配置文件applicationContext.xml,配置MyBatis和数据源: ```xml <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 4. 创建Spring MVC配置文件applicationContext-mvc.xml,配置视图解析器和控制器: ```xml <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <context:component-scan base-package="com.example.controller"/> <mvc:annotation-driven/> ``` 5. 创建MyBatis映射文件和Mapper接口,以及相应的Service和Controller类。 6. 在Spring Boot的启动类中添加@MapperScan注解,指定Mapper接口的包路径: ```java @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 7. 运行Spring Boot应用程序,访问相应的URL,测试整合是否成功。 以上就是将SSM整合Spring Boot的基本步骤,具体实现过程可能会因项目需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java鱼仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值