SpringBoot常用注解

1、 Bean的相关注解

@Component:标注一个普通的Spring Bean类

@Controller:标注一个控制类组件

@Sevice:标注一个控制器类组件

@Repository:标注一个仓库类组件

2、SQL注解开发实现动态SQL

2.1直接使用@SelectProvider

@Maper
public ineterface StudentMaper {
	// 显示所有学生信息
	//动态查询 type指定一个类 method:使用这个类中的方法返回的SQL字符作为查询的语句
	@SelectProvider(type = com.scnu.maper.StudentSelectSql.class,method = “findAllStudent”)
}
```java
package com.scnu.mapper;
import...
public class StudentSelectSql {
	// 根据注解传递过来的参数来构建SQL语句
	public String findAllStudent(Student student){
		return new SQL(){{
			//此处关键字区分大小写
			SELECT("*");
			FROM("student");
			if(student.getSno()!=null){
				WHERE("sno=#{sno}");
			}
			if(student.getMajorClass()!=null && (student.getMajorClass()!=""){
				WHERE("majorclass=#{majorclass}");
			}
		}}.toString();
	}
]

2.2利用标签script

@Select("<script> " +
            "SELECT id, name, email,password " +
            "FROM user " +
            " <where> " +
            " <if test=\"email != null\">id=#{email}</if> " +
            " <if test=\"name != null\"> AND name=#{name}</if> " +
            " </where> " +
            " </script> ")

对于Like语句,需要借助concat函数实现

@Select("SELECT name from user WHERE email LIKE concat(#{prefix},'%') limit 5")

3、main方法的@SpringBootApplication

/**此处的@SpringBootApplication注解相当于@Configuration + @EnableAutoConfiguration + @ComponentScan
*/
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

1、@Configuration

    @Configuration的注解类标示这个类可以使用Spring IOC容器作为Bean的来源

    @Bean

    注解会告诉Spring,一个带有@Bean的注解方法会返回一个对象,该对象会被注册为在Spring应用程序上下文中的Bean

    经常与@Bean配合使用,使用这两个注解可以创建一个简单的Spring配置类,可以用来取代XML文件。

2、@EnableAutoConfiguration

    能够自动配置Spring的上下文,试图猜测和配置用户想要的Bean类,通常会自动根据用户的类路径和Bean定义自动配置

3、@ComponentScan

    会自动扫描包下全部带有@Component(当然包括@Service、@Repository、@Controller等子注解) 的类,并注册为Bean,这些Bean一般通过@Autiowired注解进行注入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值