MyBatis typeAliases(别名)

    UserMapper.xml

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="us">
    5. <!--
    6. 按主键id查询学生信息
    7. Student getAll(Integer id);
    8. 有没有入参(传入的参数)?有parameterType,里面是有别名机制的
    9. 有没有返回值?有就resultType
    10. -->
    11. <select id="getById" parameterType="int" resultType="org.pojo.Student">
    12. select * from student where id=#{id}
    13. </select>
    14. </mapper>

    修改主配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd" >
    <configuration>
        ....
            <mapper resource="UserMapper.xml"></mapper>
        </mappers>
    </configuration>
    

    测试功能

    public class AppTest{
        @Test
        public void testGetById() throws IOException {
            //读取配置文件信息
            InputStream in = Resources.getResourceAsStream("mybatis.xml");
            //创建SqlSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            //取出SqlSession
            SqlSession sqlSession = factory.openSession();
            //按主键查学生
            List<Student> list = sqlSession.selectList("us.getById",1001);//注意这条
            list.forEach(student -> System.out.println(student));
            //关闭SqlSession
            sqlSession.close();
        }
    }