學習springBoot(2)經常使用註解

繼續學習SpringBoot,先說說經常使用的註解吧,從別處mark的。。。html

@SpringBootApplication: 包含@Configuration、@EnableAutoConfiguration、@ComponentScan 一般用在主類上。java

@Repository: 用於標註數據訪問組件,即DAO組件。mysql

@Service: 用於標註業務層組件。spring

@RestController: 用於標註控制層組件(如struts中的action),包含@Controller和@ResponseBody。sql

@ResponseBody: 表示該方法的返回結果直接寫入HTTP response body中 通常在異步獲取數據時使用,在使用@RequestMapping後,返回值一般解析爲跳轉路徑,加上@responsebody後返回結果不會被解析爲跳轉路徑,而是直接寫入HTTP response body中。好比異步獲取json數據,加上@responsebody後,會直接返回json數據。數據庫

@Component: 泛指組件,當組件很差歸類的時候,咱們可使用這個註解進行標註。json

@ComponentScan: 組件掃描。我的理解至關於context:component-scan,若是掃描到有@Component @Controller @Service等這些註解的類,則把這些類註冊爲bean。springboot

@Configuration: 指出該類是 Bean 配置的信息源,至關於XML中的,通常加在主類上。bash

@Bean: 至關於XML中的,放在方法的上面,而不是類,意思是產生一個bean,並交給spring管理。app

@EnableAutoConfiguration: 讓 Spring Boot 根據應用所聲明的依賴來對 Spring 框架進行自動配置,通常加在主類上。

@AutoWired: byType方式。把配置好的Bean拿來用,完成屬性、方法的組裝,它能夠對類成員變量、方法及構造函數進行標註,完成自動裝配的工做。 當加上(required=false)時,就算找不到bean也不報錯。

@Qualifier: 當有多個同一類型的Bean時,能夠用@Qualifier("name")來指定。與@Autowired配合使用

@Resource(name="name",type="type"): 沒有括號內內容的話,默認byName。與@Autowired幹相似的事。

@RequestMapping:

RequestMapping是一個用來處理請求地址映射的註解,可用於類或方法上。用於類上,表示類中的全部響應請求的方法都是以該地址做爲父路徑。 該註解有六個屬性: params:指定request中必須包含某些參數值是,才讓該方法處理。 headers:指定request中必須包含某些指定的header值,才能讓該方法處理請求。 value:指定請求的實際地址,指定的地址能夠是URI Template 模式 method:指定請求的method類型, GET、POST、PUT、DELETE等 consumes:指定處理請求的提交內容類型(Content-Type),如application/json,text/html; produces:指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回

@RequestParam: 用在方法的參數前面。 @RequestParam String a =request.getParameter("a")。

@PathVariable:

路徑變量。參數與大括號裏的名字同樣要相同。

RequestMapping("user/get/mac/{macAddress}") public String getByMacAddress(@PathVariable String macAddress){   //do something; }

@Profiles Spring Profiles提供了一種隔離應用程序配置的方式,並讓這些配置只能在特定的環境下生效。 任何@Component或@Configuration都能被@Profile標記,從而限制加載它的時機。

@Configuration @Profile("prod") public class ProductionConfiguration { // ... }

@ConfigurationProperties Spring Boot將嘗試校驗外部的配置,默認使用JSR-303(若是在classpath路徑中)。 你能夠輕鬆的爲你的@ConfigurationProperties類添加JSR-303 javax.validation約束註解:

複製代碼 @Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings { @NotNull private InetAddress remoteAddress; // ... getters and setters } 複製代碼

全局異常處理

@ControllerAdvice: 包含@Component。能夠被掃描到。 統一處理異常。

@ExceptionHandler(Exception.class): 用在方法上面表示遇到這個異常就執行如下方法。

説了這麼多,接下來鏈接下數據庫作個簡單的查詢,使用spring-data-jpa,先建一個實體類,運行項目,自動在數據庫生成咱們的數據表

配置文件:

#數據庫配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
複製代碼

實體類:

@Entity
@Table(name = "book")
public class Book {
    @Id
    @GeneratedValue
    private Integer Id;

    @Column(length=100)
    private String bookName;

    public Integer getId() {
        return Id;
    }

    public void setId(Integer id) {
        Id = id;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
}

複製代碼

dao層:

@Component
public interface  BookDao extends JpaRepository<Book, Integer> {

}

複製代碼

Controller:

@Controller
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookDao bookDao;

    /**
     * 查詢全部圖書
     * @return
     */
    @RequestMapping(value="/list")
    @ResponseBody
    public List<Book> list(){
        List<Book> all = bookDao.findAll();
        return all;
    }
}

複製代碼

查詢結果:

[{"bookName":"語文","id":1},{"bookName":"數學","id":2}]
複製代碼
相關文章
相關標籤/搜索