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<StudentEntity> 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 id="updateByCon" parameterType="StudentEntity" statementType="PREPARED">
		update student 
		<set>
			<if test="stuName!=null">stuName=#{stuName},</if>
			<if test="gender!=null">gender=#{gender},</if>
			<if test="age!=null">age=#{age},</if>
			<if test="address!=null">address=#{address},</if>
			<if test="deptIdd!=null">deptIdd=#{deptIdd},</if>
		</set>
		where stuId=#{stuId}
	</update>

代码:

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();
		}
	}
}

二  trim标记的使用

trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码:
示例1. 

<select id="queryByCon" parameterType="stuEntity" resultMap="stuMap">
		select * from student
		<trim prefix="where" prefixOverrides="and|or">
			<if test="studentName!=null">
				and stuName like concat('%',#{studentName},'%')
			</if>
			<if test="sex!=null">
				and gender=#{sex}
			</if>
			<if test="age!=null">
				and age>=#{age}
			</if>
		</trim>
</select>

上面两个属性的意思如下:
  prefix:前缀      
  prefixoverride:去掉第一个and或者是or

示例2.

<update id="updateByCon" parameterType="stuEntity" >
		update student 
		<trim prefix="set" suffixOverrides="," suffix="where stuId=#{stuId}">
			<if test="studentName!=null">
				stuName=#{studentName},
			</if>
			<if test="sex!=null">
				gender=#{sex},
			</if>
			<if test="age!=null">
				age=#{age},
			</if>
			<if test="address!=null">
				address=#{address},
			</if>
			<if test="deptIdd!=null">
				deptIdd=#{deptIdd},
			</if>
	   </trim>
	</update>

 上面三个属性的意义如下,其中prefix意义:自动在前面加上set标签
  suffixoverride:去掉最后一个逗号(也可以是其他的标记,
                    就像是上面前缀中的and一样)
  suffix:后缀,这里是自动在sql语句的后面加上where stuId=#{stuId}

 

三  mybatis动态sql中foreach标签的使用


foreach标签主要用于构建in条件,他可以在sql中对集合进行迭代。如下:

<delete id="deleteBatch"> 
    delete from user where id in
    <foreach collection="array" item="id"  open="(" close=")" separator=",">
      #{id}
    </foreach>
  </delete>

我们假如说参数为----  int[] ids = {1,2,3,4,5}  ----那么打印之后的SQL如下:
  delete form user where id in (1,2,3,4,5)
  释义:
    collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array
    item : 表示在迭代过程中每一个元素的别名
    index :表示在迭代过程中每次迭代到的位置(下标)
    open :前缀
    close :后缀
    separator :分隔符,表示迭代时每个元素之间以什么分隔
我们通常可以将之用到批量删除、添加和 in 查询字句 等操作中。

具体案例:

<select id="queryForEach1" resultMap="stuMap">
		select * from student where stuId in 
		<foreach collection="array" item="theId" open="(" separator="," close=")">
			#{theId}
		</foreach>
	</select>
	<select id="queryForEach2" resultMap="stuMap">
		select * from student where stuId in 
		<foreach collection="list" item="theId" open="(" separator="," close=")">
			#{theId}
		</foreach>
	</select>

代码

package com.obtk.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;

public class EachQuery {
	public static void main(String[] args) {
		SqlSession session=null;
		try {
			session=MybatisUtil.getSession();
			//int[] ids=new int[]{100,105,109,110};
			ArrayList idList=new ArrayList();
			idList.add(100);
			idList.add(105);
			idList.add(109);
			idList.add(110);
			//List<StudentEntity> stuList=session.selectList("stu.queryForEach1",ids);
			List<StudentEntity> stuList=session.selectList("stu.queryForEach2",idList);
			for(StudentEntity stu : stuList){
				System.out.println(stu.getStuId()+"\t"+stu.getStudentName()+"\t"+stu.getSex());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			MybatisUtil.closeSession();
		}
	}
}

 

 

 

 

 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis的实现逻辑过程可以大致分为以下几个步骤: 1. 配置文件加载:首先,MyBatis会加载配置文件,通常是一个XML文件,其中包含了数据库连接信息、映射文件的路径、插件配置等。配置文件可以通过Java代码或者Spring等框架进行加载。 2. SqlSessionFactory创建:配置文件加载完成后,MyBatis会根据配置信息创建一个SqlSessionFactory对象,它是MyBatis的核心接口。SqlSessionFactory负责创建SqlSession对象,同时也包含了一些全局的配置信息。 3. 映射文件加载:SqlSessionFactory会根据配置文件中的映射文件路径,加载所有的映射文件。映射文件定义了Java对象与数据库表之间的映射关系,以及SQL语句的定义。映射文件可以使用XML或者注解来配置。 4. SqlSession创建:通过SqlSessionFactory,可以创建一个SqlSession对象。SqlSession是与数据库交互的会话,它提供了执行SQL语句、提交事务等操作的方法。 5. SQL语句执行:在SqlSession中,可以通过调用相应的方法执行SQL语句。SQL语句可以直接写在代码中,也可以在映射文件中定义并引用。 6. 结果映射:执行SQL语句后,MyBatis会将查询结果映射到Java对象中。这个过程可以通过配置文件中的映射规则来完成,也可以使用注解进行配置。 7. 事务管理:MyBatis支持事务管理,可以通过SqlSession的commit()方法提交事务,或者rollback()方法回滚事务。也可以通过配置文件来自动管理事务。 8. 资源释放:在完成数据库操作后,需要调用SqlSession的close()方法关闭会话,释放资源。 总的来说,MyBatis的实现逻辑过程包括配置文件加载、SqlSessionFactory创建、映射文件加载、SqlSession创建、SQL语句执行、结果映射、事务管理和资源释放等步骤。通过这一系列的步骤,MyBatis实现了Java对象与数据库之间的映射关系,简化了数据库操作的编写过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御前两把刀刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值