#sql标签
if标签
#if标签通过test属性给出判断的条件,如果条件成立,则将执行标签内的SQL语句
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp where
<if test="empName != null and empName != ''">
emp_name = #{empName}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
</select>
where标签
#考虑if标签中的范例出现的一种情况:当第一个if标签条件不成立而第二个条件成立时,拼接成的SQL语句中where后面连着的是and,会造成SQL语句语法错误,而where标签可以解决这个问题
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<where>
<if test="empName != null and empName != ''">
emp_name = #{empName}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
</where>
</select>
choose、when、otherwise标签
#只要有一个when的条件满足就退出,所有的when条件都不满足的时候,就执行otherwise
<select id="getEmp" resultMap="resultId">
select * from t_user
<choose>
<when test="ename!= null and ename != ''">
username = #{ename}
</when>
<when test="password!= null and password != ''">
and password = #{password}
</when>
<when test="age!= null and age != ''">
and age = #{age}
</when>
<when test="sex!= null and sex != ''">
and sex = #{sex}
</when>
<when test="email!= null and email != ''">
and email = #{email}
</when>
<otherwise>id = 1</otherwise>
</choose>
</select>
foreach标签
● foreach标签允许指定一个集合或数组,并且对这个集合或数组进行遍历
● foreach标签可以用的属性:
1、collection:指定需要遍历的集合或数组 2、item:当前遍历到的元素 3、index:当前遍历到的元素的序号 4、当遍历的集合是Map类型时,index表示键,item表示值 5、open:指定遍历开始前添加的字符串 6、close:指定遍历开始后添加的字符串 7、separator:指定每次遍历之间的分隔符
● collection属性值注意事项:
如果遍历的是List时,属性值为list 如果遍历的是数组时,属性值为array 如果遍历的是Map时,属性值可以是map.keys()、map.values()、map.entrySet() 除此之外,还可以在映射方法的参数中使用@Param()注解自定义collection属性值
#批量添加数据:
<insert id="addMoreEmp">
insert into t_emp values
<foreach collection="list" separator="," item="emp">
(null,#{emp.empName},#{emp.age},#{emp.gender},null)
</foreach>
</insert>
#批量删除数据:
<delete id="deleteMoreEmp">
delete from t_emp where emp_id in
<foreach collection="array" item="empId" separator="," open="("
close=")">
#{empId}
</foreach>
</delete>
sql标签
用于记录一段通用的SQL语句片段,在需要用到该SQL语句片段的地方中通过include标签将该SQL语句片段插入
sql标签通过id属性唯一标识一个SQL语句片段,include标签通过*refid属性*指定使用某个SQL片段
<sql id="item">
emp_id,emp_name,age,gender,dept_id
</sql>
<select id="getEmpByEmpId" resultType="Emp">
select <include refid="item"></include>
from t_emp
where emp_id = #{empId}
</select>
#ESCAPE的用法 (字符转义)
ESCAPE 关键字的主要作用就是指定一个字符替代“\”的作用。
### 查询名字带有“%”字符的用户
> SELECT * FROM user WHERE name LIKE CONCAT("%", "$%", "%") ESCAPE "$"
### 查询名字带有“_”字符的用户
> SELECT * FROM user WHERE name LIKE CONCAT("%", "a_", "%") ESCAPE "a"
需要注意的查询条件中所有通过ESCAPE指代的字符均会替代“\”的作用。
### 假设存在名字为 %a 和 %_ 两个的用户
> SELECT * FROM user WHERE name LIKE "a%_" ESCAPE "a" ### %a %_
> SELECT * FROM user WHERE name LIKE "a%a" ESCAPE "a" ### %a
> SELECT * FROM user WHERE name LIKE "a%a_" ESCAPE "a" ### %_
#case when的几种用法
#用法一(case 字段 when):
select
case c_sex when 1 then '男'
else '女'
end as '性别',
count(*) as '人数' from user_inofr
group by '性别'
#用法二(case when 字段):
select
case when c_sex=1 then '男'
else '女'
end as '性别',
count(*) as '人数' from user_inofr
group by '性别'
#用法三(部分处理,其他保留原样):
select
case when c_quantity is null then '0'
else c_quantity
end as '用量',
count(*) from material_detail
group by '用量'
#用法四(between):
select
case when c_month between 1 and 3 then '一'
when c_month between 4 and 6 then '二'
when c_month between 7 and 9 then '三'
else '四'
end as '季度',sum(c_salesum) '销量' FROM spiderdata_car_salesum
group by 季度
order by 季度
#FIND_IN_SET函数的使用
FIND_IN_SET(str,strlist)函数
第一个参数:可以是字段也可以是字段中的一个数据
第二个参数:可以使字段集合也可以是以逗号作为分割根据的字符串
#查询id为1,3,5,7的分类信息
SELECT * FROM category WHERE FIND_IN_SET(id,'1,3,5,7')
#查询id为1的分类信息
SELECT * FROM category WHERE FIND_IN_SET(1,id)
<if test="ids != null and ids != ''">
and FIND_IN_SET (id, #{ids,jdbcType=VARCHAR})
</if>
#按周/月/日统计数据的方法
按天统计
select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days;
按月统计
select DATE_FORMAT(create_time,'%Y%m') months,count(caseid) count from tc_case group by months;
DATE_FORMAT(date,format)
按天统计
select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks;
或
查看MySQL的manual
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
其中
1如果周一为一周的第一天,则(小写) DATE_FORMAT(date,'%x %v')
2如果周日为一周的第一天,则(大写) DATE_FORMAT(date,'%X %V')
[sql]
select DATE_FORMAT(date,'%x年-第%v周') as week,sum(money) as money from finance_base where DATE_FORMAT(date,'%Y')=2010 group by week
select DATE_FORMAT(date,'%x年-第%v周') as week,sum(money) as money from finance_base where DATE_FORMAT(date,'%Y')=2010 group by week
根据format字符串格式化date值。下列修饰符可以被用在format字符串中:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位 www.2cto.com
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”