zoukankan      html  css  js  c++  java
  • MyBatis(九)、注解开发

    注解开发代替的是mapper.xml配置文件,主配置文件不能被替换。

    使用@results注解解决别名问题和@result注解

    public interface IUserDao {
    /**
    * 查询所有用户
    * @return
    */
    @Select("select * from user")
    @Results(id="userMap",
    value= {
    @Result(id=true,column="id",property="userId"),
    @Result(column="username",property="userName"),
    @Result(column="sex",property="userSex"),
    @Result(column="address",property="userAddress"),
    @Result(column="birthday",property="userBirthday")
    })
    List<User> findAll();
    
    
    /**
    * 根据 id 查询一个用户
    * @param userId
    * @return
    */
    @Select("select * from user where id = #{uid} ")
    @ResultMap("userMap")
    //@ResultMap(value={"userMap"})
    User findById(Integer userId);
    /**
    * 保存操作
    * @param user
    * @return
    */
    @Insert("insert into 
    user(username,sex,birthday,address)values(#{username},#{sex},#{birthday},#{address}
    )")
    @SelectKey(keyColumn="id",keyProperty="id",resultType=Integer.class,before = 
    false, statement = { "select last_insert_id()" })
    int saveUser(User user);
    /**
    * 更新操作
    * @param user
    * @return
    */
    @Update("update user set 
    username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id 
    =#{id} ")
    int updateUser(User user);
    /**
    * 删除用户
    * @param userId
    * @return
    */
    @Delete("delete from user where id = #{uid} ")
    int deleteUser(Integer userId);
    /**
    * 查询使用聚合函数
    * @return
    */
    @Select("select count(*) from user ")
    int findTotal();
    
    @Results 注解
    代替的是标签<resultMap>
    该注解中可以使用单个@Result 注解,也可以使用@Result 集合
    @Results({@Result(),@Result()})或@Results(@Result())
    @Resutl 注解
    代替了 <id>标签和<result>标签
    @Result 中 属性介绍:
    id 是否是主键字段
    column 数据库的列名
    property 需要装配的属性名
    one 需要使用的@One 注解(@Result(one=@One)()))
    many 需要使用的@Many 注解(@Result(many=@many)()))
    @One 注解(一对一)
    代替了<assocation>标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
    @One 注解属性介绍:
    select 指定用来多表查询的 sqlmapper
    fetchType 会覆盖全局的配置参数 lazyLoadingEnabled。。
    使用格式:
    @Result(column=" ",property="",one=@One(select=""))
    @Many 注解(多对一)
    代替了<Collection>标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。
    注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType
    (一般为 ArrayList)但是注解中可以不定义;
    使用格式:
    @Result(property="",column="",many=@Many(select=""))
    

    一对一[通常立即加载]

    /**
    * 查询所有账户,采用延迟加载的方式查询账户的所属用户
    * @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(column="uid",
    property="user",
    one=@One(select="com.itheima.dao.IUserDao.findById",
    fetchType=FetchType.LAZY)
    )
    })
    List<Account> findAll();
    

    一对多[通常延迟加载]

    /**
    * 查询所有用户
    * @return
    */
    @Select("select * from user")
    @Results(id="userMap",
    value= {
    @Result(id=true,column="id",property="userId"),
    @Result(column="username",property="userName"),
    @Result(column="sex",property="userSex"),
    @Result(column="address",property="userAddress"),
    @Result(column="birthday",property="userBirthday"),
    @Result(column="id",property="accounts",
    many=@Many(
    select="com.itheima.dao.IAccountDao.findByUid",
    fetchType=FetchType.LAZY
    )
    )
    })
    List<User> findAll();
    }
    @Many:
    相当于<collection>的配置
    select 属性:代表将要执行的 sql 语句
    fetchType 属性:代表加载方式,一般如果要延迟加载都设置为 LAZY 的值
    

    缓存配置

    一级缓存默认都是开启的

    二级缓存需要自己开启

    • 1.全局配置的setting标签
    • 2.Dao上面@CacheNamespace(blocking = true)
  • 相关阅读:
    MongoDB-基础-limit-skip-sort
    MongoDB-基础-条件操作符
    mongodb-基础-update-remove
    Linq to sql-存储过程
    SQL Server-游标使用
    JavaScript-求时间差
    HTTP 错误 500.21
    .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
    HTTP 错误 500.21
    WebApi&MVC对比
  • 原文地址:https://www.cnblogs.com/biturd/p/12623135.html
Copyright © 2011-2022 走看看