1. @Component @Controller @Service @Repository
@Component
通用的Spring容器管理的单例bean组件
@Controller
作用于表现层(Spring-mvc的注解)
@Service
作用于业务逻辑层
@Repository
作用于持久层。若数据库操作中抛出异常,就能对其处理,转而抛出翻译之后的spring专属数据库异常,方便排查处理
2. @Many @One
@Many
一对多关联查询
@Results(id="hMap",value = {
@Result(property = "id", column = "id"),
@Result(property = "record", column = "id",
many = @Many(select="com.aaa.mapper.aaaMapper.getRecord",fetchType= FetchType.LAZY))
})
@Select("select * from aaa where id = #{id}")
public WorkflowHistory getWorkflowHistory(@Param("id")String id);
@ResultMap("bbbMap")
@Select("select * from bbb where h_id = #{hid} order by create_time desc")
public List<Record> getRecord(@Param("hid")String hid);
@One
一对一关联查询
@Results(id="hMap",value = {
@Result(property = "id", column = "id"),
@Result(property = "record", column = "id",
one = @One(select="com.aaa.mapper.aaaMapper.getRecord",fetchType= FetchType.EAGER))
})
@Select("select * from aaa where id = #{id}")
public WorkflowHistory getWorkflowHistory(@Param("id")String id);
@ResultMap("bbbMap")
@Select("select * from bbb where h_id = #{hid} order by create_time desc")
public Record getRecord(@Param("hid")String hid);
3. @Transactional
注意
- 场景1:方法A和方法B在同一个对象中,在方法A中调用方法B,
方法A:无@Transactional注解,
方法B:有@Transactional注解,
则方法B中的事务失效。 - 场景2:方法A和方法B在不同的对象中,在方法A中调用方法B,
方法A:无@Transactional注解,
方法B:有@Transactional注解,
则方法B中的事务有效。