工作中用到的mybatis 用法小结

1、返回insert的对象的主键id

在insert标签 添加 keyProperty 即可返回

<insert id="insert"  keyProperty="userId" parameterType="com.test.project1.User">  
</insert>
2、insertOrUpdate 插入或更新
3、mybatis 控制台打印sql语句及日志

在spring-mybatis.xml的configration节点下的settings节点下再加setting即可

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration   
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 打印查询语句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>

    <!-- mapper已经在spring-mybatis.xml中的sqlSessionFactory配置,这里不再需要配置 -->
<!--     <mappers> -->
<!--         <mapper resource="com/a/b/c/dao/BusinessInfoDaoMapper.xml" /> -->
<!--     </mappers> -->
</configuration>
4、根据某个字段insertOrUpdate
<insert id="saveOrUpdate" >
  <selectKey keyProperty="id" resultType="com.piaoniu.entity.Country" order="BEFORE">
    select count(id) as id from country where name = #{name}
  </selectKey>
  <if test="id == 1">
    update country 
    set countryname = #{countryname},countrycode = #{countrycode} 
    where name = #{name}
  </if>
  <if test="id == 0">
    insert into country values(#{name},#{countryname},#{countrycode})
  </if>
</insert>
5、对数学拼接的字符串’1,2,3,4,5’使用in
/* ids="1,2,3,4,5" 存储格式为字符串 */
 SELECT
        *
        from table_1
        where
          table_1.id in (${ids})
        </if>
6、大于等于,小于等于 等的字符转义表示
 <        <=      >       >=       &        '        "
&lt;    &lt;=   &gt;    &gt;=   &amp;   &apos;  &quot;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值