首页 > 其他分享 >MyBatis注解开发 | MyBatis

MyBatis注解开发 | MyBatis

时间:2022-09-20 23:44:35浏览次数:76  
标签:column id 开发 user MyBatis 注解 property public Result

1.常用注解
@Insert     新增
@Update     更新
@Delete     删除
@Select     查询
@Result     结果集
@Results    封装多个结果集
@One        一对一结果集
@Many       一对多结果集 
  • 注解代替Mapper.xml文件
    • mapper.UserMapper

      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();
      
          /**
           * 一对多
           */
          @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();
      
          @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();
      }
      
    • mapper.OrderMapper

      public interface OrderMapper {
          @Select("select * from orders where uid=#{uid}")
          public List<Order> findByUid(int uid);
      
          /**
           * 一对一 方式二.两张表分开查
           */
          @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();
      
          /** 
           * 一对一 方式一.两张表一起查
           */
          /*@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();*/
      }
      
    • mapper.RoleMapper

      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);
      }
      
    • 加载映射关系

      <mappers>
          <package name="com.miaokela.mapper"></package>
      </mappers>
      
    • 附录(模型类):

      domain.User

      public class User {
      
          private int id;
          private String username;
          private String password;
          private Date birthday;
      
          //当前用户具备哪些角色
          private List<Role> roleList;
      
          public List<Role> getRoleList() {
              return roleList;
          }
      
          public void setRoleList(List<Role> roleList) {
              this.roleList = roleList;
          }
      
          /*//描述的是当前用户具有的订单
          private List<Order> orderList;
          public List<Order> getOrderList() {
              return orderList;
          }
          public void setOrderList(List<Order> orderList) {
              this.orderList = orderList;
          }*/
      
          public Date getBirthday() {
              return birthday;
          }
      
          public void setBirthday(Date birthday) {
              this.birthday = birthday;
          }
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public String getUsername() {
              return username;
          }
      
          public void setUsername(String username) {
              this.username = username;
          }
      
          public String getPassword() {
              return password;
          }
      
          public void setPassword(String password) {
              this.password = password;
          }
      
          @Override
          public String toString() {
              return "User{" +
                      "id=" + id +
                      ", username='" + username + '\'' +
                      ", password='" + password + '\'' +
                      ", birthday=" + birthday +
                      ", roleList=" + roleList +
                      '}';
          }
      }
      

      domain.Order

      public class Order {
      
          private int id;
          private Date ordertime;
          private double total;
      
          //当前订单属于哪一个用户
          private User user;
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public Date getOrdertime() {
              return ordertime;
          }
      
          public void setOrdertime(Date ordertime) {
              this.ordertime = ordertime;
          }
      
          public double getTotal() {
              return total;
          }
      
          public void setTotal(double total) {
              this.total = total;
          }
      
          public User getUser() {
              return user;
          }
      
          public void setUser(User user) {
              this.user = user;
          }
      
          @Override
          public String toString() {
              return "Order{" +
                      "id=" + id +
                      ", ordertime=" + ordertime +
                      ", total=" + total +
                      ", user=" + user +
                      '}';
          }
      }
      

      domain.Role

      public class Role {
      
          private int id;
          private String roleName;
          private String roleDesc;
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public String getRoleName() {
              return roleName;
          }
      
          public void setRoleName(String roleName) {
              this.roleName = roleName;
          }
      
          public String getRoleDesc() {
              return roleDesc;
          }
      
          public void setRoleDesc(String roleDesc) {
              this.roleDesc = roleDesc;
          }
      
          @Override
          public String toString() {
              return "Role{" +
                      "id=" + id +
                      ", roleName='" + roleName + '\'' +
                      ", roleDesc='" + roleDesc + '\'' +
                      '}';
          }
      }
      

标签:column,id,开发,user,MyBatis,注解,property,public,Result
From: https://www.cnblogs.com/miaokela/p/16714103.html

相关文章

  • day01-项目开发流程
    多用户即时通讯系统011.项目开发流程2.需求分析用户登录拉取在线用户列表无异常退出(包括客户端和服务端)私聊群聊发文件服务器推送新闻/广播3.设计阶段3.1界......
  • 开发工具的下载与学习使用
    1、IntelliJIDEA2022.2.2的下载和破解过程:先上IDEA官网下载IDEA  下载安装IDEA后,再去下载激活补丁  在 idea64.exe.vmoptions 配置文件结尾添加如下配置:......
  • Mybatis使用PageHelper分页插件
    1<dependency>2<groupId>com.github.pagehelper</groupId>3<artifactId>pagehelper-spring-boot-starter</artifactId>4......
  • mybatisplus打印SQL日志 【mybatisplus专栏】
    一、mybatisplus如何打印SQL日志:在SpringBoot项目中添加以下配置#方式一mybatis-plus:configuration:log-impl:org.apache.ibatis.logging.stdout.StdOutImp......
  • 分析mybatis的#{}、${}(#绑定变量超过一定值导致Oracle挂掉)
    我后来改成了1万条,数据库相对小点压力不大,也不会出现重启的问题; 最近跟数据库干上了先说下问题起源,算奖确认订单,需要批量update订单,查了相关资料,是mybatis一次性绑定变......
  • MyBatis返回Map键值对数据Key值大小写问题
    Controller@RestController@RequestMapping("/web")publicclassMapKeyTest{@AutowiredprivateInvoicingBuyOrderServiceinvoicingBuyOrderService;......
  • 什么是 开发环境、测试环境、生产环境、UAT环境、仿真环境
    开发环境:开发环境是程序猿们专门用于开发的服务器,配置可以比较随意,为了开发调试方便,一般打开全部错误报告。测试环境:一般是克隆一份生产环境的配置,一个程序在测试环境工......
  • Mybatis if标签判断数字大小
    if标签语法<select...>SQL语句1<iftest="条件表达式">SQL语句2</if></select>条件表达式中大于号小于号用gt,lt<iftest="numgt0">...</if><if......
  • Mybatis-plus 数据安全保护
    1、加密前 application-dev.ymlspring:datasource:url:jdbc:p6spy:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTim......
  • 05.有关MyBatis的配置与使用
    有关MyBatis的配置与使用虽然SpringDataJPA在国外广泛流行,但是在国内仍是MyBatis使用更加普遍整合MyBatis第一步:新建SpringBoot项目,在pom.xml中引入MyBatis的Starter......