Mybtis注解
增加接口CategoryMapper ,並在接口中聲明的方法上,加上注解
增加對CategoryMapper 的映射:
<mappers>
//com.shop.mapper包下的CategoryMapper接口
<mapper class="com.shop.mapper.CategoryMapper"/>
</mappers>
以后mapper內每增加一個接口,需要在mappers標簽內增加對應的映射
或者直接映射包:(只要接口在該包下,就不用再寫映射了)
<mappers>
//com.shop.mapper包
<package name="com.shop.mapper"/>
</mappers>
基於注解的簡單增刪改查:
public interface CategoryMapper {
@Insert(" insert into category ( name ) values (#{name}) ")
public int add(Category category);
@Delete(" delete from category where id= #{id} ")
public void delete(int id);
@Update("update category set name=#{name} where id=#{id} ")
public int update(Category category);
@Select(" select * from category ")
public List<Category> list();
}
多個參數:例如分頁
需要在參數前加上注釋@Param("參數名")
@Select("select * from comment where cbid=#{cbid} limit #{begin},#{pageSize}")
public List<Comment> Listpage(@Param("pageSize") int pageSize,@Param("begin") int begin,@Param("cbid") int cbid);
注解方式:一對多
查詢所有Category,及Category下的Product
新增接口ProductMapper
注解@Select用於根據分類id獲取產品集合
public interface ProductMapper {
@Select(" select * from product where cid = #{cid}")
public List<Product> listByCategory(int cid);
}
@Select注解獲取Category類本身,@Results 通過@Result和@Many中調用ProductMapper.listByCategory()方法相結合,來獲取一對多關系
public interface CategoryMapper {
@Select(" select * from category ")
@Results({
//下句可以省略
@Result(property = "id", column = "id"),
//column = "id"對應Category的id的字段,select = "com.shop.mapper.ProductMapper.listByCategory")寫全包名+接口名+方法名
//一對多用many=@Many(),一對多需要添加屬性:javaType = List.class
@Result(property = "products", javaType = List.class, column = "id", many = @Many(select = "com.shop.mapper.ProductMapper.listByCategory") )
})
public List<Category> list();
}
注解方式:多對一
CategoryMapper接口,提供get方法
public interface CategoryMapper {
@Select(" select * from category where id = #{id}")
public Category get(int id);
}
ProductMapper接口,提供list方法
public interface ProductMapper {
@Select(" select * from product ")
@Results({
//column="cid"對應product表中的cid,多對一用one=@One()
@Result(property="category",column="cid",one=@One(select="com.how2java.mapper.CategoryMapper.get"))
})
public List<Product> list();
}
注解方式:多對多
訂單項與商品是多對一關系,訂單與訂單項是一對多關系
ProductMapper接口,提供 get方法:
public interface ProductMapper {
@Select("select * from product_ where id = #{id}")
public Product get(int id);
}
OrderItemMapper,提供listByOrder方法:
public interface OrderItemMapper {
@Select(" select * from order_item_ where oid = #{oid}")
@Results({
@Result(property="product",column="pid",one=@One(select="com.how2java.mapper.ProductMapper.get"))
})
public List<OrderItem> listByOrder(int oid);
}
OrderMapper,提供list方法
public interface OrderMapper {
@Select("select * from order_")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "orderItems", javaType = List.class, column = "id",
many = @Many(select = "com.how2java.mapper.OrderItemMapper.listByOrder"))
})
public List<Order> list();
}
注解方式:動態SQL
用script標簽包圍,然后像xml語法一樣書寫
例子:
@Select("<script>select * from comment <where>"
+ "<if test='cbid!=0'> and cbid=#{cbid}</if>"
+ "<if test='cuid!=0'> and cuid=#{cuid}</if>"
+ "</where></script>")
注解方式:返回insert語句的自增長id
例如:
@Insert("insert into discuss values(null,now(),#{dresult},#{event.eveid},1)")
@Options(useGeneratedKeys=true,keyProperty="did",keyColumn="did")
public int addDiscuss(Discuss dis);
返回的自增長id被儲存到參數Discuss對象中
int did = dis.getDid();