mybatis之接口开发

一:规则:

        1)接口名称必须和mapper文件名称相同

        2)接口包路径和命名空间一致(接口文件和mapper文件在一起)

                  3)接口中要声明的方法名和某个sql配置中的id属性一致

        4)参数和返回值,要按java规则来写

例:

   


以id属性id为例


PersonMapper接口类中:

                 

/**
 * Created by **** on 2017/3/24.
 */
public interface PersonMapper {
    public List<Person> find(Map map);
}

测试类:TestPersonInterface

     

/**
 * Created by 王新起 on 2017/3/24.
 */
public class TestPersonInterface {
    private SqlSessionFactory factory;
    @Before //最先运行  获得faction工厂
    public void init() throws IOException {

        String resource="sqlMapConfig.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        factory= new SqlSessionFactoryBuilder().build(is);
    }

    @Test //接口开发
    public void testFind(){
        SqlSession session=factory.openSession();

        //获取接口对象
        PersonMapper pm=session.getMapper(PersonMapper.class);
        Map map =new HashMap();
        map.put("name","aa");
        List<Person> list=pm.find(map);
        System.out.println(list.size());
        for (Person p :list){
            System.out.println(p);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_35222261/article/details/65657241