MyBatis基于注解的使用
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();