xml映射器
@Many注解使用
通常用在一个实体类中查询List<t>
@Mapper
public interface UserMapper {
@Results(id = "userResult", value = {
@Result(property = "id", column = "id", id = true),
@Result(property = "userName", column = "userName"),
@Result(property = "manList", column = "id", many = @Many(select = "com.mybatis.demo.mybatis.mapper.UserMapper.selectManById"))
})
@Select(" select * from user where id = #{id} ")
User selectById(@Param("id") Integer id);
@Results(id = "man", value = {
@Result(property = "id", column = "id", id = true),
@Result(property = "name", column = "name")
})
@Select({"select * from man where man.user_id = #{userId}"})
List<Man> selectManById(@Param("userId") Integer userId);
}
@Option 属性
useGeneratedKeys(仅适用于 insert 和 update)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系型数据库管理系统的自动递增字段),默认值:false。
查入语句会自动查询新增的id,如果是新增一个实体类的话会自动设置实体类的id
@Options(useGeneratedKeys = true)
网友评论