MyBatis 教程

MyBatis 注解简单使用

本章节将介绍怎样简单的使用 MyBatis 注解,示例如下:

(1)使用 @Select、@Results 和 @Result 三个注解实现 select 查询,以及将查询结果进行结果映射。如下:

@Select("SELECT user_id, name, sex, age FROM user")
@Results({
   @Result(id=true, column="user_id", property="userId"),
   @Result(column="name", property="name"),
   @Result(column="sex", property="sex"),
   @Result(column="age", property="age")
})
List<UserBean> demo();

(2)使用 @ResultMap 注解引用 Mapper XML 文件中 <resultMap> 定义的映射。如下:

/**
 * 引用 BookMapper.xml 中名为 userMap 的结果映射
 * @return
 */
@Select("SELECT user_id, name, sex, age FROM user")
@ResultMap("userMap")
List<UserBean> demo();

Mapper XML 文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hxstrive.mybatis.annotation.demo2.UserMapper">
   <!-- 结果映射,和其他方法共用 -->
   <resultMap id="userMap" type="com.hxstrive.mybatis.annotation.demo2.UserBean" >
      <id column="user_id" jdbcType="INTEGER" property="userId" />
      <result column="name" jdbcType="VARCHAR" property="name" />
      <result column="sex" jdbcType="VARCHAR" property="sex" />
      <result column="age" jdbcType="INTEGER" property="age" />
   </resultMap>
</mapper>

(3)使用 @One 注解实现一对一关联映射。如下:

@Select("SELECT book_id, book_name, price, content FROM book")
@Results({
   @Result(id=true, column="book_id", property="bookId"),
   @Result(column="book_name", property="bookName"),
   @Result(column="price", property="price"),
   @Result(column="content", property="content"),
   @Result(column="book_id", property="bookExt",
      one=@One(select="getExtBookExtById")
   )
})
List<BookBean> demo();

@Select("select book_id, book_summary, book_content " +
      "from book_ext where book_id=#{bookId}")
@Results({
   @Result(id=true, column="book_id", property="bookId"),
   @Result(column="book_summary", property="bookSummary"),
   @Result(column="book_content", property="bookContent")
})
BookExtBean getExtBookExtById(Integer bookId);

上面实例中,在 demo() 方法中,使用 @Result 注解的 one 属性配合 @One 注解实现关联映射。其中,@One 注解通过 select 属性指定了 getExtBookExtById 方法,当每条数据映射时,将会调用 

getExtBookExtById 方法。

(4)使用 @Many 注解实现一对多关联关系映射。如下:

@Select("SELECT `user_id`, `name`, `sex`, `age` FROM `user`")
@Results({
        @Result(id=true, column="user_id", property="userId"),
        @Result(column="name", property="name"),
        @Result(column="sex", property="sex"),
        @Result(column="age", property="age"),
        @Result(column="user_id", property="userContactList",
                many=@Many(select="getUserContactByUserId")
        )
})
List<UserBean> getUserList();


@Select("SELECT `id`, `user_id`, `usage`, `number` FROM `user_contact` "
        + "where user_id=#{userId}")
@Results({
        @Result(id=true, column="id", property="id"),
        @Result(column="user_id", property="userId"),
        @Result(column="usage", property="usage"),
        @Result(column="number", property="number")
})
List<UserContactBean> getUserContactByUserId(Integer userId);

上面示例中,通过 @Result 注解的 many 属性配合 @Many 注解实现一对多映射。其中,@Many 注解通过 select 属性指定映射方法 getUserContactByUserId。注意:getUserContactByUserId 映射方法再本 Mapper 中进行了定义。

(5)使用 @Insert 注解实现 insert 语句,插入数据。如下:

a、通过传递普通参数插入数据

@Insert("INSERT INTO book(book_name, price, content) VALUE(" +
        "#{bookName}, #{price}, #{content})")
void insertBook(@Param("bookName") String bookName,
                @Param("price") Float price, @Param("content") String content);

b、通过传递 JavaBean 插入数据

@Insert("INSERT INTO book(book_name, price, content) VALUE(" +
        "#{bookName}, #{price}, #{content})")
void insertBook(BookBean book);

c、通过 @SelectKey 注解获取刚刚插入数据的 ID

@Insert("INSERT INTO book(book_name, price, content) VALUES("
        + "#{book.bookName}, #{book.price}, #{book.content})")
@SelectKey(statement="SELECT last_insert_id()",
        keyProperty="book.bookId", resultType=Integer.class, before=false)
int insertBook(@Param("book") BookBean book);

(6)使用 @Update 注解实现 update 语句,修改数据。如下:

@Update("UPDATE book SET book_name=#{bookName}, price=#{price}, content=#{content} "
        + " WHERE book_id=#{bookId}")
void updateBook(@Param("bookId") Integer bookId, @Param("bookName") String bookName,
                @Param("price") Float price, @Param("content") String content);

(7)使用 @Delete 注解实现 delete 语句,删除数据。如下:

@Delete("DELETE FROM book WHERE book_id=#{bookId}")
void deleteBook(@Param("bookId") Integer bookId);
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号