工作中用到的mybatis 用法小结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/suifeng2018/article/details/79455100
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;

猜你喜欢

转载自blog.csdn.net/suifeng2018/article/details/79455100