2021-03-26

2 常用注解

2.1 @Param()参数注解

在mapper接口与xml配置时,接口中的方法名与对应的id匹配,但是参数一般使用一个参数,并且mybatis拥有类型解析器,会自动获取对应的参数数据(mapper接口参数列表的变量与xml中实际获取的变量名没有关联),是由对应的解析器进行解析获取,如果获取不到,也会在错误信息中提示可以使用的参数名列表,如果想传递多个不同类型的参数并指定传递的变量名,那么就必须使用@Param()参数注解

 Parameter 'paraasdsad' not found. Available parameters are [asdasd, param1]

在使用@Param注解前如果没有找到会提示

Parameter 'asdsad' not found. Available parameters are [arg0, arg1, param1, param2]

在使用@Param注解在对应的接口中设置绑定的变量名时会将指定名加入参数列表

    //根据id修改名字
    //@Param参数绑定 为指定的参数 绑定一个名字 可以在对应的mapper.xml中直接使用名字获取对应的值
    int updateAnameByAid(@Param("aname") String aname, @Param("aid") int aid);
Parameter 'asdsad' not found. Available parameters are [aname, aid, param1, param2]

2.2 @Select()查询注解

语法:@Select(value=“查询语句”)可以简写为@Select(“查询语句”)

等价于mapper.xml中的

如果没有设置参数与返回值类型,默认使用当前的方法参数与返回值作为使用

    //查询所有数据
    @Select("select * from author")
    ArrayList<Author> selectAll();
    //等价于
    <select id="selectAll" resultType="com.yunhe.vo.Author">
        select *
        from author
    </select>
    //根据id查询
    @Select("select * from author where aid= #{aid}")
    Author selectByAid(@Param("aid") int aid);

2.3 @Insert()添加注解

语法:@Insert(value=“添加语句”)可以简写为@Insert(“添加语句”)

等价于mapper.xml中的

注意:在进行赋值时#{属性}是参数的属性 需要提供getter方法才能获取

如果没有设置参数,默认使用当前的方法参数作为使用

    //添加
    @Insert("insert into author (aname,aage,atel,aaddress)values(#{aname},#{aage},#{atel},#{aaddress})")
    int insert(Author author);

2.4 @Update()修改注解

语法:@Update(value=“修改语句”)可以简写为@Update(“修改语句”)

等价于mapper.xml中的

如果没有设置参数,默认使用当前的方法参数作为使用

    //根据id修改名字
    @Update("update author set aname=#{aname} where aid = #{aid}")
    int updateAnameByAid(@Param("aname") String aname, @Param("aid") int aid);

2.5 @Delete()删除注解

语法:@Delete(value=“删除语句”)可以简写为@Select(“删除语句”)

等价于mapper.xml中的

如果没有设置参数,默认使用当前的方法参数作为使用

//根据id删除
@Delete("delete from author where aid=#{aid}")
int delete(@Param("aid") int id);

2.6 动态sql注解

在对应的标签中使用{}将带有动态sql语句包裹并在语句前后使用进行标识

由于动态sql书写复杂,所以一般使用xml配置形式进行实现

数组批量删除

xml配置形式

<delete id="deleteByArray">
    delete from author where aid in
    <foreach item="i" collection="arr" open="(" close=")" separator=",">
        #{i}
    </foreach>
</delete>

注解形式

    //动态sql批量删除
    @Delete({"<script>delete from author where aid in\n" +
            "    <foreach item=\"i\" collection=\"arr\" open=\"(\" close=\")\" separator=\",\">\n" +
            "        #{i}\n" +
            "    </foreach></script>"})
    int deleteByArray(@Param("arr") int [] arr);

集合批量注册

