MyBatis系列(二) MyBatis接口绑定与多参数传递
2024-08-30 07:15:23
前言
通过上一篇博文的,已经可以做到通过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); }
最新文章
- k8s DNS 服务发现的一个坑
- 110.Balanced Binary Tree Leetcode解题笔记
- Android:padding和android:layout_margin的区别
- 硅谷新闻3--使用Android系统自带的API解析json数据
- 在每次request请求时变化session
- Knockout绑定audio的pause事件导致音频无法停止
- return 和 exit
- 从m个数中取top n
- Html5的<;button>;标签
- [ArcGIS所需的补丁]ArcGIS 10.2.2 for Desktop联系Oracle(2014年10上个月发布)数据库崩溃
- IE常见的CSS的BUG(二)
- Centos安装vncserver服务
- post请求测试
- 第六届蓝桥杯B组java最后一题
- 百度音乐flac 下载
- tablib cell() missing 1 required positional argument: &#39;column&#39; 报错
- Java Socket 通信实例 - 转载
- mysql 5.7 配置
- 20165326 学习基础和c语言基础调查
- 190. Reverse Bits (Int; Bit)
热门文章
- Java进程间通信学习
- 记一次删除ocr与dbfile的恢复记录
- PO设计模式
- Vuex中mapState的用法
- c#引用c++dll和c++导出类出现的各种问题
- error: Libtool library used but &#39;LIBTOOL&#39; is undefined
- 201871010135 张玉晶 《2019面向对象程序设计(java)课程学习进度条》
- JDK1.8 LocalDate 使用方式;LocalDate 封装Util,LocalDate工具类(一)
- destoon聚合搜索页面模板
- IComparable和IComparer接口