本文主要是介绍Mybatis 从入门到精通二: mybatis的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Mybatis 从入门到精通二: mybatis的使用
目录
- 1、like 的使用范例
- 2、时间的比较的范例
- 3、if…else…表示方法
- 4、insert后返回自增字段的值
- 5、获取当前时间
- 6、字符串拼接
1、like 的使用范例
name like CONCAT(CONCAT('%', #{name}), '%')
2、时间的比较的范例
<![CDATA[
and bill_date >= #{begBillDate,jdbcType=TIMESTAMP}
]]>
3、if…else…表示方法
<choose>
<when test="">
//...
</when>
<otherwise>
//...
</otherwise>
</choose>
4、insert后返回自增字段的值
xml中加入后面的参数,useGeneratedKeys=“true” keyProperty=“id”。
注意,此处id 已经自动更新到传入的DO中,取DO的自增id即可。
范例:
方法1 :使用该种方法,需要数据已提交到数据库。对于未提交到数据库,只存在事务中的,是不能取到返回值的自增id的。
<insert id="addUser" parameterType="com.xxx.model.UserInfo" useGeneratedKeys="true" keyProperty="id">INSERT INTOuser_info(user_name, account, password)values(#{userName},#{account},#{password})
</insert>
方法2 :使用该种方法,对于暂时未提交数据库的,只存在事务中的,也可以取到自增的id。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TvojzDXB-1611714169591)(images/2360/1611711606064.png)]
<insert id="insertNoticeMessage" parameterType="BaseNoticeMessage"><selectKey order="AFTER" keyProperty="id" resultType="java.lang.Integer">SELECT LAST_INSERT_ID()</selectKey>INSERT INTO user_info(user_name)VALUES (#{userName})
</insert>
5、获取当前时间
SYSDATE()
6、字符串拼接
${value} ,注意此时只能填value ,填入其他的则不生效。
7、好书推荐
MyBatis从入门到精通.pdf:
http://notescloud.top/cloudSearch/detail?id=1077
原文链接:http://notescloud.top/cloudSearch/detail?id=2360
这篇关于Mybatis 从入门到精通二: mybatis的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!