天天看點

十六、Mybatis的注解開發

文章目錄

  • ​​MyBatis的常用注解​​
  • ​​MyBatis的增删改查​​
  • ​​MyBatis的注解實作複雜映射開發​​
  • ​​一對一查詢​​
  • ​​1. 一對一查詢的模型​​
  • ​​2. 一對一查詢的語句​​
  • ​​3. 建立Order和User實體​​
  • ​​4. 建立OrderMapper接口​​
  • ​​5. 使用注解配置Mapper​​
  • ​​6. 測試結果​​
  • ​​一對多查詢​​
  • ​​1. 一對多查詢的模型​​
  • ​​2. 一對多查詢的語句​​
  • ​​3. 修改User實體​​
  • ​​4. 建立UserMapper接口​​
  • ​​5. 使用注解配置Mapper​​
  • ​​6. 測試結果​​
  • ​​多對多查詢​​
  • ​​1. 多對多查詢的模型​​
  • ​​2. 多對多查詢的語句​​
  • ​​3. 建立Role實體,修改User實體​​
  • ​​4. 添加UserMapper接口方法​​
  • ​​5. 使用注解配置Mapper​​
  • ​​6. 測試結果​​

MyBatis的常用注解

這幾年來注解開發越來越流行,Mybatis也可以使用注解開發方式,這樣我們就可以減少編寫Mapper映射檔案了。

@Insert:實作新增

@Update:實作更新

@Delete:實作删除

@Select:實作查詢

@Result:實作結果集封裝

@Results:可以與@Result 一起使用,封裝多個結果集

@One:實作一對一結果集封裝

@Many:實作一對多結果集封裝

MyBatis的增删改查

簡單的 user 表的增删改查的操作

@Insert("insert into user2 values(#{id},#{username},#{password},#{birthday})")
public void save(User user);

@Select("select * from user2 where id=#{id}")
public User findById(int id);

@Select("select * from user2")
public List<User> findAll();

@Update("update user2 set usernme=${username}, password=#{password} where id=#{id}")
public void update(User user);

@Delete("delete from user2 where id=#{id}")
public void delete(int id);      
十六、Mybatis的注解開發
十六、Mybatis的注解開發
十六、Mybatis的注解開發
十六、Mybatis的注解開發

修改MyBatis的核心配置檔案,我們使用了注解替代的映射檔案,是以我們隻需要加載使用了注解的Mapper接口即可

十六、Mybatis的注解開發

或者指定掃描包含映射關系的接口所在的包也可以

十六、Mybatis的注解開發

MyBatis的注解實作複雜映射開發

實作複雜關系映射之前我們可以在映射檔案中通過配置<resultMap>來實作,使用注解開發後,我們可以使用@Results注解,@Result注解,@One注解,@Many注解組合完成複雜關系的配置

十六、Mybatis的注解開發
十六、Mybatis的注解開發

一對一查詢

1. 一對一查詢的模型

使用者表和訂單表的關系為,一個使用者有多個訂單,一個訂單隻從屬于一個使用者一對一查詢的需求:查詢一個訂單,與此同時查詢出該訂單所屬的使用者

十六、Mybatis的注解開發

2. 一對一查詢的語句

對應的sql語句:

select * from orders;

select * from user where id=查詢出訂單的uid;

查詢的結果如下:

十六、Mybatis的注解開發

3. 建立Order和User實體

十六、Mybatis的注解開發

4. 建立OrderMapper接口

十六、Mybatis的注解開發

5. 使用注解配置Mapper

十六、Mybatis的注解開發

​​

​OrderMapper.java​

@Select("select * from orders")
@Results({
        @Result(column = "id", property = "id"),
        @Result(column = "ordertime", property = "ordertime"),
        @Result(column = "total", property = "total"),
        @Result(
                property = "user", // 要封裝的屬性名稱
                column = "uid", // 根據那個字段取查詢user表的資料
                javaType = User.class, // 要封裝的實體類型
                // select屬性 代表查詢那個接口的方法獲得資料
                one = @One(select = "com.lzjtu.mapper.UserMapper.findById")
        )
})
public List<Order> findAll();      

​UserMapper.java​

@Select("select * from user2 where id=#{id}")
public User findById(int id);      

​寫法二​

@Select("select *,o.id oid from orders o, user2 u where o.uid=u.id")
@Results({
        @Result(column = "oid", property = "id"),
        @Result(column = "ordertime", property = "ordertime"),
        @Result(column = "total", property = "total"),
        @Result(column = "uid", property = "user.id"),
        @Result(column = "username", property = "user.username"),
        @Result(column = "password", property = "user.password")
})
public List<Order> findAll();      

6. 測試結果

十六、Mybatis的注解開發
十六、Mybatis的注解開發

一對多查詢

1. 一對多查詢的模型

使用者表和訂單表的關系為,一個使用者有多個訂單,一個訂單隻從屬于一個使用者一對多查詢的需求:查詢一個使用者,與此同時查詢出該使用者具有的訂單

十六、Mybatis的注解開發

2. 一對多查詢的語句

對應的sql語句:

select * from user;

select * from orders where uid=查詢出使用者的id;

查詢的結果如下:

十六、Mybatis的注解開發

3. 修改User實體

十六、Mybatis的注解開發

4. 建立UserMapper接口

十六、Mybatis的注解開發

5. 使用注解配置Mapper

十六、Mybatis的注解開發

​UserMapper.java​

@Select("select * from user2")
@Results({
        @Result(id=true, column = "id", property = "id"),
        @Result(column = "username", property = "username"),
        @Result(column = "password", property = "password"),
        @Result(
                property = "orderList",
                column = "id",
                javaType = List.class,
                many = @Many(select = "com.lzjtu.mapper.OrderMapper.findByUid")
        )

})
public List<User> findUserAndOrderAll();      

​OrderMapper.java​

@Select("select * from orders where id=#{uid}")
public List<Order> findByUid(int uid);      

6. 測試結果

十六、Mybatis的注解開發
十六、Mybatis的注解開發

多對多查詢

跟一對多寫法差不多(除了查詢語句)

1. 多對多查詢的模型

使用者表和角色表的關系為,一個使用者有多個角色,一個角色被多個使用者使用

多對多查詢的需求:查詢使用者同時查詢出該使用者的所有角色

十六、Mybatis的注解開發

2. 多對多查詢的語句

對應的sql語句:

select * from user;

select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=使用者的id

查詢的結果如下:

十六、Mybatis的注解開發

3. 建立Role實體,修改User實體

十六、Mybatis的注解開發

4. 添加UserMapper接口方法

十六、Mybatis的注解開發

5. 使用注解配置Mapper

十六、Mybatis的注解開發

​UserMapper.java​

@Select("select * from user2")
@Results({
         @Result(id = true, column = "id", property = "id"),
         @Result(column = "username", property = "username"),
         @Result(column = "password", property = "password"),
         @Result(
                 property = "roleList",
                 column = "id",
                 javaType = List.class,
                 many = @Many(select = "com.lzjtu.mapper.RoleMapper.findByUid")
         )
 })
public List<User> findUserAndRoleAll();      
@Select("select * from sys_user_role ur,sys_role r where ur.roleId=r.id and ur.userId=#{id}")
public List<Role> findByUid(int id);      

6. 測試結果