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")
相关推荐
f***S24421 分钟前
MyBatis-Plus 自定义 SQL 和复杂查询
数据库·sql·mybatis
t***821129 分钟前
Spring Boot 整合 MyBatis 与 PostgreSQL 实战指南
spring boot·postgresql·mybatis
0***v7772 小时前
springboot 异步操作
java·spring boot·mybatis
ps酷教程2 小时前
java泛型反射&mybatis的TypeParameterResolver
java·mybatis
b***59432 小时前
springboot+mybaties项目中扫描不到@mapper注解的解决方法
java·spring boot·mybatis
a crazy day2 小时前
Mybatis相关知识点【详细版】
mybatis
李宥小哥2 小时前
Redis14-实践-查询缓存
spring·缓存·mybatis
P***84397 小时前
idea、mybatis报错Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
tomcat·intellij-idea·mybatis
p***950010 小时前
Springboot3 Mybatis-plus 3.5.9
数据库·oracle·mybatis
倚肆10 小时前
MyBatis-Plus Mapper 接口方法详解
java·mybatis