MyBatis的对象关系映射基于注解实现方法

1.基础传参

java 复制代码
 /***
     * mysql获取所有表以及备注信息
     *
     * @param database
     * @return
     */
    @Select("SELECT\n" +
            "table_name name,TABLE_COMMENT VALUE \n" +
            "FROM INFORMATION_SCHEMA.TABLES a \n" +
            "WHERE a.table_schema= #{database}  \n" +
            "AND table_type='BASE TABLE'")
    List<Map<String, Object>> getMySqlTableList(@Param("database") String database);

2.动态传参

java 复制代码
 @Update({"<script>",
         "update Author",
         "  <set>",
         "    <if test='username != null'>username=#{username},</if>",
         "    <if test='password != null'>password=#{password},</if>",
         "    <if test='email != null'>email=#{email},</if>",
         "    <if test='bio != null'>bio=#{bio}</if>",
         "  </set>",
         "where id=#{id}",
         "</script>"})
 void updateAuthorValues(Author author);

3.// 返回非自增主键

java 复制代码
    
    @Insert({"INSERT INTO sys_user(user_name,user_password,user_email,user_info) values(#{userName},#{userPassword},#{userEmail},#{userInfo})"})
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert2(SysUser user);

4.//resultMap方式进行对象映射配置(Mybatis3.3.0版本以前)

java 复制代码
@Results({
    @Result(property="id",column="id",id=true),
    @Result(property="roleName",column="role_name"),
    @Result(property="createTime",column="create_time"),
    @Result(property="enabled",column="enabled"),
    @Result(property="createBy",column="create_by"),
})
@Select("select id,role_name,enabled,create_by,create_time"
        + "from role where id=#{id}")
Role selectById2(long id);

5.Provider注解

java 复制代码
  public String selectById(final long id){
        return new SQL().SELECT("id,role_name,enabled,create_by,create_tim").FROM("role").WHERE("id=#{id}").toString();
    }

6.根据id查询

复制代码
@SelectProvider(type=RoleProvider.class,method="selectById")
相关推荐
涵涵(互关)9 小时前
Maven多模块项目MyMetaObjectHandler自动填充日期未生效
spring·maven·mybatis
ss27316 小时前
手写MyBatis第96弹:异常断点精准捕获MyBatis深层BUG
java·开发语言·bug·mybatis
lang2015092818 小时前
MyBatis配置全解析:核心要点详解
mybatis
yunmi_1 天前
安全框架 SpringSecurity 入门(超详细,IDEA2024)
java·spring boot·spring·junit·maven·mybatis·spring security
wuxuanok1 天前
苍穹外卖 —— 公共字段填充
java·开发语言·spring boot·spring·mybatis
伯明翰java1 天前
mybatis-generator插件自动生成mapper及其实体模型配置
java·开发语言·mybatis
简色2 天前
题库批量(文件)导入的全链路优化实践
java·数据库·mysql·mybatis·java-rabbitmq
lunzi_fly2 天前
【源码解读之 Mybatis】【核心篇】--第5篇:Executor执行器体系详解
mybatis
韩立学长2 天前
【开题答辩实录分享】以《走失人口系统档案的设计与实现》为例进行答辩实录分享
mysql·mybatis·springboot
Flash Dog2 天前
【MyBatis】——执行过程
java·mybatis