1. package com.example.aninbatisdemo;
    2. import java.io.InputStream;
    3. import com.example.aninbatis.sqlsession.SqlSessionFactory;
    4. import com.example.aninbatis.sqlsession.SqlSessionFactoryBuilder;
    5. import com.example.aninbatis.utils.Resources;
    6. import com.example.aninbatisdemo.mapper.UserMapper;
    7. import com.example.aninbatisdemo.mapper.UserMapperImpl;
    8. import com.example.aninbatisdemo.pojo.User;
    9. import org.junit.jupiter.api.Test;
    10. public class UserDaoTest {
    11. /**
    12. * 测试使用手写mybatis框架去实现的
    13. */
    14. @Test
    15. public void testQueryUserById() {
    16. // 全局配置文件
    17. String resource = "SqlMapConfig.xml";
    18. InputStream inputStream = Resources.getResourceAsStream(resource);
    19. // SqlSessionFactory的创建可能有几种创建方式,但是我还是不想要知道SqlSessionFactory的构造细节
    20. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    21. UserMapper userMapper = new UserMapperImpl(sqlSessionFactory);
    22. User param = new User();
    23. param.setId(1);
    24. User user = userMapper.queryUserById(param);
    25. System.out.println(user);
    26. }
    27. }

    输出

    1. >>>>>>>>>>>>>> 执行sql:select * from `user` where id = ?
    2. >>>>>>>>>>>>>> 参数:User(id=1, name=null)
    3. User(id=1, name=张三)