博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybaits 动态SQL语句
阅读量:7103 次
发布时间:2019-06-28

本文共 3808 字,大约阅读时间需要 12 分钟。

hot3.png

-------------------------动态查询---------------------------------------

<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>

 

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>

 

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>

 

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

转载于:https://my.oschina.net/chenliyong/blog/673603

你可能感兴趣的文章
如何快速将Excel表格转换成PDF
查看>>
云计算在未来一年的发展预测
查看>>
kafka实战
查看>>
服务器常用命令及使用
查看>>
报错:Socket connection closed by the other side (how rude!)
查看>>
iOS之RunLoop
查看>>
去除titleBar
查看>>
CentOS下快速yum安装LAMP环境
查看>>
sysbench-0.5的安装和做性能测试
查看>>
Linux内核调优部分参数说明
查看>>
Linux之部署Zabbix监控系统
查看>>
Sharepoint 2013 整合 Office Web Apps Server 2013
查看>>
gcc 不支持__attribute__((naked)
查看>>
C语言基础之类型,运算符,表达式
查看>>
写给即将到来的你
查看>>
XenDesktop5各版本特性对比
查看>>
Skype For Business Server 2015 离线消息
查看>>
centos6.5上dstat的安装
查看>>
Windows优化策略及Netcat后门工具
查看>>
利用random生成6位随机验证码
查看>>