【Spring Boot 常用注解及用法总结】

一、引入注解的目的:

  • 当bean过多时,会引起xml文件的过于臃肿,庞大,难以维护
  • 加强java bean的内聚度

二、常用注解概述:

RESTFUL特点包括:

1、每一个URI代表1种资源;
2、客户端使用GET、POST、PUT、DELETE4个表示操作方式的动词对服务端资源进行操作:
	GET用来获取资源,POST用来新建资源(也可以用于更新资源)
	PUT用来更新资源,DELETE用来删除资源;

@GetMapping("{id}") 用于查找
	@GetMapping("{id}")
	    public Banner findById(@PathVariable("id") Integer id) {}
	    
@PutMapping("deactivate/{id}")用于修改
	@PutMapping("deactivate/{id}")
	   public TrinaResponse<Void> deactivate(@PathVariable("id") Integer id) {} 
	   
@PostMapping 用于保存
	@PostMapping
	public void save(@RequestBody Banner banner) {}

@DeleteMapping 用于删除
	@DeleteMapping("{id}")
	    public void deleteById(@PathVariable("id") Integer id) {}
	
--------------------------------------------------------------------------------
@RestController
    简单理解 @RestController = @Controller+@ResponseBody
@RequestMapping("/banner") 请求路径
@RequestBody 用在参数上,接口传参得使用json
 	比如:
    	public void save(@RequestBody Banner banner) { }
@Service用于标注业务层组件、 
@Controller用于标注控制层组件
@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
	@AllArgsConstructor 作用于类,用于当前有很多个需要注入的资源,使用这个注解,
	并将service标位 final 即可
	
	比如: 下面这样就可以把@Resource拿掉了
	//@Resource
	private final IXXXX1Service XXXX1Service;
	//@Resource
    private final IXXXX2Service xXXX2Service;
	//@Resource
    private final IXXXX3Service xXXX3Service;
	//@Resource
    private final IXXXX4Service xXXX4Service;

@Value注解
	支持如下方式的注入:
		注入普通字符
		注入操作系统属性
		注入表达式结果
		注入其它bean属性
		注入文件资源
		注入网站资源
		注入配置文件
	@Value三种情况的用法:
		${} @Value("${test.testValue}") 是去找外部配置的参数,将值赋过来
		#{}SpEL表达式,去寻找对应变量的内容
		#{} 直接写字符串就是将字符串的值注入进去

	
@Repository用于标注数据访问组件,即DAO组件。
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用
	如下:
		@Autowired @Qualifier("personDaoBean") 存在多个实例配合使用

Spring支持AspectJ的注解式切面编程。

@Aspect 声明一个切面

@After 在方法执行之后执行(方法上)

@Before 在方法执行之前执行(方法上)

@Around 在方法执行之前与之后执行(方法上)

@PointCut 声明切点
--------------------------------------------------------------------------------
   
lombok

1@Data 提供类的get、set、equals、hashCode、canEqual、toString
2@Builder 可以通过构造器模式来赋值,而不需要setter
	比如:
		UserInfo userInfo = UserInfo.builder()
        .name("zzl")
        .email("bgood@sina.com")
        .build();
        
3@NoArgsConstructor 无参构造函数
4@AllArgsConstructor 所有参数的构造函数
5@Log4j/@Slf4j : 注在类上,提供对应的 Logger 对象,变量名为 log

	
--------------------------------------------------------------------------------
	
启动类
@SpringBootApplication
@MapperScan("com.sjt.test.mapper") 扫描对应包下面的mapper,整合mybaits

外+spring cloud 其他模块的启动注解@EnableXXXXX
	@EnableAspectAutoProxy:开启对AspectJ自动代理的支持;
	@EnableAsync:开启异步方法的支持;
	@EnableScheduling:开启计划任务的支持;
	@EnableWebMvc:开启web MVC的配置支持;
	@EnableConfigurationProperties:开启对@ConfigurationProperties注解配置Bean的支持;
	@EnableJpaRepositories:开启对SpringData JPA Repository的支持;
	@EnableTransactionManagement:开启注解式事务的支持;
	@EnableCaching:开启注解式的缓存支持;


--------------------------------------------------------------------------------

配置类
@ConfigurationSpring3.0@Configuration用于定义配置类,可替换xml配置文件,
	被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被
	AnnotationConfigApplicationContextAnnotationConfigWebApplicationContext类
	进行扫描,并用于构建bean定义,初始化Spring容器
