mybaits 動態SQL語句

-------------------------動態查詢---------------------------------------
<select id="findAll" parameterType="map" resultMap="studentMap">
  select * from students
  <where>
   <if test="pid!=null">
    and students_id = #{pid}
   </if>
   <if test="pname!=null">
    and students_name = #{pname}
   </if>
   <if test="psal!=null">
    and students_sal = #{psal}
   </if>
  </where>
 </select>java

 

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("studentNamespace.findAll",map);
  }catch(Exception e){
   e.printStackTrace();
   throw e;
  }finally{
   MybatisUtil.closeSqlSession();
  }
 }
-----------------------------------動態更新-----------------------------------------
<update id="dynaUpdate" parameterType="map">
  update students
  <set>
   <if test="pname!=null">
    students_name = #{pname},
   </if>
   <if test="psal!=null">
    students_sal = #{psal},   
   </if>
  </set>
  where students_id = #{pid}
 </update>sql

 

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("studentNamespace.dynaUpdate",map);
   sqlSession.commit();
  }catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback();
   throw e;
  }finally{
   MybatisUtil.closeSqlSession();
  }
 }
-----------------------------------動態刪除----------------------------------------------
<delete id="dynaDeleteArray">
  delete from students where students_id in
  <!-- foreach用於迭代數組元素
    open表示開始符號
    close表示結束符合
    separator表示元素間的分隔符
    item表示迭代的數組,屬性值能夠任意,但提倡與方法的數組名相同
    #{ids}表示數組中的每一個元素值
   -->
  <foreach collection="array" open="(" close=")" separator="," item="ids">
   #{ids}
  </foreach>
 </delete>數組

 


/**
  * 根據ID批量刪除學生(數組版本)
  */
 public void dynaDeleteArray(int... ids) throws Exception{
  SqlSession sqlSession = null;
  try{
   sqlSession = MybatisUtil.getSqlSession();
   sqlSession.delete("studentNamespace.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("studentNamespace.dynaDeleteList",ids);
   sqlSession.commit();
  }catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback();
   throw e;
  }finally{
   MybatisUtil.closeSqlSession();
  }
 }
------------------------------------動態插入----------------------------------------------
<!-- sql片斷對應字段名,id屬性值任意 -->
 <sql id="key">
  <!-- 去掉最後一個, -->
  <trim suffixOverrides=",">
   <if test="id!=null">
    students_id,
   </if>
   <if test="name!=null">
    students_name,
   </if>
   <if test="sal!=null">
    students_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="cn.itcast.javaee.mybatis.app14.Student">
  insert into students(<include refid="key"/>) values(<include refid="value"/>)
 </insert>mybatis

 

public void dynaInsert(Student student) throws Exception{
  SqlSession sqlSession = null;
  try{
   sqlSession = MybatisUtil.getSqlSession();
   sqlSession.insert("studentNamespace.dynaInsert",student);
   sqlSession.commit();
  }catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback();
   throw e;
  }finally{
   MybatisUtil.closeSqlSession();
  }
 }app

相關文章
相關標籤/搜索