版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiangshangchunjiezi/article/details/90643155
接口绑定,就是在MyBatis中任意定义接口,然后把接口里面的方法和SQL语句绑定, 我们直接调用接口方法就可以,这样比起原来了SqlSession提供的方法我们可以有更加灵活的选择和设置。
student user = (student) session.selectOne("test.studentMapper.selectUserByID", 1);
接口绑定有两种实现方式:
- 一种是通过注解绑定,就是在接口的方法上面加上 @Select、@Update等注解,里面包含Sql语句来绑定;
//这种方式不用写mapper.xml
@Select("select * from `tb_Teacher` where id = #{id}")
public teacher selectTeacherByID(int id);
- 另外一种就是通过xml里面写SQL来绑定, 在这种情况下,要指定xml映射文件里面的namespace必须为接口的全路径名。
<select id="selectUserByID" parameterType="int" resultType="test.student"> select id,name,age,stuCountry stu_country from `tb_Student` where id = #{id} </select>
两种方式选择:
-
当Sql语句比较简单时候,用注解绑定, 当SQL语句比较复杂时候,用xml绑定,一般用xml绑定的比较多。