MyBatis基本使用2-Mapper代理开发

1.新建mapper接口,com.xx.mapper ,新建接口TestMapper

2.resources中 新建com/xx/mapper  文件夹 ,可以与1中的包名对应 。 新建 TestMapper.xml

3.TestMapper接口中实现相同的方法

List<Test> GetAll();

4. TestMapper.xml

<mapper namespace="com.ld.mapper.TestMapper" >
    <select id="GetAll" resultType="com.ld.model.Test" >
        select * from test;
    </select>
</mapper>

5.使用

 //List<Test> tests= sqlSession.selectList("test.GetAll");


TestMapper testMapper=sqlSession.getMapper(TestMapper.class);

List<Test> tests=testMapper.GetAll();

 6.mybatis-config.xml修改

    <mappers>
        <!--加载sql的映射文件-->
        <package name="com.ld.mapper" />
    </mappers>

 7.别名简化 包名

<typeAliases>
        <package name="com.ld.model"/>
</typeAliases>


<mapper namespace="com.ld.mapper.TestMapper" >
    <select id="GetAll" resultType="Test" >
        select * from test;
    </select>
</mapper>

 

posted @ 2022-05-25 17:24  Xyang  阅读(26)  评论(0编辑  收藏  举报
hi