<?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"><mappernamespace="dyna"><!-- create table STUDENT (
ID NUMBER not null,
NAME VARCHAR2(20),
SEX NUMBER(2),
HOBBY VARCHAR2(200),
AGE NUMBER )
--><!-- sql用於定義共享的sql 其他select中通過include引用該sql --><sqlid="queryColumn">
name,sex,age
</sql><selectid="queryStudnetABCDEFG">
select
<!-- include 應用 --><includerefid="queryColumn"></include>
from book
</select><!-- trim
suffixOverrides
prefixOverrides 要覆蓋的字符串
suffix
prefix 替換的字符串
--><insertid="addStudent"parameterType="map" >
insert into student(
<!-- trim 前綴替換用法 --><trimprefixOverrides=","prefix="">
,id
<iftest="name!=null and name!=null" >
,name
</if><iftest="sex!=null and sex!=null" >
,sex
</if><iftest="hobby!=null and hobby!=null" >
,hobby
</if><iftest="age!=null and age!=null" >
,age
</if></trim>
)
values(
#{id},
<!-- trim 後綴替換用法 --><trimsuffixOverrides=","suffix=""><iftest="name!=null and name!=''" >
#{name},
</if><iftest="sex!=null and sex!=''" >
#{sex},
</if><iftest="hobby!=null and hobby!=''" >
#{hobby},
</if><iftest="age!=null and age!=''" >
#{age},
</if></trim>
)
<!-- selectKey主鍵生成 --><selectKeykeyProperty="id"order="BEFORE"resultType="string"
>
select max(id)+1 from student
</selectKey></insert><!-- where --><selectid="queryStudent"parameterType="map"resultType="map">
select * from student
<!-- where 默認代替第一的 and 爲 where--><where><iftest="id!=null and id!=''" >
and id=#{id}
</if><iftest="name!=null and name!=''" >
and name=#{name}
</if><iftest="sex!=null and sex!=''" >
and sex=#{sex}
</if><iftest="hobby!=null and hobby!=''" >
and hobby=#{hobby}
</if><iftest="age!=null and age!=''" >
and age=#{age}
</if></where></select><!-- set --><updateid="updateStudent"parameterType="map" >
update student
<!-- set 默認會去掉最後一個 逗號(,) --><set><iftest="name!=null and name!=''" >
name=#{name},
</if><iftest="sex!=null and sex!=''" >
sex=#{sex},
</if><iftest="hobby!=null and hobby!=''" >
hobby=#{hobby},
</if><iftest="age!=null and age!=''" >
age=#{age},
</if></set>
where id=#{id}
</update><!-- foreach --><selectid="queryStudentByHobby"parameterType="map"resultType="map"><!-- open 循環開始前的字符串
close 循環結束的字符串
collection 循環的參數(該參數是一個集合或數組)
index 循環的索引
item 循環所使用的變量
separator 除了最後一次循環外 所要拼接的字符串(分隔符)
--><foreachopen="select * from student where hobby in("close=")"collection="hobbys"index="i"item="hobby"separator=","
>
#{hobby}
</foreach></select></mapper>
privatestatic SqlSession sqlSession;
@BeforepublicvoidgetSqlSession() throws IOException{
if (sqlSession==null) {
final String mybatisConf = "mybatis-conf.xml";
InputStream in = Resources.getResourceAsStream(mybatisConf);
SqlSessionFactory ssf= new SqlSessionFactoryBuilder() .build(in);
sqlSession=ssf.openSession();
}
}
@AfterpublicvoidcloseSqlSession(){
sqlSession.commit();
sqlSession.close();
}
@TestpublicvoidtestDynaSqlWhere(){
String statement="dyna.queryStudent";
Map map= new HashMap();
map.put("id", "1");
map.put("name", "oracle");
List result=sqlSession.selectList(statement, map);
System.out.println("---> " +result);
}
@TestpublicvoidtestDynaSqlSet(){
String statement="dyna.updateStudent";
Map map= new HashMap();
map.put("id", "1");
map.put("name", "11122");
map.put("age", "30");
sqlSession.update(statement, map);
}
@TestpublicvoidtestDynaSqlForEach(){
String statement="dyna.queryStudentByHobby";
Map map= new HashMap();
String[] hobbys={"playgame","readbook","listenCD"};
map.put("hobbys",hobbys );
List result=sqlSession.selectList(statement, map);
System.out.println("---> " +result);
}
@TestpublicvoidtestDynaSqlTrim(){
String statement="dyna.addStudent";
Map map= new HashMap();
map.put("hobby","playgame");
sqlSession.insert(statement, map);
}