首页 > 编程知识 正文

Springboot常用注解大全,springboot常用注解及实现

时间:2023-05-06 00:16:48 阅读:198016 作者:4760

SpringBoot:

controller层:1、@Controller@Controller 用来响应页面,表示当前的类为控制器。2、@RestController@RestController 是@ResponseBody和@Controller的结合表明当前类是控制器且返回的是一组数据,不是页面3、@Autowired这个注解的作用是将其他的类,接口引入,类似于之前的类的初始化等,用这个注解,类中或接口的方法就可以直接调用了。4、@RequestMapping当前台界面调用Controller处理数据时候告诉控制器怎么操作作用:URL映射。5、@GetMapping@RequestMapping(method = RequestMethod.GET)的简写作用:对应查询,表明是一个查询URL映射6、@PostMapping@RequestMapping(method = RequestMethod.POST)的简写作用:对应增加,表明是一个增加URL映射7、@PutMapping@RequestMapping(method = RequestMethod.PUT)的简写作用:对应更新,表明是一个更新URL映射8、@DeleteMapping@RequestMapping(method = RequestMethod.DELETE)的简写service层:1.@service用于标注业务层组件dao层:1.@Repository是用来注解接口,@Repository注解可以标记在任何的类上,用来表明该类是用来执行与数据库相关的操作(即dao对象),并支持自动处理数据库操作产生的异常配置:1.@Configuration,@Bean可理解为用spring的时候xml里面的<beans>标签,用@Configuration注解该类,等价 与XML中配置beans@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名例如:@Configuration public class ExampleConfiguration { @Value("com.mysql.jdbc.Driver") private String driverClassName; @Value("jdbc://xxxx.xx.xxx/xx") private String driverUrl; @Value("${root}") private String driverUsername; @Value("123456") private String driverPassword; @Bean(name = "dataSource") public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(driverUrl); dataSource.setUsername(driverUsername); dataSource.setPassword(driverPassword); return dataSource; } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } } 这样,在项目中@Autowiredprivate DataSource dataSource;的时候,这个dataSource就是我们在ExampleConfiguration中配的DataSource。

启动类:

@SpringBootApplication:启动注解,@SpringBootApplication是一个复合注解,包括@ComponentScan,和@SpringBootConfiguration,@EnableAutoConfiguration@SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。@EnableAutoConfiguration的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web ,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。在下面博客会具体分析这个注解,快速入门的demo实际没有用到该注解。@ComponentScan,扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理。是以前的<context:component-scan>(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。@ServletComponentScan:在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。@MapperScan("com.Vm.server") :@MapperScan注解只会扫描包中的接口,不会扫描类,扫描指定包中的接口@EnableScheduling: spring自带的定时服务public class ScheduledTasks { @Scheduled(fixedRate = 1000 * 30) //每30秒执行一次 public void reportCurrentTime(){ System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date ())); }}

Mybatis:

//Mapper中的namespace用于绑定Dao接口的,即面向接口编程。//namespace:一般是dao接口所在的路径+接口名称<mapper namespace="com.VmService.dao.IBlackDao">//resultMap属性:type为java实体类;id为此resultMap的标识。//id和result标签是最简单的映射,id为主键映射;result其他基本数据库表字段到实体类属性的映射。<resultMap id="BlackBean" type="com.VmService.model.BlackBean"> <id column="ID" property="id"/> <id column="IM" property="im"/></resultMap>//进行SQL语句查找<select id="selectAllBlackList" resultMap="BlackBean"> SELECTid as id,im as im,FROMt_blacklist t1 ORDER BYid DESC</select>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。