@Bean
	注解在方法上,声明当前方法的返回值为一个bean,替代xml中的方式;

@RestControllerAdvice 同一异常处理 https://www.jianshu.com/p/47aeeba6414c	
	全局异常处理
	全局数据绑定
	全局数据预处理
	
@ExceptionHandler	用于全局处理控制器里的异常。

@Transactional 
	注解放在类级别时,表示所有该类的公共方法都配置相同的事务属性信息。
	当前类的所有方法都支持事务并且是只读。当类级别配置了@Transactional,
	方法级别也配置了@Transactional,应用程序会以方法级别的事务属性信息来管理事务,
	换言之,方法级别的事务属性信息会覆盖类级别的相关配置信息。
	
	@Transactional(propagation= Propagation.SUPPORTS,readOnly=true)

--------------------------------------------------------------------------------

mybatis
@Param("modularCode") 实体类的别名,传入,在判断和赋值的时候要记得:别名.属性
    void methodName(@Param("modularCode") String modularCode);


--------------------------------------------------------------------------------

动态sql

resultMap 结果集
<resultMap type="com.trinasolar.csde.acm.api.entity.Application" id="applicationResultMap">
    <result property="id" column="id" jdbcType="INTEGER"/>
    <result property="appName" column="app_name" jdbcType="VARCHAR"/>
    <result property="appCode" column="app_code" jdbcType="VARCHAR"/>
    <result property="appType" column="app_type" jdbcType="VARCHAR"/>
    <result property="appIcon" column="app_icon" jdbcType="VARCHAR"/>
    <result property="app" column="app" jdbcType="VARCHAR"/>
    <result property="modularName" column="modular_name" jdbcType="VARCHAR"/>
    <result property="modularCode" column="modular_code" jdbcType="VARCHAR"/>
    <result property="enableShowIndex" column="enable_show_index" jdbcType="BOOLEAN"/>
    <result property="jumpUrl" column="jump_url" jdbcType="VARCHAR"/>
    <result property="status" column="status" jdbcType="BOOLEAN"/>
    <result property="appOrder" column="app_order" jdbcType="INTEGER"/>
    <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
    <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
</resultMap>

基本的查询列

<sql id="applicationBaseColumn">
    t.`id`, 
     t.`app_name`, 
     t.`app_code`, 
     t.`app_type`, 
     t.`app_icon`, 
     t.`app`, 
     t.`modular_name`, 
     t.`modular_code`, 
     t.`enable_show_index`, 
     t.`jump_url`, 
     t.`status`, 
     t.`app_order`, 
     t.`create_time`, 
     t.`update_time`, 
     t.`create_by`, 
     t.`update_by`
 </sql>


使用:
<select id="selectById" resultMap="applicationResultMap">
	SELECT
    <include refid="applicationBaseColumn"/>
    FROM `application` t
    WHERE t.`id` = #{id}
</select>


SET 更新
	status是Boolean或者数据库字段类型为tinyint(1)的时候
 	<if test="status != null" /> 只能判断为null,不能判断!=''
	并且set里面的判断没有AND 不和where一样

<set>     
   <if test="modularName != null and modularName != ''">
        `modular_name` = #{modularName},
    </if>
    <if test="status != null">
        `status` = #{status},
    </if>
    `update_time` = now(),
    <if test="createBy != null and createBy != ''">
        `create_by` = #{createBy},
    </if>
</set>

 ORDER BY  二次排序 + 双 CONCAT 实现 模糊查询
 
