第一次随手记
之前很多都以及解决的又会重复碰到 去网上找很麻烦所以就自己记下来
关于mybatis 的一些参数问题
public List<Map<String, Object>> findAllTMeetingTop4(@Param("type") String type);
<select id="findAllTMeetingTop4" resultType="Map">
SELECT
a.type,
a.begintime,
a.endtime,
b.name AS cpcName
FROM table a
LEFT JOIN table b ON a.orgID=b.id
WHERE 1=1
<if test=" type.equals('0') ">
and meetingtype='党支部委员会'
</if>
<if test=" type.equals('1')">
and meetingtype='支部党员大会'
</if>
<if test=" type.equals('2') ">
and meetingtype='党小组会'
</if>
<if test=" type.equals('3') ">
and meetingtype='党课''
</if>
<if test=" type.equals('4') ">
and meetingtype='其他'
</if>
ORDER BY begintime DESC LIMIT 4
</select>
<if test=" type.equals('4') "></if>
这中间的 type是传递的参数 作为字符穿的比较 可以是 type.toString()方法 也可以是eq的方法比较
mybatis的数组集合查询方法
<select id="findTMeetingTop2Bycpcid" resultType="Map">
SELECT
a.id AS id,
a.begintime,
a.endtime,
ifnull(b.name,"")AS cpcName
FROM
t_meeting a
LEFT JOIN sys_office b
ON a.orgID = b.id
WHERE a.orgID IN
<foreach collection="list" item="cpcids" index="index" open="(" close=")" separator=",">
#{cpcids}
</foreach>
ORDER BY a.create_date DESC LIMIT 2
</select>
关于当前表的分组取N条数据
SELECT
a.*
FROM table a
WHERE N>(
SELECT COUNT(*) FROM table b
WHERE a.type=b.type AND a.create_date>b.create_date )
ORDER BY a.type,a.create_date DESC
----------------------------------------------
table 当前表
N 多少条数据
type 分组的根据
create_date 作为分组的排序比较
mysql的递归 写的时候根本就没有注意表结构 表中有 parent_id 和 parent_ids
当时用的是parent_id 自己写的函数递归查询的 但是在mysql中有一个函数
FIND_IN_SET(#{id},a.parent_ids) 可以直接递归
此查询是查询自己以及所有的下级
<select id="getlistOffice" resultType="Office">
SELECT
<include refid="officeColumns"/>
FROM sys_office a
<include refid="officeJoins"/>
<!-- WHERE a.id = #{id} OR a.id IN (getOffice(#{id})) -->
WHERE a.id = #{id} OR FIND_IN_SET(#{id},a.parent_ids)
</select>
getOffice(#{id})这个函数还是要记下来 说不准什么时候自己就需要了
DELIMITER $$
USE `newjeeplus`$$
DROP FUNCTION IF EXISTS `getOffice`$$
CREATE DEFINER=`root`@`%` FUNCTION `getOffice`(offid VARCHAR(4000)) RETURNS
VARCHAR(4000) CHARSET latin1
BEGIN
DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);
SET sTemp = '$';
SET sTempChd =CAST(offid AS CHAR);
WHILE sTempChd IS NOT NULL DO
SET sTemp = CONCAT(sTemp,',',sTempChd);
SELECT GROUP_CONCAT(id) INTO sTempChd FROM sys_office WHERE
FIND_IN_SET(parent_id,sTempChd)>0;
END WHILE;
RETURN sTemp;
END$$
DELIMITER ;