LoginSignup
40
37

More than 5 years have passed since last update.

MyBatis備忘録

Last updated at Posted at 2018-01-09

MyBatisの備忘録を記載していく。

IF文
<if test="(author != null and author.name != '') or authorList.size() > 0">
</if>
IN句
column in
<foreach item="sample" open="(" close=")" collection="sampleList" separator=",">
  #{sample}
</foreach>
不等号記号「<」を使う場合
<![CDATA[
  column <= #{parameter}
]]>
パラメータをDATE型としてバインドする場合
column = #{parameter,jdbcType=DATE}
include
<sql id="sample">
  column = True
<sql>

<select id="xxxxx" resultType="string">
  select column from table where <include refid="sample" />
</select>
resultType

結果がListの場合、resultTypeはListの中身の型を指定する。
List<String>型で取得したい場合は、resultType="string"と記述する。

Bulk Insert
<insert id="xxxxx" parameterType="java.util.List">
  insert into table(column, ・・・) values
  <foreach collection="list" item="item" separator=",">
  ( #{item.propertyName}, #{item.propertyName}, ・・・ )
  </foreach>
</insert> 
Bulk Update
<update id="xxxxx" parameterType="java.util.List">
  <foreach collection="list" item="item" separator=";">
  update table set column = #{item.propertyName} where column = #{item.propertyName}
  </foreach>
</update> 
jdbcType
jdbcType java
VARCHAR String
INTEGER int
NUMERIC BigDecimal
BIT boolean
DATE Date
TIMESTAMP Date
エイリアス

"java.lang.String"を"string"と表記できる。
http://www.mybatis.org/mybatis-3/ja/configuration.html#typeAliases

javaのMapperインタフェースのパラメータの指定の仕方
void select(
        @Param("param1") String param1,
        @Param("param2") List<String> param2);

Mapper XMLの方は、parameterType="map"と記述する。

検索条件(Criteria)の作り方
TableExample example = new TableExample();
example.createCriteria()
        .andColumnEqualTo(value)
        .andColumnEqualTo(value);

example.setOrderByClause("column DESC, column ASC");
40
37
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
37