使用註解開發

qq_48094059發表於2020-12-10

1.註解在介面上實現

public interface UserMapper {

    @Select("select * from user")
    List<User> getUsers();
}

2.需要在核心配置檔案中繫結介面

 <!--繫結介面-->
    <mappers>
        <mapper class="tian.dao.UserMapper"></mapper>
    </mappers>

3.測試

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();
    }

本質:反射機制實現
底層:動態代理!

相關文章