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")
相关推荐
77qqqiqi2 小时前
解决Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required报错问题
java·数据库·微服务·mybatis·mybatisplus
橘子编程3 小时前
SpringMVC核心原理与实战指南
java·spring boot·spring·tomcat·mybatis
Savvy..3 小时前
Day07 JDBC+MyBatis
mybatis·jdbc·数据库连接池·sql注入·yml
找不到、了4 小时前
关于MyBatis 的懒加载(Lazy Loading)机制
java·mybatis
慌糖5 小时前
Spring Boot音乐服务器项目-查询喜欢的音乐模块
服务器·spring boot·mybatis
love静思冥想7 小时前
MyBatis XML 配置方式是 返回 Boolean 类型
xml·mybatis
昵称为空C10 小时前
基于Mybatis-Plus的数据库操作日志方案
spring boot·mybatis
Catfood_Eason11 小时前
MyBatis与Spring的整合
sql·spring·mybatis
小醉你真好15 小时前
「Spring Boot + MyBatis-Plus + MySQL 一主两从」读写分离实战教程
spring boot·mysql·mybatis
程序猿小D1 天前
基于SpringBoot+MyBatis+MySQL+VUE实现的便利店信息管理系统(附源码+数据库+毕业论文+远程部署)
数据库·spring boot·mysql·vue·mybatis·毕业论文·便利店信息管理系统