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")
相关推荐
面向星辰15 小时前
sql基本增删改查语句汇总
数据库·sql·mybatis
凌波粒20 小时前
SpringMVC基础教程(3)--SSM框架整合
java·sql·spring·intellij-idea·mybatis
humors2212 天前
服务端开发案例(不定期更新)
java·数据库·后端·mysql·mybatis·excel
朝新_2 天前
【实战】动态 SQL + 统一 Result + 登录校验:图书管理系统(下)
xml·java·数据库·sql·mybatis
百***84452 天前
SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)
spring boot·tomcat·mybatis
q***71852 天前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
原来是好奇心2 天前
Spring Boot缓存实战:@Cacheable注解详解与性能优化
java·spring·mybatis·springboot
战血石LoveYY2 天前
mybatis踩坑值 <if test=> 不能用大写AND
mybatis
C++chaofan2 天前
基于session实现短信登录
java·spring boot·redis·mybatis·拦截器·session
神仙别闹2 天前
基于SpringMVC+Spring+MyBatis开发的个人博客网站
java·spring·mybatis