Mybatis的传参

最近重新温习了遍Mybatis ,觉得还是汇总一下比较好,方便自己以后的快速开发

最终要的一点事,自己写的话,记忆更加深刻;

首先自己先写了个静态块,防止代码冗余:

private static SqlSession session;
static{
SqlSessionFactory factory = MybatisUtils.getFactory();
/**
* 映射sql的标识字符串
* com.yc.mybatis.onetoone.classMapper是classMapper.xml文件中mapper标签的namespace属性值
* getCalss是select标签的id属性值,通过select标签的id属性值就可以找到要执行的sql语句
*/
session = factory.openSession(true);
}

First:

常规类型 传单个参数:

//传单个参数 (数字类型)
@Test
public void test1(){
String statement = "com.yc.mybatis.test.classMapper.getClass1";
Classes c = session.selectOne(statement,2);
System.out.println( c);
session.close();
} //传单个参数(字符类型)
@Test
public void test10(){
String statement = "com.yc.mybatis.test.classMapper.getClass3";
Classes c = session.selectOne(statement,"ruanjian1");
System.out.println( c);
session.close();
}

xml

<select id="getClass" parameterType="int" resultMap="getClassMap">
select * from class c, teacher t where c.teacher_id = t.t_id and c.teacher_id=#{id}
</select> <select id="getClass3" parameterType="string" resultMap="getClassMap">
select * from class c, teacher t where c.teacher_id = t.t_id and c.c_name=#{name}
</select>

Map传参 适用于单个参数

@Test
public void test11(){
String statement="com.yc.mybatis.test.classMapper.getClass1";
Map<Object, Object> c=session.selectMap(statement, 1,"id");
System.out.println(c);
}

xml

<select id="getClass1" parameterType="int" resultMap="getClassMap">
select * from class where c_id=#{id}
</select>

Map传参 多个参数(也适用于单个参数)

@Test
public void test2(){
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
Map<String, Object> map=new HashMap<String, Object>();
map.put("id", 1);
map.put("name", "ruanjian1");
String statement="com.yc.mybatis.test.classMapper.getClass2";
list=session.selectList(statement,map);
System.out.println(list);
}

xml

<select id="getClass2" parameterType="hashmap" resultMap="getClassMap">
select * from class c, teacher t where c.teacher_id = t.t_id and c.teacher_id=#{id} and c.c_name=#{name}
</select>

以对象的形式传参

@Test
public void test3(){
Classes c=new Classes();
c.setName("ruanjian1");
String statement="com.yc.mybatis.test.classMapper.getClass44";
List<Object> list=session.selectList(statement,c);
System.out.println(list);
}

xml

<select id="getClass44" parameterType="com.yc.bean.Classes" resultMap="getClassMap">
select * from class c, teacher t where c.teacher_id = t.t_id and c.c_name=#{name}
</select>

Second

    对于foreach 你可以传递一个List实例或者数组作为参数对象传给MyBatis。当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称作为键。List实例将会以“list”作为键,而数组实例将会以“array”作为键。

用list传单个参数变量

@Test
public void test5(){
String statement = "com.yc.mybatis.test.classMapper.getClass5";
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
List<Object> c = session.selectList(statement,list);
for(Object o:c){
System.out.println( o);
}
session.close();
}

xml

<!--
item: 表示集合中每一个元素进行迭代时的别名
index: 指定一个名字,用于表示在迭代过程中,每次迭代到的位置;
在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。
open: 表示该语句以什么开始,
separator: 表示在每次进行迭代之间以什么符号作为分隔符,
close: 表示以什么结束
-->
<select id="getClass5" parameterType="list" resultMap="getClassMap">
select * from class c,teacher t where c.teacher_id=t.t_id and t.t_id in
<foreach collection="list" item="item" index="index"
open="(" close=")" separator=",">
#{item}
</foreach>
</select>

用List传数组形式

@Test
public void test6(){
String statement = "com.yc.mybatis.test.classMapper.getClass6";
Integer[] ids={2,3,5};
List<Object> c = session.selectList(statement,ids);
for(Object o:c){
System.out.println( o);
}
session.close();
}

xml

<select id="getClass6" parameterType="list" resultMap="getClassMap">
select * from class c,teacher t where c.teacher_id=t.t_id and t.t_id in
<foreach collection="array" item="item" index="index"
open="(" close=")" separator=",">
#{item}
</foreach>
</select>

用List传map形式

    @Test
public void test7(){
String statement = "com.yc.mybatis.test.classMapper.getClass7";
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Map<String,Object> map=new HashMap<String,Object>();
map.put("ids", list);
map.put("name", "ruanjian1");
List<Object> c = session.selectList(statement,map);
for(Object o:c){
System.out.println( o);
}
session.close();
}

xml

<select id="getClass7" parameterType="list" resultMap="getClassMap">
select * from class c,teacher t where c.teacher_id=t.t_id and c.c_name=#{name} and t.t_id in
<foreach collection="ids" item="item" index="index"
open="(" close=")" separator=",">
#{item}
</foreach>
</select>

代码分享链接:http://pan.baidu.com/s/1gfkLFCf

上一篇:IIS的地址指向


下一篇:转载:tensorflow保存训练后的模型