Stories

Detail Return Return

spring常用註解 - Stories Detail

分享一些我最近都在用的關於spring和mybatis註解

Spring(Spring boot)

聲明bean註解

@Component

它是一個spring組件掃描的基礎註解,用於標識一個普通bean對象,當被spring的掃描器掃到時,會自動創建為bean對象並放入spring容器管理中其默認名稱是類名首字母小寫。
它可以被@Controller(控制層)、@Service(業務層)、@Respository(數據訪問層)所替代,這三個註解是Component註解的特化版,用途更加明確。

@Component
public class StudentController {
 // 控制層邏輯
}

@Controller

用於標註 Spring MVC 中的控制器類,處理 Web 請求並返回響應。
在Spring Boot 項目中,結合@ResponseBody可以實現RestFul風格類型的開發。如果對RestFul風格類型的開發有所疑問,請看RestFul風格介紹。
Spring官方文檔中對@Controller註解的描述是,它負責處理來自客户端的 HTTP 請求並將處理結果返回給客户端。

@Controller
public class StudentController {
    //學生管理控制層

    @GetMapping("/student")
    public String selectStudent(){
        return "學生管理控制層";
    }
}

@RestController

是一個組合註解,裏面包含着@Controller註解比@Controller註解用途更加精確、功能更好並且標明它是一個控制層註解,而且這個註解只能標註在控制層使用

@RestController
public class StudentController {
    //學生管理控制層
    
}

@Service

用於標識該類用於Service業務層,用於編寫一些邏輯代碼

public interface StudentService {
    // 學生管理邏輯層接口
}

@Service
public class StudentServiceImpl implements StudentService{
    //學生管理邏輯代碼
}

@Respository

標識該類用於數據訪問層,通常用於Dao(數據訪問類)
但是如今開發以用Mapper層替代

@Repository
public interface StudentDao {
    //學生管理數據訪問層
}

DI依賴注入

@Autowired

默認根據類型自動注入(完成自動注入的前提是該類型下只有一個)

@Service
public class StudentServiceImpl implements StudentService{
    //學生管理邏輯代碼

    @Autowired
    private StudentDao studentDao;
    //業務邏輯
}

如果同類型下有多個bean注入可以使用註解
@Autowired + @Qualifier("xxx") :指定qualifier中的bean對象注入

@Controller
public class StudentController {
    //學生管理控制層

    @Autowired
    @Qualifier("studentServiceImpl01")
    private StudentService studentService;
    @GetMapping("/student")
    public String selectStudent(){
        return "學生管理控制層";
    }
}

public interface StudentService {
    // 學生管理邏輯層接口
}


@Service
public class StudentServiceImpl01 implements StudentService{
    //學生管理邏輯代碼
}


@Service
public class StudentServiceImpl02 implements StudentService{
    //學生管理邏輯代碼
}

注意!!!
@qualifier註解單獨使用不起任何作用,必須搭配@AutoWired註解使用

@Resource(name="xxx") :@Resource註解是autowired和qualifier註解的結合體
它可以根據類型注入也可以根據名稱注入,但是
注意!!!
它不是spring註解,而是Java的註解
@Resource 按照類型注入

@Controller
public class StudentController {
    //學生管理控制層

    @Resource
    private StudentService studentService;
    @GetMapping("/student")
    public String selectStudent(){
        return "學生管理控制層";
    }
}

@Resource(name="xxx")按照名稱注入

@Controller
public class StudentController {
    //學生管理控制層

    @Resource(name = "StudentServiceImpl")
    private StudentService studentService;
    @GetMapping("/student")
    public String selectStudent(){
        return "學生管理控制層";
    }
}

@SpringBootApplication

它是一個聲明這個是啓動類和引導類註解,是spring核心註解之一

@SpringBootApplication
public class Demo01Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo01Application.class, args);
    }

}

我們編寫的代碼必須放在它的當前包或者子包下,在@SpringBootApplication啓動時spring組件會自動掃描當前包和子包下並解析@Service,@Controller層...等等註解

其他註解

@ResponseBody

將方法的返回值響應給瀏覽器/前端,如果返回的是一組集合或集合,就會先轉換成Json格式,在返回給前端

@RestController
public class StudentController {
    //學生管理控制層

    @Autowired
    private StudentService studentService;

    /**
     * 查詢學生並統一返回結果
     * @param studentList
     * @return
     */
    @GetMapping
    public Result selectStudent(@RequestBody("student") Student student){
        //查詢代碼
        return Result(student);
    }
}

