Mapper XML
最基本的元素有 select insert update delete sql resultMap 等。
select
对应一个 INSERT 语句
1
2
3
<select id="selectPerson" parameterType="int" resultType="hashmap">
  SELECT * FROM PERSON WHERE ID = #{id}
</select>
#{id} 告诉Mybatis 使用 PreparedStatement 参数。
update
对应一个 UPDATE 语句
1
2
3
4
5
6
7
8
<update id="updateAuthor">
  update Author set
    username = #{username},
    password = #{password},
    email = #{email},
    bio = #{bio}
  where id = #{id}
</update>
delete
对应一个 DELETE 语句
1
2
3
<delete id="deleteAuthor">
  delete from Author where id = #{id}
</delete>
insert
对应一个 INSERT 语句
1
2
3
4
<insert id="insertAuthor">
  insert into Author (id,username,password,email,bio)
  values (#{id},#{username},#{password},#{email},#{bio})
</insert>
sql
对应一个可复用的SQL片段
1
2
3
<sql id="Base_Column_List">
    id, name, create_time, update_time
</sql>
使用 include 标签引用一个 sql 片段:
1
2
3
4
5
6
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=BIGINT}
</select>
resultMap
可以将SQL结果映射为对应的JavaBean。
这段xml使用 resultType="com.someapp.model.User" 将 SQL 结果映射为 User。这里Mybatis自动生成了一个ResultMap。
1
2
3
4
5
<select id="selectUsers" resultType="io.someapp.model.User">
  select id, username, hashedPassword
  from some_table
  where id = #{id}
</select>
SQL 的字段名和 JavaBean 的属性名往往不一致,这时候我们可以在 SQL 中使用 as 定义别名来使名称一致,但更好的选择是使用 ResultMap。
1
2
3
4
5
<resultMap id="userResultMap" type="User">
  <id property="id" column="user_id" />
  <result property="username" column="user_name"/>
  <result property="password" column="hashed_password"/>
</resultMap>
对应的SQL依然保持简洁。
1
2
3
4
5
<select id="selectUsers" resultMap="userResultMap">
  select user_id, user_name, hashed_password
  from some_table
  where id = #{id}
</select>
resultMap 还有许多高级的用法,详情可以参考官方文档。
Dynamic SQL
if
1
2
3
4
5
6
7
<select id="findActiveBlogWithTitleLike" resultType="Blog">
  SELECT * FROM BLOG
  WHERE state = ‘ACTIVE’
  <if test="title != null">
    AND title like #{title}
  </if>
</select>
choose, when, otherwise
Mybatis 中,if 不支持 else,这时我们可以使用choose,类似于Java中的switch语句。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="findActiveBlogLike" resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>
trim, where, set
foreach
bind
调试
Spring Boot
在 application.properties 中添加 logging.level.Mapper所在包名=debug 就可以显示在log中显示SQL语句及参数。
1
logging.level.io.bookmark.user.mapper=debug
一种可用的UnitTest方式
1
2
3
4
5
6
7
8
9
10
11
12
13
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApp.class)
public class UserMapperTest {
    @Autowired
    UserMapper userMapper;
    @Test
    public void selectByUserId() throws Exception {
        User user =  userMapper.selectByUserId("123");
        System.out.println(user);
        assertNotNull(user);
    }
}
另外还可以使用 MyBatis-Spring-Boot-Starter-Test,会自动回滚数据。