mybatis——动态SQL

我们在操作数据库时常常需要进行SQL语句的拼接。有了动态SQL之后,语句拼接更加容易操作。

下面我们通过一个例子实现动态sql应用:

一、创建工程如下所示:

二、user_info代码如下:

<?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">
<mapper namespace="online.shenran.userInfo.dao.IStudentDao">

	<!-- 查询语句拼接 where if 
	元素只会在至少有一个子元素的条件返回 SQL子句的情况下才去插入“WHERE”子句;
	而且,若语句的开头为“AND”或“OR”,where 元素也会将它们去除 -->
	<!--  <select id="select" resultType="online.shenran.vo.StuInfo">
		select id,user_name userName,password,real_name realName,age
		from  user_info 
		<where>
			<if test="userName!=null">
				and user_name like #{userName}
			</if>	
			<if test="age!=null">
				and age = #{age}
			</if>
			</where>	
	</select>-->
	
	<!-- 查询语句拼接 choose (when, otherwise)
	类似 Java 中的 switch 语句
	-->
	<select id="select" resultType="online.shenran.vo.StuInfo">
		select id,user_name userName,password,real_name realName,age
		from  user_info 
		<choose>
			<when test="userName!=null">
				and user_name like #{userName}
			</when>	
			<when test="age!=null">
				and age = #{age}
			</when>	
		</choose>
	</select>
	
	<!-- 多项删除SQL拼接 foreach
	foreach元素用于对一个集合进行遍历,构建 IN 条件语句时常用该元素;
	foreach 元素允许指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量,
	也允许指定开头与结尾的字符串以及在迭代结果之间放置分隔符。 -->
	<delete id="delete">
		delete from  user_info 
		where id in 
		<foreach item="id" collection="list" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</delete>
	
	<!--修改SQL拼接   set 
	元素可以用于动态包含需要更新的列,而删掉无关的逗号  -->
	<update id="update">
		update user_info 
		<set>
			<if test="realName!=null">
				real_name=#{realName},
			</if>
			<if test="age!=null">
				age=#{age}
			</if>
		</set>
		where id =#{id}
	</update>
</mapper> 

三、IStudentDao代码如下:

package online.shenran.userInfo.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import online.shenran.vo.StuInfo;

public interface IStudentDao {

	//Integer,是因为可以让传值时,可以传null
	List<StuInfo> select  (@Param ("userName") String userName, @Param("age") Integer age) ;

	boolean delete (List<Integer> list);
	
	boolean update(StuInfo stuInfo);
}

四、test代码如下:

package online.shenran.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import online.shenran.userInfo.dao.IStudentDao;
import online.shenran.vo.StuInfo;




public class Test {
	public static void main(String[] args) {
		try {
			InputStream inputStream= Resources.getResourceAsStream("mybatis_config.xml");
			SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
			
			SqlSession sqlSession =sqlSessionFactory.openSession(true);
			IStudentDao studentDao =sqlSession.getMapper(IStudentDao.class);
			List<StuInfo> list = studentDao.select(null, null);
			System.out.println("总用户数:"+list.size());
			StuInfo stuInfo=new StuInfo();
			stuInfo.setAge(12);
			stuInfo.setRealName("kim");
			if(studentDao.update(stuInfo)) {
				System.out.println("见图二数据库内数据被修改");
			}
//			List<Integer> array =new ArrayList<Integer>();
//			array.add(1);
//			array.add(2);
//			if(studentDao.delete(array)){
//				System.out.println("见图三数据库内1&2被删除");
//			}
			sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

结果如下:

首先我们通过navicat查看数据库如下:

然后执行Test,此时数据库中已经更改完成。

 

然后我们解开test中的批量删除方法。运行结果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值