版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
mybatis学习笔记(4)---- 注解开发
开发方式
- 使用xml配置,把sql和映射写在xml文件中。
- 使用注解配置,把sql和映射写在Mapper接口中。
两者都各有优缺点,虽然注解使用起来相对方便一些,但开发中sql往往很长,这样的话,在注解中就写起来就显得很恶心。
步骤
- 在核心配置文件中使用class,映射到相应的mapper接口,因为此时已经不需要或者说没有xml文件了。
<mappers>
<mapper class="com.github.excellent.UserMaper"/>
</mappers>
- 在方法上添加注解
常用注解
- @Insert("") ;双引号中写sql语句,用于插入操作使用。
- @Options(useGeneratedKeys = true,keyProperty = “id”) 获取自增长主键的值
- @Delete("delete from user where username = ‘${username}’ ") :删除
- @Select(“select * from user”) :查询
- @Update(“update user set username = #{username} where id = #{id}”):修改
- @Results(id=“Basemap”,value = {
@Result(column = “u_id”,property = “id”),
@Result(column = “u_name”,property = “name”)
})
解决名称不一致问题。