Mybatis常用注解简介说明

宫欣彤 MyBatis 2022-06-07 11:01:21 12913 1
下文笔者讲述MyBatis的常用注解简介说明,如下所示
注解 目标 对应的XML标签
@CacheNamespace <cache>
@CacheNamespaceRef <cacheRef>
@Results 方法 <resultMap>
@Result 方法 <result>
<id>
@One 方法 <association>
@Many 方法 <collection>
@Insert
@Update
@Delete
方法 <insert>
<update>
<delete>
@InsertProvider
@UpdateProvider
@DeleteProvider
@SelectProvider
方法 <insert>
<update>
<delete>
<select> 允许创建动态SQL
@Param 参数 N/A
@Options 方法 映射语句的属性
@select 方法 <select>

@Select简单查询

@Select(" Select * from user ")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "tel", property = "tel"),
            @Result(column = "birth", property = "birth"),
            @Result(column = "address", property = "address")
    })
    List<User> queryAllUser();

@Insert 简单插入

@Insert(" insert into user(name,sex,age) values(#{name},#{sex},#{age} " )
int saveUser(User user);

@Update 简单更新

@Update("update user set name= #{name} ,sex = #{sex},age =#{age} where id = #{id}")
void updateUserById(User user);

@Delete 简单删除

@Delete("delete from  user  where id =#{id} ")
void deleteById(Integer id);

@One

@Select(" select * from user where id = #{id} ")
    @Results(
            value = {
                    @Result(column = "name",property = "name"),
                    @Result(column = "type",property = "type",
                    //one指示我们,查询出来的结果只有一个。
                    one = @One(select="com.xxxx.UserMapper.findTypeById",
                    //及时加载  
                    fetchType = FetchType.EAGER))
            }
    )
    User findUserById(Integer id);

@Many

 
 @Select(" select * from dept")
    @Results({
            @Result(id = true, column = "did", property = "did"),
            @Result(column = "name", property = "name"),
            @Result(column = "address", property = "address"),
            @Result(column = "id",property = "emps",
            //many指示我们,查询出来的结果有很多个
            many = @Many(
            //select = sql语句
            select = "com.xxxx.EmpMapper.findAllEmpByDid",
            //懒加载
            fetchType = FetchType.LAZY))
    })
    List<Dept> findAllDept();
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: http://www.Java265.com/JavaFramework/MyBatis/202206/3670.html

最近发表

热门文章

好文推荐

Java265.com

//www.java265.com

站长统计|USA-001

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

Honor accompaniments