請求參數

@ResponseBody("參數1")

將前端傳遞過來的請求參數,綁定到方法形參上,前端傳遞過來的參數可以跟方法的形參名不一致。但是我推薦一致

/**
     * 查詢單個學生的信息
     * @param id
     * @return
     */
    public Result selectStudentById(@RequestParam("id") Integer id){
        //查詢代碼
        return Result();
    }

@RequestBody

將前端傳遞的json格式的數據封裝到對象/集合中

@PathVariable

獲取路徑上的變量
例如 :localhost:8080/student/1 //獲取變量為1的參數

請求路徑

@RequestMapping

他的作用是映射URL路徑,將HTTP請求映射到控制層上

註解可以標註在類上和方法上
它的屬性有六種,但是目前我就學了前兩種
1.value :指定請求的實際地址 ,例如:/student,/emp
2.method:指定請求的method類型,Post,Get,Put,Delete等等。

    /**
 * 查詢單個學生的信息
 * @param id
 * @return
 */
@RequestMapping( value = "/student",method = RequestMethod.GET)
public Result selectStudentById(@RequestParam("id") Integer id){
    //查詢代碼
    return Result();
}

@GetMapping

它用於請求HTTP Get請求,此註解只能標註在方法中,響應時只會響應Get請求

    /**
     * 查詢單個學生的信息
     * @param id
     * @return
     */
    @GetMapping("/student")
    public Result selectStudentById(@RequestParam("id") Integer id){
        //查詢代碼
        return Result();
    }

@PostMapping

它用於請求HTTP Post請求,此註解只能標註在方法中,響應時只會響應Post請求

/**
     * 新增學生的信息
     * @param student
     * @return
     */
    @PostMapping("/student")
    public Result insertStudent(Student student){
        //新增代碼
        return Result();
    }

@DeleteMapping

它用於請求HTTP Delete請求,此註解只能標註在方法中,響應時只會響應Delete請求

/**
     * 刪除學生的信息
     * @param id
     * @return
     */
    @DeleteMapping("/student/{id}")
    public Result deleteStudent(@PathVariable("id") Integer id){
        //刪除代碼
        return Result();
    }

@PostMapping

它用於請求HTTP Post請求,此註解只能標註在方法中,響應時只會響應Post請求

/**
     * 修改學生的信息
     * @param student
     * @return
     */
    @PostMapping("/student")
    public Result updateStudent(Student student){
        //修改代碼
        return Result();
    }

Mybatis

MyBatis 是一個優秀的持久層框架,它簡化了 Java 應用程序與關係型數據庫之間的交互。MyBatis 通過 XML 或註解的方式將 SQL 語句與 Java 對象進行映射,避免了傳統 JDBC 編程中的大量樣板代碼。

使用

在Maven中使用,在pom.xml裏導入主配置文件

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.0</version>
</dependency>

@Mapper

聲明當前就是mybatis的一個持久層的接口,Mapper接口。

@Mapper
public interface StudentMapper {
    
    //編寫代碼
}

@Select

標註在方法上,是一個執行查詢SQL語句的註解,
使用: @Select(sql語句)

@Mapper
public interface StudentMapper {

    @Select("select * from student")
    void selectStudent();
}

@Update

標註在方法上,是一個執行修改SQL語句的註解,
使用: @Update(sql語句)

@Mapper
public interface StudentMapper {

    @Update("update student set name = #{name} where id = #{id}")
    void updateStudent();
}

@Delete

標註在方法上,是一個執行刪除SQL語句的註解,
使用:@Delete(sql語句)

@Mapper
public interface StudentMapper {

    @Delete("delete from student where id = #{id}")
    void deleteStudent();
}

@Insert

標註在方法上,是一個執行新增SQL語句的註解,
使用: @Insert(sql語句)

@Mapper
public interface StudentMapper {

    @Insert("insert into student (name,age,gender,id_card)
     values (#{name},#{age},#{gender},#{idCard})")
    void insertStudent();
}
user avatar king_wenzhinan Avatar jianxiangjie3rkv9 Avatar xuxueli Avatar jiangyi Avatar ahahan Avatar lenve Avatar shenchendexiaoyanyao Avatar swifter Avatar javadog Avatar wxweven Avatar aishang Avatar heerduo Avatar
Favorites 22 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.