文章目录
limit用法
【生产环境订正数据一定要提前备份!!】
以阿里云sql服务用法示例
limit 0,10
查询第一条到第十条
limit 100,50
从第101条开始,查询50条记录
limit 10
查询前10条记录
删除无用数据
小知识,多表查询以后,尤其是有sql判断的,返回的vo可能大部分字段都是null,需要筛选一下,list直接remove可能会抛出 java.util.ConcurrentModificationException,可以新建一个list,add需要的vo,老的list直接clear
(
CASE
WHEN ISNULL(id) THEN
'0'
WHEN sex = 1 THEN
'1'
ELSE
'0'
END
) sex
查询mysql当前进程
show processlist;
时区问题
如果配置了@JsonFormat(pattern=“yyyy-MM-dd”,timezone = “GMT+8”),数据库没有配置指定时区,或者指定时区也为serverTimezone=GMT+8,某些特定的时间内,比如1990-09-14 00:00,插入到mysql中的时间少一个小时,变为1990-09-13 23:00
其中一个方案是
jdbc.url设定时区和JsonFormat设定时区一致
serverTimezone=Asia/Shanghai
@JsonFormat(pattern="yyyy-MM-dd",timezone = "Asia/Shanghai")
据说是因为
有国家(包含中国)政策颁布了在1986-1991年等还存在夏令时。在这样的时间区间,夏季时,会将时间拨快1个小时(即东9区时间),夏季结束时会再次将时间拨回一个小时(即东8区时间)
新增字段
alter TABLE stu ADD(`haha` VARCHAR(16) DEFAULT NULL COMMENT 'ccc');
新增字段的速度要看该表数据量的大小,每条数据都要执行一遍
parameterType
<select id="xxx" resultType="xxxDO" parameterType="java.lang.Long">
SELECT <include refid="all_fields"/> FROM table1
WHERE order_id = #{orderId}
</select>
没有map,不可用使用 if test
time不能是String
不会报错,但是影响查询结果
AND create_time <= CONCAT(‘2019-09-24’, ’ 23:59:59’)
AND create_time >= CONCAT(‘2019-07-21’, ’ 00:00:00’)
控制台输出sql语句
dao目录<!-- mysql打印语句 -->
<logger name="com.lsh.demo.bootstone.dao" level="DEBUG" />
判断参数是否等于某个值
<if test="finalType!=null and finalType=='2'.toString()" >
and type in (2)
</if>
<if test="times =='0'.toString()">
time = #{times}
</if>
<if test="hotelName!=null and hotelName!=''">
AND t1.hotel_name like concat('%',#{hotelName},'%')
</if>
<if test="guestName!=null and guestName!=''">
AND t1.guest_name = #{guestName}
</if>
时间是不等于null
<if test="payEndTime != null">
AND create_time <= CONCAT(#{payEndTime},' 23:59:59')
</if>
时间判断
<if test="xxxEndTime != null and xxxStartTime!=null">
and date(create_time) <![CDATA[ >= ]]> #{xxxStartTime} and date(create_time) <![CDATA[ <= ]]> #{xxxEndTime}
</if>
AND begin_date >= CURDATE() 当前日期 2020-07-07
防注入
#方式能够很大程度防止sql注入,$方式无法防止Sql注入。
【结论】在编写MyBatis的映射语句时,尽量采用“#{xxx}”这样的格式。
若不得不使用“${xxx}”这样的参数,要手工地做好过滤工作,来防止SQL注入攻击
右连接
left join 左表有的,右表为空也查出来
union all 合并两个表
映射字段缺失
- sql能查询到,但是没有映射到java bean中,检查下sql类型和bean类型,
sql中是int,bean中是 String (一次踩坑)
the profiles “@activatedProperties@” are currently active
检查 application.properties 文件
spring.profiles.active=dev
activeByDefault 未生效
<activation>
<activeByDefault>true</activeByDefault>
</activation>