mybatis如何遍历Map的key和value【增删改查】

 

转:

mybatis如何遍历Map的key和value

1.sql.xml

  1. "1.0" encoding="UTF-8"?>
  2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.genius">
  4.  
  5. <select id="selectOne" parameterType="java.util.HashMap"
  6. resultType="java.util.HashMap">
  7. select count(*) as num from ${tableName} where seq =
  8. #{seq};
  9. select>
  10.  
  11. <insert id="insertOne" parameterType="java.util.Map">
  12. insert into ${tableName}
  13. <foreach collection="content.keys" item="key" open="(" close=")"
  14. separator=",">
  15. ${key}
  16. foreach>
  17. values
  18. <foreach collection="content.values" item="value" open="("
  19. close=")" separator=",">
  20. #{value}
  21. foreach>
  22. insert>
  23.  
  24. <update id="updateOne" parameterType="java.util.Map">
  25. UPDATE ${tableName} SET
  26. <foreach collection="content.keys" item="key" open="" close=""
  27. separator=",">
  28. ${key} = #{content[${key}]}
  29. foreach>
  30. where seq = #{content[seq]} and genius_uid <=
  31. #{content[genius_uid]};
  32. update>
  33.  
  34. <delete id="deleteOne" parameterType="java.util.Map">
  35. delete from ${tableName}
  36. where seq = #{content[seq]};
  37. delete>
  38.  
  39. mapper>

2.java代码:

  1. SqlSession session = MyBatisConnectionFactory.getSession( "pg");
  2. HashMap params = new HashMap<>(); //传入的参数
  3. params.put( "content", tableContent);
  4. params.put( "tableName", tableName);
  5. params.put( "seq", seq);
  6. int flag = session.delete("deleteOne", params); //删除记录
  7. HashMap map = session.selectOne( "selectOne", params); //查询记录是否存在
  8. flag = session.update( "updateOne", params) > 0 ? true : false; //更新
  9. flag = session.insert( "insertOne", params) > 0 ? true : false; //新增

转载于:https://www.cnblogs.com/libin6505/p/10899619.html

你可能感兴趣的:(mybatis如何遍历Map的key和value【增删改查】)