ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MyBatis常用注解

2022-07-27 21:04:41  阅读:239  来源: 互联网

标签:常用 column public user MyBatis 注解 property id Result


MyBatis的常用注解

 

 

 在Mybatis框架中,对于单表简单的CRUD,我们可以通过下面的注解来完成。

@Insert:实现新增

@Update:实现更新

@Delete:实现删除

@Select:实现查询

@Result:实现结果集封装

@Results:可以与@Result一起使用,封装多个结果集

@One:实现一对一结果集封装

@Many:实现一对多结果集封装

使用注解开发减少了xml中繁琐的配置,大大提高了开发的效率。我们通过在mapper接口中添加注解,用较少的代码实现相同的功能,参考下面的代码示例。

package com.itheima.mapper;

import com.itheima.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

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();

}

MyBatis的注解实现复杂映射开发

实现复杂关系映射之前我们可以在映射文件中通过配置<resultMap>来实现,使用注解开发后,我们可以使用@Results注解@Result注解,@One注解,@Many注解组合完成复杂关系的配置

@Results代替的是标签<resultMap>该注解中可以使用单个@Result注解,也可以使用@Result集@Results

合。使用格式:@Results((@Result(),@Result()1)或@Results(@Result())

@Result代替了<id>标签和<result>标签

@Result中属性介绍:

column:数据库的列名

property:需要装配的属性名

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

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

 

一对一

这种通常的使用场景是两个表的数据交汇,需要连接查询,这里的是一个实体对象里封装了另一个实体对象。

多表连接查询

*@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, //要封装的实体类型
                    //select属性 代表查询那个接口的方法获得数据
                    one = @One(select = "com.itheima.mapper.UserMapper.findById")
            )
    })
public List<Order> findAll();

这里的@One中的是通过获取到的id再在user表通过id中查询一次

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

一对多

通常一个用户对应着多个订单,形成了一对多,这里是在user实体类中封装了order实体类的List集合。

@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.itheima.mapper.OrderMapper.findByUid")
            )
    })
    public List<User> findUserAndOrderAll();

这里的@Many是通过获取到的id再在order表中查询多次

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

多对多

一个user对应着多个称号,而一个称号又被多个人拥有,是一个简单的多对多关系。

@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.itheima.mapper.RoleMapper.findByUid")
            )
    })
    public List<User> findUserAndRoleAll();

这里的@Many是通过获取到的id再在role和user的关系表和称号表联合查询多次

 @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);

 

MyBatis注解开发做简单的增删改查很方便,对于复杂的需求还是有一定难度的,今天学的这些其实我还不是很理解,可能存在一些理解错误,欢迎大家批评指正。

标签:常用,column,public,user,MyBatis,注解,property,id,Result
来源: https://www.cnblogs.com/wjingbo/p/16526258.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有