MyBatis中的注解开发

MyBatis中的注解开发

注解的使用

MyBatis种如果使用注解开发, 则不需要编写Mapper.xml配置文件

并且在SqlMapConfig.xml<mappers>标签中的标签需使用<package><mapper class="">, 不能使用<mapper resource="">配置(指定配置文件配置)

在使用注解时, Mapper.xml配置文件不能同时存在, 否则报错

CRUD注解

MyBatis中的CRUD注解有4种: @Select , @Insert , @Update , @Delete

  1. @Select注解, 查询操作

    /**
    * 查询所有用户
    * @return 用户列表
    */
    @Select("SELECT * FROM user")
    List<User> findAll();
    
  2. @Insert注解, 插入操作

    /**
    * 保存用户
    * @param user 用户信息
    */
    @Insert("INSERT INTO user(username, address, sex, birthday) VALUES (#{username}, #{address}, #{sex}, #{birthday})")
    void save(User user);
    
  3. @Update注解, 更新操作

    /**
    * 更新用户
    * @param user 用户信息
    */
    @Update("UPDATE user SET username=#{username} WHERE id=#{id}")
    void updateUser(User user);
    
  4. @Delete注解, 删除操作

    /**
    * 删除用户
    * @param id 用户id
    */
    @Delete("DELETE FROM user WHERE id=#{id}")
    void delete(Integer id);
    

@Results注解

解决实体类与数据表中字段名不一致的问题, 相当于Mapper.xml配置文件中的<ResultMap>标签功能

id : 指定@Results的id, 可用于重复使用注解信息

value : 其中包含一个 @Result 数组

@Resultid 指定是否为主键, column 指定表中字段名, property 指定实体类属性名

/**
* 查询所有用户
* @return 用户列表
*/
@Select("SELECT * FROM user")
@Results(id = "userMap", value = {
    @Result(id = true, column = "id", property = "uId"),
    @Result(column = "username", property = "uName"),
    @Result(column = "address", property = "uAddress"),
    @Result(column = "sex", property = "uSex"),
    @Result(column = "birthday", property = "uBirthday")
})
List<User> findAll();

通过@Results中的id属性值重复使用注解

@ResultMap 中的 value 属性为一个 String数组

可通过value 指定 多个@Resultid 值来使用注解

/**
* 通过id查询用户
* @param id 用户id
* @return 用户信息
*/
@Select("SELECT * FROM user WHERE id=#{id}")
@ResultMap("userMap")
User findById(Integer id);

对一

@Result注解中 one属性值指定 对方实体的封装

@One注解

  1. select : 指定对方查询的唯一标识方法
  2. fetchType: 指定加载类型(延迟/ 立即)
/**
* 查询所有账户信息, 并包含用户
* @return 账户信息列表
*/
@Select("SELECT * FROM account")
@Results(id = "accountMap", value = {
    @Result(id = true, column = "id", property = "id"),
    @Result(column = "uid", property = "uid"),
    @Result(column = "money", property = "money"),
    @Result(property = "user", column = "uid", one = @One(select = "com.study.mybatis.dao.IUserDao.findById", fetchType = FetchType.EAGER))
})
List<Account> findAll();

对多

many属性

@Many注解:

  1. select: 指定查询方法
  2. fetchType: 指定加载类型(延迟/ 立即)
/**
 * 查询所有用户
 * @return 用户列表
 */
@Select("SELECT * FROM user")
@Results(id = "userMap", value = {
        @Result(id = true, column = "id", property = "uId"),
        @Result(column = "username", property = "uName"),
        @Result(column = "address", property = "uAddress"),
        @Result(column = "sex", property = "uSex"),
        @Result(column = "birthday", property = "uBirthday"),
        @Result(property = "accounts", column = "id", many = @Many(select = "com.study.mybatis.dao.IAccountDao.findByUid", fetchType = FetchType.LAZY))
})
List<User> findAll();

缓存配置

开启二级缓存

Dao中加入注解 @@CacheNamespace , 设置blocking = true

@CacheNamespace(blocking = true)
public interface IUserDao {
	...
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值