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")
相关推荐
程序员大金39 分钟前
基于SSM+Vue+MySQL的酒店管理系统
前端·vue.js·后端·mysql·spring·tomcat·mybatis
Tatakai2516 小时前
Mybatis Plus分页查询返回total为0问题
java·spring·bug·mybatis
A_cot17 小时前
Redis 的三个并发问题及解决方案(面试题)
java·开发语言·数据库·redis·mybatis
晚睡早起₍˄·͈༝·͈˄*₎◞ ̑̑18 小时前
苍穹外卖学习笔记(七)
java·windows·笔记·学习·mybatis
二十雨辰21 小时前
[苍穹外卖]-12Apache POI入门与实战
java·spring boot·mybatis
Amagi.1 天前
Redis的内存淘汰策略
数据库·redis·mybatis
执键行天涯1 天前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
工业甲酰苯胺1 天前
Spring Boot 整合 MyBatis 的详细步骤(两种方式)
spring boot·后端·mybatis
ggdpzhk1 天前
Mybatis 快速入门(maven)
oracle·maven·mybatis
Java小白笔记2 天前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis