一、动态SQL的用法
where 1=1 目的解决1.不影响查询的结果 2.做后边的and拼接操作,如果前边条件不成立,where 直接拼接and查询报错。
where和if一般结合使用:
a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的
and去掉
注意:where标签不能去掉条件最后多余的and
where 1 =1;<where>chkoutId=#{id}<if test="pName!=null and pName!=''">and filename like concat('%',#{pName},'%')</if><if test="checkout != null and checkout != '' and checkout eq'其他'.toString()">and checkout not like concat('%','100','%')</if><if test="time != null and time != ''"><![CDATA[and DATE_FORMAT(startTime,'%Y-%m-%d') <= DATE_FORMAT(#{time}, '%Y-%m-%d')]]><![CDATA[and DATE_FORMAT(endTime,'%Y-%m-%d') >= DATE_FORMAT(#{time}, '%Y-%m-%d')]]></if></where>
IFNULL(count(id),0)IFNULL(sum(dataSize),0)COUNT(errMsg='存在异常' or NULL)
注意:count在值是NULL是不统计数, (count(‘任意内容’))都会统计出所有记录数
<insert id=" " useGeneratedKeys="true"keyProperty="id" parameterType=" ">INSERT INTO archive_checkout_detail (id,<include refid="column" />)VALUES (#{id, jdbcType=INTEGER}, <!-- 主键 -->#{filename, jdbcType=VARCHAR}, <!-- 产品文件名称 -->#{lv, jdbcType=VARCHAR}, <!-- 数据级别 -->#{fileSize, jdbcType=BIGINT} <!--文件大小(单位:字节) -->)</insert><!-- 表字段 ,不包含id --><sql id="column">chkoutId,filename,lv,fileSize</sql>
<update id="updateByKey" parameterType=" ">UPDATE 表名<set><if test="id != null">id = #{id, jdbcType=INTEGER}, <!-- id --></if><if test="checkoutName != null">checkoutName = #{checkoutName, jdbcType=VARCHAR}</if></set>WHERE id = #{id}</update>
注意:
Long selectCount2(@Param("taskIdList") List<Integer> taskIdList);<select id="selectCount2" resultType="long">SELECT IFNULL(count(*),0)FROM archive_check ackwhere ack.taskId in (<foreach close="" collection="taskIdList" item="taskId"open="" separator=",">#{taskId}</foreach>)</select>List<ArchiveCheckPO> selectExeById(@Param("archiveTaskAndCommonlv") ArchiveTaskAndCommonlv archiveTaskAndCommonlv, @Param("taskIdList") List<Integer> taskIdList);<select id="selectExeById" parameterType="ArchiveTaskAndCommonlv"resultMap="BaseResultMap">selectack.id, ack.fileName, ack.fileDir, ack.chkTime, ack.chkPhase,ack.chkType,ack.errStatus,ack.errMsgFROM archive_check ackwhere ack.taskId in (<foreach collection="taskIdList" item="taskId" index="index"open="(" close=")" separator=",">#{taskId}</foreach>)limit #{archiveTaskAndCommonlv.currentPage},#{archiveTaskAndCommonlv.limit}</select>int insertBatch(@Param("tableName") String tableName, @Param("archiveMetaPOList") List<ArchiveMetaPO> archiveMetaPOList);<insert id="insertBatch" useGeneratedKeys="true">INSERT INTO ${tableName} (id,<include refid="column" />)VALUES<foreach collection="archiveMetaPOList" item="archiveMetaPO" index="index"open="(" close=")" separator=",">(#{archiveMetaPO.id, jdbcType=INTEGER}, <!-- id -->#{archiveMetaPO.archId, jdbcType=VARCHAR}, <!-- 存档编号 -->#{archiveMetaPO.taskId, jdbcType=INTEGER} <!-- 归档任务id,外键(archive_task(id)) -->)</foreach></insert>MyBatis传入参数为list、数组、map写法/***传入数组**/public List<Student> findStudentByArray(Integer[] ids);<select id="findStudentByArray" parameterType="Integer" resultType="com.ali.entity.Student">select id,st_name,st_age from t_student where id in<foreach collection="array" item="item_id" open"(" separator="," close=")">#{item_id}</foreach></select>/***传入哈希**/public List<Map> findStudentByMap(Map<String,Object> ids);<select id="findStudentByMap" parameterType="Map" resultType="com.ali.entity.Student">select id,st_name,st_age from t_student where id in<foreach collection="ids" item="item_id" open="(" separator="," close=")">#{item_id}</foreach></select>/***传入列表**/public List<Student> findStudentByList(List<Integer> ids);<select id "findStudentByList" parameterType="Integer" resultType="com.ali.entity.Student">select id,st_name,st_age from t_student where id in<foreach collection="list" item="item_id" open"(" separator="," close=")">#{item_id}</foreach></select>
在MyBatis框架中:JavaType和ofType的区别:
JavaType用来指定POJO中属性的类型
ofType用来指定映射到List集合中POJO的类型
public class User {private int id;private String name;private String mobile;private List<Post>posts;}<resultMap type="User" id="resultUserMap"><result property="id" javaType="int" column="user_id" /><result property="name" javaType="string" column="username" /><result property="mobile" column="mobile" /><!--javatype指定的是user对象的属性的类型, 而oftype指定的是映射到list集合属性中POJO的类型,本例指的是post类型)--><collection property="posts" ofType="Post" javaType="java.util.ArrayList" column="id"><id property="id" column="post_id" javaType="int" jdbcType="INTEGER"/><result property="title" column="title" javaType="string" jdbcType="VARCHAR"/><result property="content" column="content" javaType="string" jdbcType="VARCHAR"/></collection></resultMap>
private int id;private String chat_name;private int chat_hot;private List<chat_son> son;private List<chat_myson> myson;<resultMap id="BaseResultMap" type="cn.zcbigdata.mybits_demo.entity.Chat"><id column="id" jdbcType="INTEGER" property="id" /><result column="chat_name" jdbcType="VARCHAR" property="chat_name" /><result column="chat_hot" jdbcType="INTEGER" property="chat_hot" /><collection property="son" column="chat_name" ofType="cn.zcbigdata.mybits_demo.entity.chat_son" select="select_son"><id column="id" jdbcType="INTEGER" property="id" /><result column="chat_name" jdbcType="VARCHAR" property="chat_name" /><result column="chat_reply" jdbcType="VARCHAR" property="chat_reply" /><result column="chat_preson" jdbcType="VARCHAR" property="chat_preson" /></collection>//property属性里是在java的实体类中的变量名称,即上文List所对应的变量名称//column属性是子查询的字段//ofType属性,因为此次是要查询返回的是一个集合,所以我们使用ofType//select属性主要是选择要执行的sql语句,找到对应的id即可<collection property="myson" column="chat_name" ofType="cn.zcbigdata.mybits_demo.entity.chat_myson" select="select_myson"><id column="id" jdbcType="INTEGER" property="id" /><result column="chat_name" jdbcType="VARCHAR" property="chat_name" /><result column="chat_myReply" jdbcType="VARCHAR" property="chat_myReply" /></collection></resultMap><!-- 通过话题名称显示,有模糊查询--><select id="select_chatName" resultMap="BaseResultMap" parameterType="String">select id,chat_name,chat_hotfrom chat where chat_name like CONCAT('%',#{chat_name},'%')<!-- limit #{offset},#{limit}--></select><!-- 他人回复 子查询--><select id = "select_son" resultType="cn.zcbigdata.mybits_demo.entity.chat_son" parameterType="String">select id,chat_reply,chat_person from chat_son where chat_name = #{chat_name}</select><!-- 自己回复 子查询--><select id="select_myson" resultType="cn.zcbigdata.mybits_demo.entity.chat_myson" parameterType="String">select id,chat_myReply from chat_myson where chat_name = #{chat_name}</select>
