springBoot常用注解
概述
记录一些springBoot在web开发中常用的一些注解和介绍
@SpringBootApplication
springBootApplication自动给程序进行必要配置,等同于 @Configuration,@EnableAutoConfiguration和@ComponentScan 三个注解配置
package com.xmlxy.firstspringbootproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.PropertySource; @SpringBootApplication @PropertySource(value = "classpath:jdbc.properties",encoding = "utf-8") public class FirstSpringbootProjectApplication { public static void main(String[] args) { SpringApplication.run(FirstSpringbootProjectApplication.class, args); } }
@ResponseBody
这个注解表示返回结果直接写入HTTP response body中,异步获取json数据,一般配合@RequestMapping一起使用
package com.xmlxy.firstspringbootproject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { @ResponseBody @RequestMapping(value = "/demo6",method = RequestMethod.GET) public String demo6() { return "demo"; } }
@Controller和@RequestMapping
定义控制类,负责将用户发来URL转发到对应的服务接口,通常配合@RequestMapping使用,@RequestMapping负责URL和Controller的具体还是映射
package com.xmlxy.firstspringbootproject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/testDemo") public class TestController { @ResponseBody @RequestMapping(value = "/demo6",method = RequestMethod.GET) public String demo6() { return "demo"; } }
@RestController
等同于@ResponseBody和@Controller的配置
package com.xmlxy.firstspringbootproject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/testDemo") public class TestController { @ResponseBody @RequestMapping(value = "/demo6",method = RequestMethod.GET) public String demo6() { return "demo"; } }
@ComponentScan
主要定义扫描路径从中找出需要标识了需要装配的类自动装配到spring的bean容器中,不管是@Controller还是@Service注解,都有一个共同的注解就是@Component,而@ComponentScan注解默认装配标识@Controller,@Service,@Component注解的类到容器中。
@Autowired
自动导入依赖的bean
package com.xmlxy.firstspringbootproject; import com.xmlxy.bean.TestData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/testDemo") public class TestController { @Autowired TestData testData; @ResponseBody @RequestMapping(value = "/demo6",method = RequestMethod.GET) public String demo6() { return "demo"; } }
@Value
注入springboot配置的属性值
package com.xmlxy.firstspringbootproject; import com.xmlxy.bean.TestData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/testDemo") public class TestController { @Value("${date.value}") String value; @Autowired TestData testData; @ResponseBody @RequestMapping(value = "/demo6",method = RequestMethod.GET) public String demo6() { return "demo"; } }
@Service
对应的是业务层Bean,用于修饰service层的组件
public interface UserService { void printf(); }
UserServiceImpl.java
package com.xmlxy.service; import org.springframework.stereotype.Service; @Service("userService") public class UserServiceImpl implements UserService { @Override public void printf() { System.out.printf("2019-7-30 14:34"); } }
@Repository
数据访问层Bean,确保dao提供异常转译
package com.xmlxy.dao; import com.xmlxy.bean.TestData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Repository; import java.util.HashMap; import java.util.Map; @Repository(value = "testDao") public class TestDaoImp implements TestDao { @Autowired private NamedParameterJdbcTemplate jdbcTemplate; @Override public int add(TestData testData) { String sql = "INSERT t_hwc_db(name,age) VALUES(:name,:age)"; Map<String,Object> param = new HashMap<>(); System.out.printf("本次插入的值===>" + testData.getName() + ",年龄为==>" + testData.getAge()); param.put("name",testData.getName()); param.put("age",testData.getAge()); return jdbcTemplate.update(sql,param); } }
@Resource(name = "testDao") private TestDao testDao;
@WebFilter
用于将一个类声明为拦截器
package com.xmlxy.firstspringbootproject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(filterName = "customFilter",urlPatterns = "/*") public class CustomFilter implements Filter { private static final Logger log = LoggerFactory.getLogger(CustomFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("fifter初始化"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("doFilter请求处理"); } @Override public void destroy() { log.info("fifter销毁"); } }
@ExceptionHandler 异常处理器,当出现其定义的异常时进行处理的方法
JPA注解
@Entity:@Table(name=”“):表明这是一个实体类。一般用于jpa这两个注解一般一块使用,但是如果表名和实体类名相同的话,@Table可以省略
@MappedSuperClass:用在确定是父类的entity上。父类的属性子类可以继承。
@NoRepositoryBean:一般用作父类的repository,有这个注解,spring不会去实例化该repository。
@Column:如果字段名与列名相同,则可以省略。
@Id:表示该属性为主键。
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = “repair_seq”):表示主键生成策略是sequence(可以为Auto、IDENTITY、native等,Auto表示可在多个数据库间切换),指定sequence的名字是repair_seq。
@SequenceGeneretor(name = “repair_seq”, sequenceName = “seq_repair”, allocationSize = 1):name为sequence的名称,以便使用,sequenceName为数据库的sequence名称,两个名称可以一致。
@Transient:表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性。如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Basic。@Basic(fetch=FetchType.LAZY):标记可以指定实体属性的加载方式
@JsonIgnore:作用是json序列化时将Java bean中的一些属性忽略掉,序列化和反序列化都受影响。
@JoinColumn(name=”loginId”):一对一:本表中指向另一个表的外键。一对多:另一个表指向本表的外键。
@OneToOne、@OneToMany、@ManyToOne:对应hibernate配置文件中的一对一,一对多,多对一。