<sql id="applicationQueryConditionForOther">
  <if test="q.appName != null and q.appName != ''">
       AND t.`app_name` like CONCAT(CONCAT('%', #{q.appName}), '%')
   </if>
   <if test="q.appType != null and q.appType != ''">
       AND t.`app_type` = #{q.appType}
   </if>
   <if test="q.enableShowIndex != null">
       AND t.`enable_show_index` = #{q.enableShowIndex}
   </if>
   <if test="q.app != null and q.app != ''">
       AND t.`app` = #{q.app}
   </if>
</sql>
    
<select id="select" resultMap="applicationResultMap">
  SELECT
     <include refid="applicationBaseColumn"/>
     FROM `application` t
     <where>
         <include refid="applicationQueryConditionForOther"/>
     </where>
     ORDER BY  t.`app_order` ASC,t.`create_time` DESC
 </select>

--------------------------------------------------------------------------------


- @PreDestroy 摧毁注解 默认 单例  启动就加载
- @Async异步方法调用
- @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了 @Bean,就会作为这个Spring容器中的Bean- @Scope用于指定scope作用域的(用在类上)
- @PostConstruct用于指定初始化方法(用在方法上)
- @PreDestory用于指定销毁方法(用在方法上)
- @DependsOn:定义Bean初始化及销毁时的顺序
- @Primary:自动装配时当出现多个Bean候选者时,被注解为@PrimaryBean将作为首选者,否则将抛出异常
- @Lazy(true) 表示延迟初始化

前面的更为常见!

#三、详述
在使用注解前:要将使用注解的类都加载进xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd" > 	
<!-- 这里使用base-package来引入这个包下的所有类-->
<context:component-scan base-package="com.annotation" />
</beans>
1、@Autowired

@Autowired顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的setter与bean属性中的property。

@Autowired默认按类型匹配的方式,在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中。

package com.annotation.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.annotation.dao.AnnotationDao;
import com.annotation.service.AnnotationService;
@Service
@Scope("prototype")//这里的Scope和xml文件的用法一致
public class AnnotationServiceImpl2 implements AnnotationService {
	@Autowired
	private AnnotationDao ad;
	@Override
	public void p() {
		System.out.println("Service注解测试2:"+this.hashCode());
		ad.p();//加注解@Autowired就可以将dao注入到service中,同样在setter方法上,构造方法上,都可以注入成功,而不需要在xml中配置
	}
}

如上,加上注解,这样在xml中就不用配置<bean></bean>了,同样在类上加这个注解也可以。

假如:在xml中配置了bean而且有property(即没有注释的那种配置),但是,却没有setter方法,而给这个属性加了@Autowired,这样会成功吗?
答 案 是 否 定 的 , 因 为 : S p r i n g 会 按 照 x m l 优 先 的 原 则 去 相 应 的 j a v a 文 件 中 , \color{red}{答案是否定的,因为:Spring会按照xml优先的原则去相应的java文件中,} Springxmljava
寻 找 这 两 个 属 性 的 s e t t e r 方 法 来 配 置 \color{red}{寻找这两个属性的setter方法来配置} setter

2、@Scope

如上:scope可以指定模式,默认还是singleton,即单例模式。"prototype"表示原型即每次都会new一个新的出来。

3、@Service

@Service和@Controller、@Repository、@Component其实都是一样的,当你知道这个类具体是干什么的就用具体的注解,如果不知道就用@Component。(概述中已经叙述清楚)

另外:这里的@Service,干了两件事,1:声明该类是一个bean,这样才可以使用@Autowired来自动注入。2:这个类的bean的id是类名,且首字母小写,你也可以这样做,@Service("这里你是想要的id名")

4、@Autowired @Qualifier(“指定bean”)配套使用

这种情况是:当出现一个接口有多个实现类时来区分使用,例如下面的
AnnotationService,这个接口就有两个实现类。

package com.annotation;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.annotation.service.AnnotationService;

@Component
public class QualifierResoutce {
	@Autowired
	@Qualifier("annotationServiceImpl2")
	//	@Resource(name="annotationServiceImpl2")
	public AnnotationService as;
	
	@Resource
	public List<AnnotationService> list;
	
	@Resource
	public Map<String,AnnotationService> map;
	
	public void p(){
		System.out.println("QualifierResoutce");
		as.p();
	}
	
	public void print(){
		System.out.println("-----list存放------");
		for(AnnotationService a:list){
			System.out.println(a.getClass().getName());
		}
		System.out.println("-----map存放------");
		for(Entry<String, AnnotationService> e:map.entrySet()){
			System.out.println(e.getKey()+"   "+e.getValue());
		}
	}
}

上面的@Component,就是不知道这个类是干嘛的,所以就使用这个注解,

而这两个注解的配套使用就是,自动注入这个属性,并且走的是这个属性(接口)的哪个实现类

5、@Resource

上面也出现了这个注解,这个注解和@Autowired非常类似。

@Resource的装配顺序:
(1)、@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
(2)、指定了name或者type则根据指定的类型去匹配bean
(3)、指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错

@Resource和@Autowired的区别:
(1)、@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配
(2)、@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了

Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。

6、@Controller

@Controller对应表现层的Bean,也就是Action,当使用这个注解以后,spring容器中就会有一个和这个注解的类名相同(首字母小写)的action,

也可以这样@Controller(“ActionName”)来指定名字

同样@Repository,也是这样的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值