关键字
if
where
trim
foreach
set
【if】如果传入的p1 不为空, 那么才会SQL才拼接id = #{id}。
<select id="selectmany1" parameterType="Map" resultMap="users"> select * from test t where 1=1 <if test="p1!=null"> and username like #{p1} </if> <if test="p2!=null"> and password like #{p2} </if> </select>
@Test public void selectmany1(){ Map<String,Object> map = new HashMap<String, Object>(); map.put("p1","张%"); map.put("p2","%2%"); //map.put("p2", "%2%"); List<TestId> list=ss.selectList("com.dao.UsersMapper.selectmany1",map); for(TestId t:list){ System.out.println(t); } }
【where】mybatis的where标签是对sql语句做了处理,当它遇到AND或者OR这些,where自己就给处理了。
<select id="selectmany2" parameterType="Map" resultMap="users"> select * from test t <where> <if test="p1!=null"> and username like #{p1} </if> <if test="p2!=null"> and password like #{p2} </if> </where> </select>
@Test public void selectmany2(){ Map<String,Object> map = new HashMap<String, Object>(); map.put("p1","张%"); map.put("p2","%2%"); //map.put("p2", "%2%"); List<TestId> list=ss.selectList("com.dao.UsersMapper.selectmany2",map); for(TestId t:list){ System.out.println(t); } }
【trim】WHERE后紧随AND或则OR的时候,就去除AND或者OR。prefix为trim语句之前添加的内容,prefixOverrides 指的是忽略前缀""内的内容 ,如果有的话;同理,suffix:后缀覆盖并增加其内容; suffixOverrides:后缀判断的条件
<select id="selectmany2" parameterType="Map" resultMap="users"> select * from test t <trim prefix="where" prefixOverrides="and|or"> <if test="p1!=null"> and username like #{p1} </if> <if test="p2!=null"> and password like #{p2} </if> </trim> </select>
【foreach】foreach类似于for循环,foreach语句中的item标签代表别名,collection标签代表传递过来的数据集合的名字,open为foreach的语句之前添加上的内容,separator为每次foreach循环用其进行间隔开,close对应open,是结尾添加的内容。
<select id="selectmany3" parameterType="Map" resultMap="users"> select * from test t <if test="ll!=null"> <where> t.username in <foreach item="u" collection="ll" open="(" separator="," close=")"> #{u} </foreach> </where> </if> </select>
public void selectmany3(){ Map<String,Object> map = new HashMap<String, Object>(); List<String> l=new ArrayList<String>(); l.add("张三"); l.add("六六"); map.put("ll",l); //map.put("p2", "%2%"); List<TestId> list=ss.selectList("com.dao.UsersMapper.selectmany3",map); for(TestId t:list){ System.out.println(t); } }
【set】set标签可以理解为sql语句中进行修改update的时候进行的set语句修改
<update id="update1" parameterType="testId"> update test t <set> <if test="username!=null"> username=#{username}, </if> <if test="password!=null"> password=#{password}, </if> </set> where id=#{id} </update>
public void update1(){ TestId ti = new TestId(); ti.setId(new BigDecimal(41)); ti.setUsername("哈哈嘿嘿"); ti.setPassword("654321"); int n = ss.update("com.dao.UsersMapper.update1",ti); System.out.println(n); }
【oracle实现分页效果】
<!-- 实现分页 --> <select id="fenye" parameterType="Map" resultMap="users"> select * from <trim prefix="(" suffix=") b"> select a.*,rownum rn from <trim prefix="(" suffix=") a"> select * from test t order by t.id desc </trim> <!-- <if test="size!=null"> rownum<=#{size} </if> --> </trim> <where> b.rn between #{start} and #{size} </where> </select>
/** * 实现分页效果 */ public void select(){ Map<String,Object> map = new HashMap<String, Object>(); map.put("start", 3); map.put("size", 3); List<TestId> list=ss.selectList("com.dao.UsersMapper.fenye",map); for(TestId t:list){ System.out.println(t); } }