博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MyBatis - 08注解

Posted on 2020-11-24 21:19  Kingdomer  阅读(115)  评论(0编辑  收藏  举报

MyBatis - 08注解

(1)MyBatis常用注解

@Insert  @Update  @Delete  @Select  @Result  @Results  @One  @Many

 

(2)基本CRUD

public interface UserMapper {

    @Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
    public void save(User user);

    @Update(" update user set username=#{username}, password=#{password} where id=#{id}")
    public void update(User user);

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

    @Select("select * from user where id=#{id}")
    public User findById(int id);

    @Select("select * from user")
    public List<User> findAll();
}

加载映射关系, 指定接口所在包

    <mappers>
        <package name="com.bearpx.spring.mybatis.dao"></package>
    </mappers>

 

(3)复杂映射

注解 说明
@Results

代替<resultMap>标签,可以使用单个@Result注解,也可以使用@Result集合。

使用格式: @Results({@Result(), @Result()}) 或  @Results(@Result())

@Result

代替<id>和<result>标签。  @Result属性介绍:

column:   数据库的列名  

property: 需要装配的属性名

one:  需要使用的@One注解(@Result(one=@One)())

many: 需要使用的@Many注解(@Result (many=@many)())

@One(一对一)

代替<assocation>标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。

@One注解属性介绍:

select: 指定用来多表查询的sqlmapper

使用格式: @Result(column="", property="", one=@One(select=""))

@Many(多对一)

代替<collection>标签,是多表查询的关键,在注解中用来指定子查询返回对象集合。

使用格式: @Result(column="", property="", many=@Many(select=""))

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(3.1)查询所有订单数据,及订单的用户信息

    @Select("select *,o.id oid from orders o, user u where o.uid = u.id")
    @Results({
          @Result(column = "oid",property = "id"),
          @Result(column = "ordertime",property = "ordertime"),
          @Result(column = "total",property = "total"),
          @Result(column = "uid",property = "user.id"),
          @Result(column = "username",property = "user.username"),
          @Result(column = "password",property = "user.password")
    })
    public List<Order> findAll();

 

    @Select("select * from orders")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "ordertime",property = "ordertime"),
            @Result(column = "total",property = "total"),
            @Result(
                    property = "user",      // 要封装的属性名称
                    column = "uid",         // 根据哪个字段去查询user表的数据
                    javaType = User.class,  // 要封装的实体类型
                    one = @One(select = "com.bearpx.spring.mybatis.dao.UserMapper.findById")  // 查询哪个接口的方法获取数据
            )
    })
    public List<Order> findAll2();

(3.2)一对多查询: 查询所有用户,及用户下的所有订单数据

    @Select("select * from user")
    @Results({
            @Result(id=true, column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password")  ,
            @Result(
                    property = "orderList",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select="com.bearpx.spring.mybatis.dao.OrderMapper.findByUid")
            )
    })
    public List<User> findUserAndOrderAll();

OrderMapper接口

    @Select("select * from orders where uid=#{uid}")
    public List<Order> findByUid(int uid);

(3.3)多对多查询: 查询用户的角色信息

    @Select("select * from user")
    @Results({
            @Result(id=true, column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password")  ,
            @Result(
                    property = "roleList",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select="com.bearpx.spring.mybatis.dao.RoleMapper.findByUid")
            )
    })
    public List<User> findUserAndRoleAll();

 

public interface RoleMapper {

    @Select("select * from sys_user_role ur,sys_role r where ur.roleId = r.id and ur.userId=#{uid}")
    public List<Role> findByUid(int uid);
}