---
# System prepended metadata

title: 使用註解開發
tags: [Mybatis-註解]

---

# 使用註解開發
###### tags: `Mybatis-註解`

## 使用註解開發
1.註腳在接口上實現
```java=
public interface UserMapper {
    @Select("select * from user")
    List<User> getUsers();
}
```

2.需要在核心配置文件綁定接口
```xml=
<!--綁定接口-->
    <mappers>
        <mapper class="com.kuang.dao.UserMapper"/>
    </mappers>
```

3.測試
```java=
public class UserMapperTest {
    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //底層主要應用反射
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getUsers();
        for(User user:users){
            System.out.println(user);
        }

        sqlSession.close();
    }
}
```

**本質：反射機制的實現**
底層：動態代理

