一、动态SQL的用法

where 1=1 目的解决1.不影响查询的结果 2.做后边的and拼接操作,如果前边条件不成立,where 直接拼接and查询报错。

where和if一般结合使用:
a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的
and去掉
注意:where标签不能去掉条件最后多余的and

  1. where 1 =1;
  2. <where>
  3. chkoutId=#{id}
  4. <if test="pName!=null and pName!=''">
  5. and filename like concat('%',#{pName},'%')
  6. </if>
  7. <if test="checkout != null and checkout != '' and checkout eq'其他'.toString()">
  8. and checkout not like concat('%','100','%')
  9. </if>
  10. <if test="time != null and time != ''">
  11. <![CDATA[and DATE_FORMAT(startTime,'%Y-%m-%d') <= DATE_FORMAT(#{time}, '%Y-%m-%d')]]>
  12. <![CDATA[and DATE_FORMAT(endTime,'%Y-%m-%d') >= DATE_FORMAT(#{time}, '%Y-%m-%d')]]>
  13. </if>
  14. </where>
  1. IFNULL(count(id),0)
  2. IFNULL(sum(dataSize),0)
  3. COUNT(errMsg='存在异常' or NULL)

注意:count在值是NULL是不统计数, (count(‘任意内容’))都会统计出所有记录数

  1. <insert id=" " useGeneratedKeys="true"
  2. keyProperty="id" parameterType=" ">
  3. INSERT INTO archive_checkout_detail (
  4. id,
  5. <include refid="column" />
  6. )
  7. VALUES (
  8. #{id, jdbcType=INTEGER}, <!-- 主键 -->
  9. #{filename, jdbcType=VARCHAR}, <!-- 产品文件名称 -->
  10. #{lv, jdbcType=VARCHAR}, <!-- 数据级别 -->
  11. #{fileSize, jdbcType=BIGINT} <!--文件大小(单位:字节) -->
  12. )
  13. </insert>
  14. <!-- 表字段 ,不包含id -->
  15. <sql id="column">
  16. chkoutId,
  17. filename,
  18. lv,
  19. fileSize
  20. </sql>
  1. <update id="updateByKey" parameterType=" ">
  2. UPDATE 表名
  3. <set>
  4. <if test="id != null">
  5. id = #{id, jdbcType=INTEGER}, <!-- id -->
  6. </if>
  7. <if test="checkoutName != null">
  8. checkoutName = #{checkoutName, jdbcType=VARCHAR}
  9. </if>
  10. </set>
  11. WHERE id = #{id}
  12. </update>

注意:

  1. Long selectCount2(@Param("taskIdList") List<Integer> taskIdList);
  2. <select id="selectCount2" resultType="long">
  3. SELECT IFNULL(count(*),0)
  4. FROM archive_check ack
  5. where ack.taskId in (
  6. <foreach close="" collection="taskIdList" item="taskId"
  7. open="" separator=",">
  8. #{taskId}
  9. </foreach>)
  10. </select>
  11. List<ArchiveCheckPO> selectExeById(@Param("archiveTaskAndCommonlv") ArchiveTaskAndCommonlv archiveTaskAndCommonlv, @Param("taskIdList") List<Integer> taskIdList);
  12. <select id="selectExeById" parameterType="ArchiveTaskAndCommonlv"
  13. resultMap="BaseResultMap">
  14. select
  15. ack.id, ack.fileName, ack.fileDir, ack.chkTime, ack.chkPhase,ack.chkType,ack.errStatus,ack.errMsg
  16. FROM archive_check ack
  17. where ack.taskId in (
  18. <foreach collection="taskIdList" item="taskId" index="index"
  19. open="(" close=")" separator=",">
  20. #{taskId}
  21. </foreach>)
  22. limit #{archiveTaskAndCommonlv.currentPage},#{archiveTaskAndCommonlv.limit}
  23. </select>
  24. int insertBatch(@Param("tableName") String tableName, @Param("archiveMetaPOList") List<ArchiveMetaPO> archiveMetaPOList);
  25. <insert id="insertBatch" useGeneratedKeys="true">
  26. INSERT INTO ${tableName} (
  27. id,
  28. <include refid="column" />
  29. )
  30. VALUES
  31. <foreach collection="archiveMetaPOList" item="archiveMetaPO" index="index"
  32. open="(" close=")" separator=",">
  33. (
  34. #{archiveMetaPO.id, jdbcType=INTEGER}, <!-- id -->
  35. #{archiveMetaPO.archId, jdbcType=VARCHAR}, <!-- 存档编号 -->
  36. #{archiveMetaPO.taskId, jdbcType=INTEGER} <!-- 归档任务id,外键(archive_task(id)) -->
  37. )
  38. </foreach>
  39. </insert>
  40. MyBatis传入参数为list、数组、map写法
  41. /**
  42. *传入数组
  43. *
  44. */
  45. public List<Student> findStudentByArray(Integer[] ids);
  46. <select id="findStudentByArray" parameterType="Integer" resultType="com.ali.entity.Student">
  47. select id,st_name,st_age from t_student where id in
  48. <foreach collection="array" item="item_id" open"(" separator="," close=")">
  49. #{item_id}
  50. </foreach>
  51. </select>
  52. /**
  53. *传入哈希
  54. *
  55. */
  56. public List<Map> findStudentByMap(Map<String,Object> ids);
  57. <select id="findStudentByMap" parameterType="Map" resultType="com.ali.entity.Student">
  58. select id,st_name,st_age from t_student where id in
  59. <foreach collection="ids" item="item_id" open="(" separator="," close=")">
  60. #{item_id}
  61. </foreach>
  62. </select>
  63. /**
  64. *传入列表
  65. *
  66. */
  67. public List<Student> findStudentByList(List<Integer> ids);
  68. <select id "findStudentByList" parameterType="Integer" resultType="com.ali.entity.Student">
  69. select id,st_name,st_age from t_student where id in
  70. <foreach collection="list" item="item_id" open"(" separator="," close=")">
  71. #{item_id}
  72. </foreach>
  73. </select>

在MyBatis框架中:JavaType和ofType的区别:
JavaType用来指定POJO中属性的类型
ofType用来指定映射到List集合中POJO的类型

  1. public class User {
  2. private int id;
  3. private String name;
  4. private String mobile;
  5. private List<Post>posts;
  6. }
  7. <resultMap type="User" id="resultUserMap">
  8. <result property="id" javaType="int" column="user_id" />
  9. <result property="name" javaType="string" column="username" />
  10. <result property="mobile" column="mobile" />
  11. <!--javatype指定的是user对象的属性的类型, oftype指定的是映射到list集合属性中POJO的类型,本例指的是post类型)-->
  12. <collection property="posts" ofType="Post" javaType="java.util.ArrayList" column="id">
  13. <id property="id" column="post_id" javaType="int" jdbcType="INTEGER"/>
  14. <result property="title" column="title" javaType="string" jdbcType="VARCHAR"/>
  15. <result property="content" column="content" javaType="string" jdbcType="VARCHAR"/>
  16. </collection>
  17. </resultMap>
  1. private int id;
  2. private String chat_name;
  3. private int chat_hot;
  4. private List<chat_son> son;
  5. private List<chat_myson> myson;
  6. <resultMap id="BaseResultMap" type="cn.zcbigdata.mybits_demo.entity.Chat">
  7. <id column="id" jdbcType="INTEGER" property="id" />
  8. <result column="chat_name" jdbcType="VARCHAR" property="chat_name" />
  9. <result column="chat_hot" jdbcType="INTEGER" property="chat_hot" />
  10. <collection property="son" column="chat_name" ofType="cn.zcbigdata.mybits_demo.entity.chat_son" select="select_son">
  11. <id column="id" jdbcType="INTEGER" property="id" />
  12. <result column="chat_name" jdbcType="VARCHAR" property="chat_name" />
  13. <result column="chat_reply" jdbcType="VARCHAR" property="chat_reply" />
  14. <result column="chat_preson" jdbcType="VARCHAR" property="chat_preson" />
  15. </collection>
  16. //property属性里是在java的实体类中的变量名称,即上文List所对应的变量名称
  17. //column属性是子查询的字段
  18. //ofType属性,因为此次是要查询返回的是一个集合,所以我们使用ofType
  19. //select属性主要是选择要执行的sql语句,找到对应的id即可
  20. <collection property="myson" column="chat_name" ofType="cn.zcbigdata.mybits_demo.entity.chat_myson" select="select_myson">
  21. <id column="id" jdbcType="INTEGER" property="id" />
  22. <result column="chat_name" jdbcType="VARCHAR" property="chat_name" />
  23. <result column="chat_myReply" jdbcType="VARCHAR" property="chat_myReply" />
  24. </collection>
  25. </resultMap>
  26. <!-- 通过话题名称显示,有模糊查询-->
  27. <select id="select_chatName" resultMap="BaseResultMap" parameterType="String">
  28. select id,chat_name,chat_hot
  29. from chat where chat_name like CONCAT('%',#{chat_name},'%')
  30. <!-- limit #{offset},#{limit}-->
  31. </select>
  32. <!-- 他人回复 子查询-->
  33. <select id = "select_son" resultType="cn.zcbigdata.mybits_demo.entity.chat_son" parameterType="String">
  34. select id,chat_reply,chat_person from chat_son where chat_name = #{chat_name}
  35. </select>
  36. <!-- 自己回复 子查询-->
  37. <select id="select_myson" resultType="cn.zcbigdata.mybits_demo.entity.chat_myson" parameterType="String">
  38. select id,chat_myReply from chat_myson where chat_name = #{chat_name}
  39. </select>

二、MyBatis的缓存

三、MyBatis分页插件