Mybatis学习04

Mybatis支持使用注解配置,但是Mybatis主配置文件不能用注解代替

使用注解后,Mybatis就不支持xml配置了,如果还存在对应目录下的xml配置文件,则会报错

基于注解配置的CRUD操作

在dao层接口方法的定义上添加@Select,@Insert,@Update,@Delete注解,其value属性的值为对应的sql语句

public interface UserDao {

    /**
     * 查询所有用户
     * @return
     */
    @Select("select * from users")
    List<User> findAll();

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Select("select * from users where id=#{id}")
    User findById(Integer id);

    /**
     * 保存用户
     * @param user
     */
    @Insert("insert into users(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")
    void saveUser(User user);

    /**
     * 更新用户
     * @param user
     */
    @Update("update users set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}")
    void updateUser(User user);

    /**
     * 删除用户
     * @param id
     */
    @Delete("delete from users where id=#{id}")
    void deleteUser(Integer id);

    /**
     * 根据用户名模糊查询
     * @return
     */
    @Select("select * from users where username like #{username}")
    List<User> findByName(String username);

    /**
     * 查询总用户数
     * @return
     */
    @Select("select count(*) from users")
    int findTotal();
}

基于注解配置的多表查询和延迟加载

使用的注解:@Results@Result@ResultMap@One@Many

@Results注解用于配置输出参数映射,其属性如下

  • id:映射规则的唯一标识,其他接口方法可以通过@ResultMap注解引用
  • value:在value里用@Result注解配置每一个字段的映射规则

@Result注解用于配置单个字段的映射规则,其属性如下

  • id:用来指定当前字段是否是主键,默认值是false
  • property:实体类属性名
  • colum:数据库字段名
  • one:使用@One注解引用其他实体类对象
  • many:使用@Many注解引用其他实体类对象集合

@ResultMap注解用于引用配置的输出参数映射规则,其属性如下

  • value:用来指定映射规则的id,是一个数组,当只引用一个时,value可以不写

@One@Many注解用于引用其他实体类对象或者对象集合,其属性如下

  • select:值为副查询方法的全限定类名.方法名
  • fetchType:加载模式,FetchType.LAZY表示延迟加载,FetchType.EAGER表示立即加载

例如

public interface UserDao {

    /**
     * 查询所有用户
     * @return
     */
    @Select("select * from users")
    @Results(id="userMap",value = {
            @Result(id=true,property = "userId",column = "id"),
            @Result(property = "userName",column = "username"),
            @Result(property = "userBirthday",column = "birthday"),
            @Result(property = "userSex",column = "sex"),
            @Result(property = "userAddress",column = "address"),
            @Result(property = "accounts",column = "id",many = @Many(select = "com.chenpeng.dao.AccountDao.findByUid",fetchType = FetchType.LAZY))
    })
    List<User> findAll();
    
    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @ResultMap("userMap")
    @Select("select * from users where id=#{id}")
    User findById(Integer id);
}
public interface AccountDao {

    /**
     * 查询账户,同时包含对应的用户
     * @return
     */
    @Select("select * from account")
    @Results(id="accountMap",value = {
            @Result(id=true,property = "id",column = "id"),
            @Result(property = "uid",column = "uid"),
            @Result(property = "money",column = "money"),
            @Result(property = "user",column = "uid",one=@One(select = "com.chenpeng.dao.UserDao.findById",fetchType = FetchType.EAGER))
    })
    List<Account> findAll();
    
    /**
     * 根据用户id查询账户
     * @return
     */
    @Select("select * from account where uid=#{uid}")
    List<Account> findByUid(Integer uid);
}

基于注解配置的缓存

  • 一级缓存默认开启,无需配置

  • 二级缓存配置

    • 在主配置文件SqlMapConfig.xml中添加配置

      <!--配置开启二级缓存-->
          <settings>
              <setting name="cacheEnabled" value="true"/>
          </settings>
      
    • 在dao层接口的定义上添加@CacheNamespace(blocking = true)注解

      @CacheNamespace(blocking = true)
      public interface UserDao {
      	...
      }
      
原文地址:https://www.cnblogs.com/codeDD/p/12699079.html