Mybatis学习(二)--getMapper接口绑定方案和多参数传值

本文介绍了Mybatis接口绑定方案,该方案允许直接传入多个参数执行sql,无需设置parameterType。通过配置SqlMapConfig.xml,Mapper.xml(确保namespace与接口匹配,id对应接口方法),并设置接口,可以实现多参数传递。示例展示了如何执行包含多参数的sql语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Mybatis的基础使用中,如果想向一个sql语句中传递多个参数,只能将parameterType设置为某个类或者Map,不能直接传入多个参数,接口绑定方案可以实现直接传入多个参数。

Mybatis的接口绑定方案与基本的使用方法不同的地方在于它是使用接口来执行sql语句,使用起来更加方便

1、SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 别名 -->
	<typeAliases>
		<!-- 为某一个特定的类起别名 -->
		<typeAlias type="bean.Flower" alias="fl"/>
		<!-- 为某一个包起别名,所有的类的别名直接为类名 -->
		<package name="bean"/>
	</typeAliases>
	
	
	<environments default="default">
		<environment id="default">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.cj.jdbc.Driver" />
				<property name="url"
					value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
	</environments>

	<mappers>
	    <!-- 普通方案 -->
		 <!-- <mapper resource="mapper/FlowerMapper.xml" /> -->
		<!-- 接口绑定方案 -->
		<package name="mapper"/>
	</mappers>
</configuration>

2、Mapper.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
	
<mapper namespace="mapper.FlowerMapper">
	<select id="selectAll" resultType="fl">
		select * from flower
	</select>
 	<select id="selectById2" resultType="fl">
		select * from flower where id=#{param1} or id=#{param2}
 	</select>
</mapper>

Mapper.xml与一般的配置没有什么太大的区别,但必须要保证namespace是接口的 包路径.接口名 ;每一个id都在接口中有对应的方法。如果传递的是多参数,则不需要设置parameterType。

在将这个mapper文件加入到主配置文件时也有一些改变,如下:

<mappers>
	    <!-- 普通方案 -->
		<!-- <mapper resource="mapper/FlowerMapper.xml" /> -->
		<!-- 接口绑定方案 -->
		<package name="mapper"/>
</mappers>

package中设置的是接口的包名

3、配置接口

public interface FlowerMapper {
	List<Flower> selectAll();
	List<Flower> selectById2(int id1,int id2);
}

接口中的方法selectById2与mapper中的selectById2相对应。

4、执行sql语句

public static void main(String[] args) throws IOException {
		InputStream ins=Resources.getResourceAsStream("SqlMapConfig2.xml");
		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(ins);
		SqlSession sqlsession=sqlSessionFactory.openSession();
		
		//mybatis自动实例化接口
		FlowerMapper fm=sqlsession.getMapper(FlowerMapper.class);
		
		//实现查询
		List<Flower> list=fm.selectAll();
		for(Flower f:list){
			System.out.println(f.toString());
		}
		System.out.println("--------------------------");
		
		//多参数传递
		list=fm.selectById2(1,2);
		for(Flower f:list){
			System.out.println(f.toString());
		}
		System.out.println("--------------------------");
	}

最后的输出结果为:

Flower [id=1, name=牡丹, price=3.0, production=无]
Flower [id=2, name=兰花, price=6.0, production=无]
Flower [id=3, name=百合, price=5.0, production=无]
Flower [id=9, name=太阳花, price=9.0, production=ab]
--------------------------
Flower [id=1, name=牡丹, price=3.0, production=无]
Flower [id=2, name=兰花, price=6.0, production=无]
--------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值