mybatis 註解開發,概述
常用的註解
@Insert
@Update
@Delete
@Select
@Result : 實現結果集封裝
@Results : 可以與@Result一起使用,封裝多個結果集
@One : 實現一對一結果集封裝
@Many : 實現一對多結果集封裝
演示簡單的CRUD操作
@Insert("insert into user values(#{id},#{username},#{password},#{birthday})") public void save (User user)
@Select("select * from user") public List<User> findAll()
實現複雜查詢
@Results 代替的是標籤,<resultMap>該註解中可以使用單個@Result註解,也可以使用@Result集合。使用格式:@Results({@Result(),@Result()})或者@Results(@Result())
@Result 代替<id>標籤和<result>標籤。@Result中屬性介紹:column:資料庫中的列名,property:需要裝配的屬性名,one:需要使用的@One註解(@Result (one=@One)),many;需要使用的@Many註解(@Result (many=@Many()))。
@One (一對一) 代替<assocation>標籤,是多表查詢的關鍵,(對方查詢時使用的介面方法) ,在註解中用來指定子查詢返回單一物件。@One註解屬性介紹:select:指定用來多表查詢的sqlmapper,使用格式:@Result(column=“” ,property=“”,one=@One(select=“”))
@Many (多對一)代替<collection>標籤,是多表查詢的關鍵,在註解中用來指定子查詢返回物件集合。使用格式:@Result(property=“”,column=“”,many=@Many(select=""))
舉一個例子
一對一:
查訂單對應的使用者
// 這裡有一個id的重複,用oid,實現了冗餘列名,這種封裝和xml的方式一樣 @Select("select *,o.id oid from orders o,user 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();
// 分開查,查2次 @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,// 要封裝的實體型別 one = @One(select="cn.taotao.mapper.UserMapper.findById") 查詢哪個介面的方法獲得資料 ) })
publc List<Order> findAll()
接上面的介面查詢
@Select(“select * from user where id=#{id}”) public User findById(Integer id);
一對多:
查使用者的所有訂單
在User類中,建立一個屬性 ,建立對映關係,該使用者的所有訂單
private List<Order> orderList; 在@Result中需要引入這個屬性
// 在 UserMapper中 @Select("select * from user") @Results({ @Result(id=true, column= "id", property="id"), @Result(column= "username", property="username"), @Result(column= "password", property="password"), @Result( @Result( property="orderList",column="id" , // 當前的id,作為對方的uid查詢傳入 ,返回型別為List javaType=List.class,
many=@Many(select=“cn.taotao.mapper.OrderMapper.findbyUid”) ) ), }) public List<User> findUserAndOrderAll(); // 在 OrderMapper中 @Select(“select * from orders where uid=#{uid}”) public List<Order> findByUid(int uid);
多對多
(關鍵: 從user中建立對映關係,然後從中間表(user_role)查詢結果,這個查詢複雜,聯合查詢(user-role和role表),將結果封裝到user中的對映關係屬性中)
使用者角色表,一個使用者對應多個角色,一個角色有多個使用者。
多對多一般對應一箇中間表。這個例子用到了3張表,一個是user,一個role,一個是user_role(不需要建立mapper)
分別建立role,user的bean。
public class User { private int id; private String username; private Sting password; private Date birthday; private List<Role> roleList; //當前使用者擁有哪些角色,對映關係只需做一個即可,不用再role中建立, }
在UserMapper中
@Select(“select * from user”) @Results({ @Result(id=true,column="id",property="id"), @Result(column="username" ,property="username"), @Result(property="roleList",column="id", // 將id傳入查詢 javaType=List.class, many=@Many(select = "cn.taotao.mapper.RoleMapper.findByUid") // 對應下面的介面查詢 ) }) public List<User> findUserAndRoleAll();
建立RoleMapper,這個查詢是關鍵,關聯兩張表,根據uid查詢出所有的角色資訊
@Select("select * from user_role ur,role r where ur.roleId=r.id and ur.userId=#{uid}") public List<Role> findByUid(int uid)