mybatis的使用

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 INTO
    user_info(user_name, account, password)
    values
    (#{userName},#{account},#{password})
</insert>

 方法2 :使用该种方法,对于暂时未提交数据库的,只存在事务中的,也可以取到自增的id。

<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 ,填入其他的则不生效。



posted @ 2017-09-12 13:55  panda521  阅读(177)  评论(0编辑  收藏  举报