Mybaits映射文件配置之多参数获取

文章目录

  • Mybaits映射文件配置之多参数获取
    • 测试一下

Mybaits映射文件配置之多参数获取

​ 如果传入的参数有多个,直接通过参数名是无法获取的,因为mybaits没有进行自动绑定。如果只有一个参数,#{xxx}获取,里面填写任何单词都可以。多参数情况下,参数的默认名为:#{arg0},#{arg1} 或者 #{param1},#{param2} 两者是等价的。

测试一下

sql语句

<insert id="addUser1">
  		insert into t_user(name,age)values(#{arg0},#{arg1})
  	insert>
  	<insert id="addUser2">
  		insert into t_user(name,age)values(#{param1},#{param2})
  	insert>

测试方法

	@Test
	public void test3() throws IOException {
     
		InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		SqlSession session = factory.openSession(true);
		// 获取代理类
		UserMapper dao = session.getMapper(UserMapper.class);
		int count = dao.addUser1("zhangsan", 25);
		
		System.out.println(count);
		session.close();
	}
	
	@Test
	public void test4() throws IOException {
     
		InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		SqlSession session = factory.openSession(true);
		// 获取代理类
		UserMapper dao = session.getMapper(UserMapper.class);
		int count = dao.addUser2("zhangsan1", 26);
		
		System.out.println(count);
		session.close();
	}
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==>  Preparing: insert into t_user(name,age)values(?,?) 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: zhangsan(String), 25(Integer)
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <==    Updates: 1
1
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==>  Preparing: insert into t_user(name,age)values(?,?) 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: zhangsan1(String), 26(Integer)
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <==    Updates: 1
1

项目打包 提取码:8idb 环境:eclipse,maven

你可能感兴趣的:(Mybaits)