前言:为什么要使用动态SQL呢? 因为动态SQL更加灵活,可以实现更加复杂的CRUD功能。
基于上篇博客的基础之上我们使用动态sql来实现增删改查功能。为了方便查看,我删除了上片博客的正删改查的代码
StudentMapper.xml:
<?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">
<!-- 以下namespace名字可以随便写,但是一般都写成实体类的名字-->
<mapper namespace="com.flyz.app01.Student">
<!-- resultMap标签:映射实体与表
type属性:表示实体全路径名
id属性:为实体与表的映射取一个任意的唯一的名字
-->
<resultMap type="student" id="studentMap">
<!-- id标签:映射主键属性
result标签:映射非主键属性
property属性:实体的属性名
column属性:表的字段名
-->
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap>
<!-- ==============================下面是动态SQL功能=====================================================-->
<select id="findAllByDym" parameterType="map" resultMap="studentMap">
select * from students
<where>
<if test="pid!=null">
and id = #{pid}
</if>
<if test="pname!=null">
and name = #{pname}
</if>
<if test="psal!=null">
and sal = #{psal}
</if>
</where>
</select>
<!-- set标签自动判断哪个是最后一个字段,会自动去掉最后一个,号 -->
<update id="dynaUpdate" parameterType="map">
update students
<set>
<if test="pname!=null">
name = #{pname},
</if>
<if test="psal!=null">
sal = #{psal},
</if>
</set>
where id = #{pid}
</update>
<delete id="dynaDeleteArray">
delete from students where id in
<!-- foreach用于迭代数组元素
open表示开始符号
close表示结束符合
separator表示元素间的分隔符
item表示迭代的数组,属性值可以任意,但提倡与方法的数组名相同
#{ids}表示数组中的每个元素值
-->
<foreach collection="array" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete>
<delete id="dynaDeleteList">
delete from students where id in
<foreach collection="list" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete>
<!-- ===================================动态插入功能=====================================================-->
<!-- sql片段对应字段名,id属性值任意 -->
<sql id="key">
<!-- 去掉最后一个, -->
<trim suffixOverrides=",">
<if test="id!=null">
id,
</if>
<if test="name!=null">
name,
</if>
<if test="sal!=null">
sal,
</if>
</trim>
</sql>
<!-- sql片段对应?,id属性值任意 -->
<sql id="value">
<!-- 去掉最后一个, -->
<trim suffixOverrides=",">
<if test="id!=null">
#{id},
</if>
<if test="name!=null">
#{name},
</if>
<if test="sal!=null">
#{sal},
</if>
</trim>
</sql>
<!-- <include refid="key"/>和<include refid="value"/>表示引用上面定义的sql片段 -->
<insert id="dynaInsert" parameterType="student">
insert into students(<include refid="key"/>) values(<include refid="value"/>)
</insert>
</mapper>
StudentDao.java:
package com.flyz.app01;
import com.flyz.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class StudentDao {
//=================================以下是基于动态SQL的实现功能===============================================================
/**
* 有条件的查询所有学生
*/
public List<Student> findAll(Integer id,String name,Double sal) throws Exception {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("pid", id);
map.put("pname", name);
map.put("psal", sal);
return sqlSession.selectList(Student.class.getName() +".findAllByDym", map);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
MybatisUtil.closeSqlSession();
}
}
/**
* 有条件更新学生
*/
public void dynaUpdate(Integer id,String name,Double sal) throws Exception {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
Map<String, Object> map = new HashMap<String, Object>();
map.put("pid", id);
map.put("pname", name);
map.put("psal", sal);
sqlSession.update(Student.class.getName() +".dynaUpdate", map);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
} finally {
MybatisUtil.closeSqlSession();
}
}
/**
* 根据ID批量删除学生(数组版本)
*/
public void dynaDeleteArray(int... ids) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
sqlSession.delete(Student.class.getName()+".dynaDeleteArray",ids);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
/**
* 根据ID批量删除学生(集合版本)
*/
public void dynaDeleteList(List<Integer> ids) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
sqlSession.delete("com.flyz.app01.Student.dynaDeleteList",ids);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
/**
* 插入学生
*/
public void dynaInsert(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
sqlSession.insert(Student.class.getName()+".dynaInsert",student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
}
以上需要说明的是,多条件查询的时候,可以只写其中一个条件或者全部写上均可
网友评论