MyBatis(十七)——MyBatis注解

  1. 为了简化配置文件。
  2. Mybatis的注解简化mapper.xml文件。
  3. 如果涉及动态SQL仍然使用mapper.xml文件。
  4. mapper.xml和注解可以共存。

配置方式:
(1)mybatis.xml中<mappers>两种配置方式

配置方式一:

  	<mappers>
  		<package name="com.ouc.mapper"/>
  	</mappers>

配置方式二:

  	<mappers>
  		<mapper class="com.ouc.mapper.TeacherMapper"/>
  		<mapper class="com.ouc.mapper.StudentMapper"/>
  	</mappers>

(2)实现查询

	@Select("select * from teacher")
	List<Teacher> selAll();

(3)实现新增

	@Insert("insert into teacher values(default,#{tname})")
	int insTeacher(Teacher teacher);

(4)实现修改

	@Update("update teacher set tname=#{tname} where tid=#{tid}")
	int updTeacher(Teacher teacher);

(5)实现删除

	@Delete("delete from teacher where tid=#{0}")
	int delById(int tid);

(6)使用注解实现<resultMap>功能(以N+1举例)

①在StudentMapper接口中添加查询

public interface StudentMapper {
	@Select("select * from student where tid = #{0}")
	List<Student> selByTid(int tid);
}

②在TeacherMapper接口中添加

1 @Results() 相当于<resultMap>
2 @Result() 相当于<id/><result/>
@Result(id=true) 相当与<id/>
3 @Many() 相当于<collection/>
4 @One() 相当于<association/>

	@Results(value={
			@Result(id=true,property="tid",column="tid"),
			@Result(property="tname",column="tname"),
			@Result(property="list",column="tid",many=@Many(select="com.ouc.mapper.StudentMapper.selByTid"))
	})
	@Select("select * from teacher")
	List<Teacher> selAll();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值