xml配置形式

    <insert id="inserColleaction">
        insert into author (aname,aage,atel,aaddress) values
        <foreach collection="a" item="i" separator="," >
            (#{i.aname},#{i.aage},#{i.atel},#{i.aaddress})
        </foreach>
    </insert>

注解形式

    //动态sql批量注册
    @Insert({"<script>        insert into author (aname,aage,atel,aaddress) values\n" +
            "        <foreach collection=\"a\" item=\"i\" separator=\",\" >\n" +
            "            (#{i.aname},#{i.aage},#{i.atel},#{i.aaddress})\n" +
            "        </foreach></script>"})
    int inserColleaction(@Param("a") ArrayList<Author> alist);

2.7 @Results()、@Result()、@resultMap()结果映射注解

@Results()

语法:@Results(id=“唯一标识”,value={result标签数组})

等价于mapper.xml中的

当返回结果字段与实体类字段不匹配时

@Result()

语法:@Result(id=“true/false是否为主键”,column=“数据库列”,property=“属性名”)

等价于mapper.xml中的与

@resultMap()

语法:@resultMap(value=“返回结果集映射id”)可以简写为@resultMap(“返回结果集映射id”)

等价于mapper.xml中的resultMap属性

查询所有数据

xml配置形式

    <resultMap id="t_user" type="com.yunhe.vo.T_User">
        <id property="id" column="uid"/>
        <result property="username" column="uusername"/>
        <result property="password" column="upassword"/>
    </resultMap>

    <select id="selectAll" resultMap="t_user">
        select *
        from user
    </select>

注解形式

    @Results( id = "aaa" ,value = {
            @Result(id = true ,property = "id",column = "uid"),
            @Result(property = "username",column = "uusername"),
            @Result(property = "password",column = "upassword")
    })
    @Select("select * from user")
    ArrayList<T_User> selectAll();
	

    /* @ResultMap("aaa") 重用别的方法设置好的resultMap*/
    @Select("select * from user")
    @ResultMap("aaa")
    ArrayList<T_User> selectAll1();

注解在当前方法会直接生效所以第一个书写@Results标签的方法无需设置@ResultMap注解可以直接使用,在当前方法中的其他方法如果想重复使用需要使用@ResultMap(“id”)注解

2.8 @One()结果映射一对一注解

语法:@One(select=“执行方法”,fetchType=“加载方式(FetchType.LAZY深入懒加载)”)

one是@Result标签的一个属性,添加后相当于将result标签转换为mapper.xml中的

xml配置形式

   <resultMap id="user" type="com.yunhe.vo.User">
        <id property="uid" column="uid"/>
        <result property="uusername" column="uusername"/>
        <result property="upassword" column="upassword"/>
       <association property="role"  column="uid" select="com.yunhe.mapper.RoleMapper.selectByUid" fetchType="lazy"/>
    </resultMap>

    <select id="selectAll" resultMap="user">
        select * from user
    </select>

注解形式

@Results( id = "user" ,value = {
            @Result(id = true,property = "uid",column = "uid"),
            @Result(property = "uusername",column = "uusername"),
            @Result(property = "upassword",column = "upassword"),
            @Result(property = "role" ,column = "uid",one =@One(select = "com.yunhe.mapper.RoleMapper.selectByUid",fetchType = FetchType.LAZY))
    })
    @Select("select * from user")
    ArrayList<User> selectAllAndRole();

2.9 @Many()注解结果映射一对多注解

语法:@Many(select=“执行方法”,fetchType=“加载方式(FetchType.LAZY深入懒加载)”)

Many是@Result标签的一个属性,添加后相当于将result标签转换为mapper.xml中的

    @Results( id = "users" ,value = {
            @Result(id = true,property = "uid",column = "uid"),
            @Result(property = "uusername",column = "uusername"),
            @Result(property = "upassword",column = "upassword"),
            @Result(property = "role" ,column = "uid",many =@Many(select = "com.yunhe.mapper.RoleMapper.selectByUids",fetchType = FetchType.LAZY))
    })
    @Select("select * from user")
    ArrayList<Users> selectAllAndRoles();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2021-03-26 20:54:33,596 - Model - INFO - Epoch 1 (1/200): 2021-03-26 20:57:40,380 - Model - INFO - Train Instance Accuracy: 0.571037 2021-03-26 20:58:16,623 - Model - INFO - Test Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Best Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Save model... 2021-03-26 20:58:16,623 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 20:58:16,698 - Model - INFO - Epoch 2 (2/200): 2021-03-26 21:01:26,685 - Model - INFO - Train Instance Accuracy: 0.727947 2021-03-26 21:02:03,642 - Model - INFO - Test Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Best Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Save model... 2021-03-26 21:02:03,643 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 21:02:03,746 - Model - INFO - Epoch 3 (3/200): 2021-03-26 21:05:15,349 - Model - INFO - Train Instance Accuracy: 0.781606 2021-03-26 21:05:51,538 - Model - INFO - Test Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,538 - Model - INFO - Best Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,539 - Model - INFO - Save model... 2021-03-26 21:05:51,539 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 我有类似于这样的一段txt文件,请你帮我写一段代码来可视化这些训练结果
02-06

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值