前言

通过上一篇博文的,已经可以做到通过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);

}

最新文章

  1. k8s DNS 服务发现的一个坑
  2. 110.Balanced Binary Tree Leetcode解题笔记
  3. Android:padding和android:layout_margin的区别
  4. 硅谷新闻3--使用Android系统自带的API解析json数据
  5. 在每次request请求时变化session
  6. Knockout绑定audio的pause事件导致音频无法停止
  7. return 和 exit
  8. 从m个数中取top n
  9. Html5的&lt;button&gt;标签
  10. [ArcGIS所需的补丁]ArcGIS 10.2.2 for Desktop联系Oracle(2014年10上个月发布)数据库崩溃
  11. IE常见的CSS的BUG(二)
  12. Centos安装vncserver服务
  13. post请求测试
  14. 第六届蓝桥杯B组java最后一题
  15. 百度音乐flac 下载
  16. tablib cell() missing 1 required positional argument: &#39;column&#39; 报错
  17. Java Socket 通信实例 - 转载
  18. mysql 5.7 配置
  19. 20165326 学习基础和c语言基础调查
  20. 190. Reverse Bits (Int; Bit)

热门文章

  1. Java进程间通信学习
  2. 记一次删除ocr与dbfile的恢复记录
  3. PO设计模式
  4. Vuex中mapState的用法
  5. c#引用c++dll和c++导出类出现的各种问题
  6. error: Libtool library used but &#39;LIBTOOL&#39; is undefined
  7. 201871010135 张玉晶 《2019面向对象程序设计(java)课程学习进度条》
  8. JDK1.8 LocalDate 使用方式;LocalDate 封装Util,LocalDate工具类(一)
  9. destoon聚合搜索页面模板
  10. IComparable和IComparer接口