MyBatis注解配置

Select  映射查询的语句

Selectrovider  Select语句的动态SQL映射。允许指定一个类型和一个方法在执行时返回运行的查询语句。有两个属性:type和method,type属性是类的完全限定名,method是该类中的哪个方法名

Insert  插入语句

InsertProvider

update

updateProvider

delete

deleteProvider

Result  在列和属性之间的单独结果映射。属性包括:id,column,property,javaType,jdbcType,type Handler,one,many。id属性是一个布尔值,表示是否被用于主键映射。one属性是单独的联系,many属性是对集合而言的

Results  多个结果映射(Result)列表

Options  提供配置选项的附加值,他们通常在映射语句上作为附加功能配置出现

One  复杂类型的单独属性值映射。必须指定select属性,表示已映射的sql语句的完全限定名

Many  复杂类型的集合属性映射。必须指定select属性,表示已映射的sql语句的完全限定名

Param  当映射器方法需要多个参数时,这个注解可以被应用于映射器参数来给每一个参数取一个名字。否则,多参数将会以它们的顺序位置和sql语句中的表达式进行映射,这是默认的。使用@Param("id"),sql中参数应该被命名为#{id}

 

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

@Options(useGenerateedKeys=true,keyProperty="id")//使用数据库自动增长的主键,该操作需要底层数据库的支持,keyProperty="id" 表示将插入数据生成的主键设置到user对象的id当中

int saveUser(User user);

 

@Delete("delete frome user where id = #{id}")

int removeUser(@Param("id") Integer id);@Param("id")表示给该注解后边的变量取一个参数名称,对应@Delete中的#{id}

 

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

void modifyUser(User user);

 

@Select("select * from user where id = #{id}")

@Results({

@Result(id=true,column="id",property="id"),@Result(column="name",property="name"),@Result(column="sex",property="sex"),@Result(column="age",property="age")如果列和属性相同,则可以省略@Result注解,MyBatis会自动映射

})

 

User selectUserById(Integer id);

 

@Select("select * from user")

List<User> getAllUsers();

 

一人一个身份证

@Select("select * from user where id = #{id}")

@Results({

@Result(id=true,column="id",property="id"),

@Result(column="name",property="name"),

@Result(column="sex",property="sex"),

@Result(column="age",property="age"),

@Result(column="card_id",property="card",

 one=@One(select="com.mapper.UserMapper.selectCardById"),fetchType=FetchType.EAGER)

)

})

column="card_id",property="card"表示User的card属性对应表里的card_id列,one表示一个一对一关联关系,fetchType表示查询类型是立即加载(EAGER)还是懒加载(LAZY)

 

many=@Many(select="",fetchType="")

属性是一个集合,一对多

 

posted @ 2017-06-23 00:59  腾飞新星  阅读(1774)  评论(0编辑  收藏  举报