mybatis的mapper接口动态代理开发

一、必须遵守的四项原则
1:接口 方法名==xx.xml中的id名
2:方法返回值类型与Mapper.xml文件中返回值类型一致
3:方法的入参类型与Mapper.xml文件中入参值类型一致
4:命名空间绑定接口

二、

public class UserMapperTest {

private SqlSession sqlSession;

private InputStream in;

@Before
public void before() throws IOException {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建 SqlSessionFactory 的构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.使用构建者创建工厂对象 SqlSessionFactory
    SqlSessionFactory sqlSessionFactory = builder.build(in);
     sqlSession = sqlSessionFactory.openSession();

}

@After
public void after() throws IOException {
    //7.释放资源
    sqlSession.close();
    in.close();
}

@Test
public void findUserById() {
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<User> users = userMapper.findAll();
    for (User user : users) {
        System.out.println(user);
    }
}

}

posted @ 2020-07-15 00:04  jock_javaEE  阅读(159)  评论(0编辑  收藏  举报