The true power of MyBatis is in the Mapped Statements.

select

The select statement is one of the most popular elements that you'll use in MyBatis.

<select >
  SELECT * FROM PERSON WHERE ID = #{id}
</select>

insert, update and delete

The data modification statements insert, update and delete are very similar in their implementation.

<insert >
  insert into Author (id,username,password,email,bio)
  values (#{id},#{username},#{password},#{email},#{bio})
</insert>

sql

This element can be used to define a reusable fragment of SQL code that can be included in other statements.

<sql > ${alias}.id,${alias}.username,${alias}.password </sql>

The SQL fragment can then be included in another statement, for example:

<select >
  select
    <include ref/></include>,
    <include ref/></include>
  from some_table t1
    cross join some_table t2
</select>

Parameters

Using the #{} syntax will cause MyBatis to generate PreparedStatement properties. Using the ${} syntax will cause MyBatis to directly inject an unmodified string into the SQL Statement.

<select >
  select id, username, password
  from users
  where id = #{id}
</select>

Result Maps

The resultMap element is the most important and powerful element in MyBatis.

<select >
  select id, username, hashedPassword
  from some_table
  where id = #{id}
</select>
<resultMap >
  <id property="id" column="user_id" />
  <result property="username" column="user_name"/>
  <result property="password" column="hashed_password"/>
</resultMap>

<select >
  select user_id, user_name, hashed_password
  from some_table
  where id = #{id}
</select>

Advanced Result Maps

<resultMap >
  <constructor>
    <idArg column="blog_id" javaType="int"/>
  </constructor>
  <result property="title" column="blog_title"/>
  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
    <result property="favouriteSection" column="author_favourite_section"/>
  </association>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <association property="author" javaType="Author"/>
    <collection property="comments" ofType="Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType="Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="DraftPost"/>
    </discriminator>
  </collection>
</resultMap>

Auto-mapping

Auto-mapping works even when there is an specific result map. In the following sample id and userName columns will be auto-mapped and hashed_password column will be mapped.

<select >
  select
    user_id             as "id",
    user_name           as "userName",
    hashed_password
  from some_table
  where id = #{id}
</select>

<resultMap >
  <result property="password" column="hashed_password"/>
</resultMap>

cache

MyBatis includes a powerful transactional query caching feature which is very configurable and customizable.

By default, just local session caching is enabled that is used solely to cache data for the duration of a session. To enable a global second level of caching you simply need to add one line to your SQL Mapping file.

<cache
  eviction="FIFO"
  flushInterval="60000"
  size="512"
  readOnly="true"/>

Using a Custom Cache

In addition to customizing the cache in these ways, you can also completely override the cache behavior by implementing your own cache, or creating an adapter to other 3rd party caching solutions.

<cache type="com.domain.something.MyCustomCache">
  <property name="cacheFile" value="/tmp/my-custom-cache.tmp"/>
</cache>

cache-ref

when you want to share the same cache configuration and instance between namespaces. In such cases you can reference another cache by using the cache-ref element.

<cache-ref namespace="com.someone.application.data.SomeMapper"/>

相关文章:

  • 2021-08-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2018-01-21
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-18
  • 2021-09-10
  • 2021-10-09
  • 2022-01-19
相关资源
相似解决方案