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")
相关推荐
sensenlin913 天前
Mybatis中SQL全大写或全小写影响执行性能吗
数据库·sql·mybatis
BXCQ_xuan3 天前
软件工程实践四:MyBatis-Plus 教程(连接、分页、查询)
spring boot·mysql·json·mybatis
wuyunhang1234563 天前
Redis----缓存策略和注意事项
redis·缓存·mybatis
lunz_fly19923 天前
【源码解读之 Mybatis】【基础篇】-- 第2篇:配置系统深度解析
mybatis
森林-3 天前
MyBatis 从入门到精通(第一篇)—— 框架基础与环境搭建
java·tomcat·mybatis
森林-3 天前
MyBatis 从入门到精通(第三篇)—— 动态 SQL、关联查询与查询缓存
sql·缓存·mybatis
java干货3 天前
MyBatis 的“魔法”:Mapper 接口是如何找到并执行 SQL 的?
数据库·sql·mybatis
嬉牛4 天前
项目日志输出配置总结(多数据源MyBatis+Logback)
mybatis·logback
哈喽姥爷5 天前
Spring Boot--yml配置信息书写和获取
java·数据库·spring boot·mybatis
奔跑你个Run6 天前
mybatis plus 使用wrapper输出SQL
mybatis