mybatis之动态SQL(代码实例)

动态sql是mybatis的—个强大的特性。在使用jdbc操作数据时,如果查询条件特别多,将条件串联成sql字符串是一项痛苦的事情,通常的解决方法是写很多的if-else条件语句和字符串进行拼接,并确保不能忘了空格或在字段的最后省略逗号。mybatis使用一种强大的动态sql语言来改进这种情形,动态sql基于ognl的表达式,可使我们方便地在sql语句中实现某些逻辑。

案例1 使用动态sql进行查询

sql语句:

<select id="selectbycon"   
      parametertype="com.obtk.entitys.conditionentity"   
            resulttype="studententity">  
        select * from student  where  1=1  
      
            <if test="gender!=null">  
               and gender=#{gender}  
            </if>  
            <if test="stuname!=null">  
               and stuname like concat('%',#{thename},'%')  
            </if>  
            <if test="minage!=null">  
               and age>=#{minage}  
            </if>  
            <if test="maxage!=null">  
               and age<![cdata[<=]]>#{maxage}  
            </if>  
            <if test="address!=null">  
               and address=#{address}  
            </if>  
      
    </select>  

代码:

package com.obtk.test2;

import java.util.list;

import org.apache.ibatis.session.sqlsession;

import com.obtk.entitys.conditionentity;
import com.obtk.entitys.studententity;
import com.obtk.utils.mybatisutil;

public class testconquery {
	public static void main(string[] args) {
		sqlsession session=null;
		try {
			//4.得到session
			session=mybatisutil.getsession();
			conditionentity con=new conditionentity();
			con.setminage(20);
			con.setmaxage(44);
			con.setgender("男");
			//5.执行语句
			list stulist=session.
			         selectlist("stu.selectbycon",con);
			for(studententity stu : stulist){
				system.out.println(stu.getstuname()+","+stu.getgender()
						+","+stu.getage());
			}
		} catch (exception e) {
			e.printstacktrace();
		}finally{
			mybatisutil.closesession();
		}
	}
}

或者使用where标签,如下:

<select id="selectbycon"   
      parametertype="com.obtk.entitys.conditionentity"   
            resulttype="studententity">  
        select * from student  
        <where>  
            <if test="gender!=null">  
               and gender=#{gender}  
            </if>  
            <if test="stuname!=null">  
               and stuname like concat('%',#{thename},'%')  
            </if>  
            <if test="minage!=null">  
               and age>=#{minage}  
            </if>  
            <if test="maxage!=null">  
               and age<![cdata[<=]]>#{maxage}  
            </if>  
            <if test="address!=null">  
               and address=#{address}  
            </if>  
        </where>  
    </select>  

代码同上。

where元素的作用是在写入where元素的地方输出一个where。另外,mybatis会智能处理输出条件,如果所有条件都不满足,那么mybatis会查出所有的记录;如果输出后是以and开头的,mybatis会把第一个and忽略。因此不用写1=1这样的条件了。

案例2 使用动态sql进行更新操作

sql语句


		update student 
		
			stuname=#{stuname},
			gender=#{gender},
			age=#{age},
			address=#{address},
			deptidd=#{deptidd},
		
		where stuid=#{stuid}
	

代码:

package com.obtk.test2;

import org.apache.ibatis.session.sqlsession;

import com.obtk.entitys.studententity;
import com.obtk.utils.mybatisutil;

public class testupdatebycon {
	public static void main(string[] args) {
		sqlsession session=null;
		try {
			//4.得到session
			session=mybatisutil.getsession();
			studententity thestu=new studententity("小绿花", "女", 23, "火星");
			thestu.setstuid(129);
			//5.执行语句,参数比较多,就用实体类的对象传参
			int theid=session.update("stu.updatebycon", thestu);
			session.commit();
			system.out.println("修改成功!"+theid);
		} catch (exception e) {
			e.printstacktrace();
		}finally{
			mybatisutil.closesession();
		}
	}
}
(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