MyBatis注解实现CRUD 操作(第五天)
(一) 常见注解简介
* @Insert:实现新增,代替了<insert></insert>
* @Update:实现更新,代替了<update></update>
* @Delete:实现删除,代替了<delete></delete>
* @Select:实现查询,代替了<select></select>
* @Result:实现结果集封装,代替了<result></result>
* @Results:可以与@Result 一起使用,封装多个结果集,代替了<resultMap></resultMap>
* @One:实现一对一结果集封装,代替了<association></association>
* @Many:实现一对多结果集封装,代替了<collection></collection>
(二) 注解的单表操作
1、创建注解的接口CustomerMapper
public interface UserMapper {
@Select("SELECT * FROM `customer`")
public List<Customer> findAll();
@Insert("INSERT INTO `customer`(name) VALUES(#{name}")
public void insert(Customer customer);
@Update("UPDATE `customer` SET name= #{name} WHERE id = #{id}")
public void update(Customer customer);
@Delete("DELETE FROM `customer` where id = #{id}")
public void delete(Integer id);
}
2、编写核心配置文件
<!--我们使用了注解替代的映射文件,所以我们只需要加载使用了注解的Mapper接口即可--> <mappers>
<!--扫描使用注解的Mapper类-->
<mapper class="com.offcn.mapper.CustomerMapper"></mapper>
</mappers>
<!--或者指定扫描包含映射关系的接口所在的包也可以-->
<mappers>
<!--扫描使用注解的Mapper类所在的包-->
<package name="com.offcn.mapper"></package>
</mappers>
3、测试代码
@Test
public void testFindAll() throws Exception {
CustomerMapper customerMapper= sqlSession.getMapper(CustomerMapper.class);
List<Customer> list = customerMapper.findAll();
for (Customer customer : list) {
System.out.println(customer);
}
}
// 添加
@Test
public void testInsert() throws Exception {
CustomerMapper customerMapper= sqlSession.getMapper(CustomerMapper.class);
Customer customer= new Customer();
customer.setName("赵云");
customerMapper.insert(customer);
}
// 更新
@Test
public void testUpdate() throws Exception {
CustomerMapper customerMapper= sqlSession.getMapper(CustomerMapper.class);
Customer customer= new Customer();
customer.setName("刘备");
Customer.setId(1);
customerMapper.update(customer);
}
// 删除
@Test
public void testDelete() throws Exception {
CustomerMapper customerMapper= sqlSession.getMapper(CustomerMapper.class);
customerMapper.delete(1);
}
}
(三) 注解实现一对一
需求:查询一个订单,与此同时查询出该订单所属的用户
1、创建OrdersMapper接口
public interface OrderMapper {
@Select("SELECT * FROM orders")
@Results({
@Result(id = true, column = "oid", property = "oid"),
@Result(column = "dis", property = "dis"),
@Result(property = "customer", javaType = Customer.class, column = "cus_fk",
one=@One(select="com.offcn.mapper.CustomerMapper.findById",fetchType=FetchType.EAGER)) })
public List<Order> findAllWithCustomer();
}
2、CustomerMapper接口
public interface UserMapper {
@Select("SELECT * FROM `customer` WHERE cid = #{cid}")
public Customer findById(Integer id);
}
3、测试代码
@Test
public void testOrderWithUser() throws Exception {
OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
List<Orders> list = orderMapper.findAllWithCustomer();
for (Order order : list) {
System.out.println(order);
}
}
(四) 注解实现一对多
需求:查询一个用户,与此同时查询出该用户具有的订单
1、创建CustomerMapper接口
public interface UserMapper {
@Select("SELECT * FROM `customer`")
@Results({
@Result(id = true, column = "cid", property = "cid"),
@Result(column = "name", property = "name"),
@Result(property = "orderList", javaType = List.class, column = "cid" ,
many = @Many(select = "com.offcn.mapper.OrdersMapper.findByCid")) })
public List<Customer> findAllWithOrders();
}
2、创建OrdersMapper接口
public interface OrdersMapper {
@Select("SELECT * FROM orders WHERE cus_fk = #{cid}")
public List<Orders> findByUid(Integer cid);
}
3、测试代码
@Test
public void testUserWithOrder() throws Exception {
CustomerMapper customerMapper= sqlSession.getMapper(CustomerMapper.class);
List<Customer> list = customerMapper.findAllWithOrders();
for (Customer customer: list) {
System.out.println(customer);
}
}