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

}

}

}

以上是 Mybatis mapper接口动态代理开发步骤解析 的全部内容, 来源链接: utcz.com/z/323168.html

回到顶部