mysql实践操作总结

抽取公共的sql语句

在mybatis的动态sql编写中,通过<sql>标签抽取可重用的sql片段,常见于可重复利用的查询语句,实现语句复用;在使用时,通过<include refid="sql_id"></include>引入,可达到跟写在同一个查询语句中一样的效果。

<!-- =====================公共sql语句===================== -->
<!-- 用于读者移动端,根据关键字搜索(类型,图书名,作者) -->
<sql id="searchBy">
	<where>
		<if test="bookType!= null and bookType !=''">
			and instr(book_type,#{bookType})>0
		</if>
		<if test="searchContent!= null and searchContent !=''">
			and instr(book_name,#{searchContent})>0 or instr(book_auth,#{searchContent})>0
		</if>
		and book_status=0
	</where>
</sql>

<!-- =====================读者移动端====================== -->
<!--按分类或关键字查找  -->
<select id="findBooksBy" resultMap="BookResultMap">
		select * from t_ts_book
		<include refid="searchBy"></include>
		limit #{start},#{end}
</select>

mysql中INSTR函数的用法

INSTR(STR,SUBSTR) 即在一个字符串(STR)中搜索指定的字符(SUBSTR),返回发现指定的字符的位置(INDEX);

STR 是被搜索的字符串,SUBSTR 是希望搜索的字符串。 这个函数返回字符串在某一个字段的内容中的位置, 没有找到字符串返回0,否则返回位置(从1开始),即通过where限定,当满足大于0的条件的数据即被查出(instr返回索引值>0)。       

该函数常用于数据的模糊查询,通过在mysql中使用内部函数instr,可代替传统的like方式查询,并且速度更快。

#传统方式
SELECT * FROM t_ts_book
WHERE book_type like "%computer%"

#使用instr函数
SELECT * FROM t_ts_book
WHERE instr(book_type,"computer")>0

mybatis中批量操作

foreach的格式:<foreach collection="" item="" index="" separator=","  open="("  close=")"></foreach>                           

foreach元素的属性主要有collection,item,index,separator,open,close                                                             

collection 表示要进行循环遍历的指定集合                                                                                                                             

item 表示集合中每一个元素迭代时候的别名               

index 表示在迭代过程中的索引值,相当于for循环中的索引i                                                                                       

separator 表示每次迭代过程中以什么符号作为分隔符                                                                                                   

open 表示该语句以什么开始     

close 表示该语句以什么结束 

<!-- 批量插入图书信息-->
<insert id="insertBooks" parameterType="java.util.List">
	insert into t_ts_book(book_id,book_type,book_num,book_name,book_auth,book_pub,book_pub_num,
	book_desc,book_had,book_img,book_status)
	VALUES
		<foreach collection="books" item="book" index="index" separator="," open="(" close=")">
			#{book.bookId},#{book.bookType},#{book.bookNum},#{book.bookName},#{book.bookAuth},#{book.bookPub},
			#{book.bookPubNum},#{book.bookDesc},#{book.bookHad},#{book.bookImg},#{book.bookStatus}
		</foreach>
</insert>

mysql中日期操作

日期格式化:

DATE_FORMAT(date,format),date 参数是合法的日期。format 规定日期/时间的输出格式

DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。常见的数据格式如下                                                     

%Y 年,四位
%y 年,两位
%M  月,月名 
%m 月,数值(00-12)
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%H 小时 (00-23)
%h 小时 (01-12)
%I 小时 (01-12)
%i 分钟,数值(00-59)
%S 秒(00-59)
%s 秒(00-59)

查询指定时间(格式指定要求)的数据

select * from t_order where date_format(eattime,'%Y%m')='201806'   

日期计算:                                                                                                                                                  DATE_ADD(date,INTERVAL expr type),DATE_ADD() 函数向日期添加指定的时间间隔。通过BETWEEN可实现用于查询指定时间区间的数据

<!-- 公众号消息推送(移动端) -->
<select id="sendMsgReturnBook" resultMap="BorrowResultMap">
	select * from t_ts_borrow where borrow_status not in(4,5) and  end_time
    between date_add(date_format(now(),'%y-%m-%d 00:00:00'),interval 1 day) and date_add(date_format(now(),'%y-%m-%d 23:59:59'),interval 1 day)
</select>

获取今天日期范围now()

select date_format(now(),'%y-%m-%d 00:00:00'), date_format(now(),'%y-%m-%d 23:59:59')

猜你喜欢

转载自blog.csdn.net/zhang_jiayuan/article/details/81202900