MyBatis系列(二) MyBatis接口绑定与多参数传递

前言

通过上一篇博文的,已经可以做到通过MyBatis连接数据库,现在再来介绍一种方法通过接口绑定SQL语句。

 

不使用接口绑定的方式

不使用接口绑定的方式,是通过调用SqlSession中的selectxxx方法,通过传递一个String类型的参数(通常为namespace的属性+SQLID),来寻找对应SQL文件中的参数。

测试类:

    //创建SqlSessionFactory对象,并指向全局配置文件mybatis-config.xml
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }
    
    @Test
    public void test() throws IOException {
        //引用SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //从SqlSessionFactory拿到SqlSession
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<music> musiclist = session.selectList("com.mybatis.dao.selectall.selectmusicall", "许嵩");   
        } finally {
            session.close();
        }
        
    }

SQL文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.dao.selectall">
    
    <select id="selectmusicall" resultType="com.mybatis.bean.music">
        select id,name,music,musicurl from test where name = #{name};
    </select>
    
</mapper>

 

使用接口绑定的方式

一、新建一个接口,selectmusic

public interface SelectMusic{
    
    public List<music> getselectmusic(String name);
    
}

二、在SQL文件中绑定此接口

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace为接口的全类名,实现接口与SQL文件的绑定。 -->
<mapper namespace="com.mybatis.dao.SelectMusic">
    <!-- id也需要与接口的方法一致,实现接口方法与SQL的绑定。 -->
    <select id="getselectmusic" resultType="com.mybatis.bean.Policy">
        select id,name,music,musicurl from test where name = #{name};
    </select>
    
</mapper>

三、调用接口实现查询功能

    //创建SqlSessionFactory对象,并指向全局配置文件mybatis-config.xml
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }
    //通过接口绑定的方式来
    @Test
    public void test1() throws IOException{
        SqlSessionFactory sessionFactory = getSqlSessionFactory();
        SqlSession opensession = sessionFactory.openSession();
        try {
            
            SelectMusic selectmusic = opensession.getMapper(SelectMusic.class);
            list<music> musiclist = selectmusic.getselectmusic("许嵩");
            
        } finally {
            opensession.close();
        }
        
    }

 

多参数传递

如果需要传递多个参数,需要进行多参数传递。

在接口中直接进行声明

public interface SelectMusic{
    
    public List<music> getselectmusic(String name,String id);
    
}

调用此接口的方法时填写需要传递的两个参数,在SQL中可以使用0、1、2来接收传递来的参数。

select id,name,music,musicurl from test where name = #{0} and id = #{1} ;

也可直接在接口中添加注解,注解的名称需对应#{}中的名称。

public interface PolicySelectImp {
    
    public List<Policy> selectpolicyone(@Param("name")String name,@Param("id")String id);
    
}
posted @ 2019-12-11 14:22  大风北吹  阅读(539)  评论(0编辑  收藏  举报