mybatis 的常用注解:

@Insert:实现新增

@Update:实现更新

@Delete:实现删除

@Select:实现查询

@Result:实现结果集封装

@Results:可以与

@Result 一起使用,封装多个结果集

@ResultMap:实现引用

@Results 定义的封装

@One:实现一对一结果集封装

@Many:实现一对多结果集封装

@SelectProvider: 实现动态 SQL 映射

@CacheNamespace:实现注解二级缓存的使用

使用 Mybatis 注解实现基本 CRUD

/**
* 在mybatis中针对,CRUD一共有四个注解
* @Select @Insert @Update @Delete
*/
public interface IUserDao { /**
* 查询所有用户
* @return
*/
@Select("select * from user")
  @Results(id="userMap",
value= {
@Result(id=true,column="id",property="userId"),
            @Result(column="username",property="userName"),
            @Result(column="sex",property="userSex"),
            @Result(column="address",property="userAddress"),
            @Result(column="birthday",property="userBirthday")
  })
List<User> findAll(); /**
* 保存用户
* @param user
*/
@Insert("insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday})")
  @SelectKey(keyColumn="id", keyProperty="id", resultType=Integer.class, before = false, statement = { "select last_insert_id()" })
void saveUser(User user); /**
* 更新用户
* @param user
*/
@Update("update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}")
void updateUser(User user); /**
* 删除用户
* @param userId
*/
@Delete("delete from user where id=#{id} ")
void deleteUser(Integer userId); /**
* 根据id查询用户
* @param userId
* @return
*/
@Select("select * from user where id=#{id} ")
  @ResultMap("userMap")
User findById(Integer userId); /**
* 根据用户名称模糊查询
* @param username
* @return
*/
// @Select("select * from user where username like #{username} ")
@Select("select * from user where username like '%${value}%' ")
List<User> findUserByName(String username); /**
* 查询总用户数量
* @return
*/
@Select("select count(*) from user ")
int findTotalUser();
}

SqlMapConfig.xml

<!-- 配置映射信息 -->
<mappers>
<!--
配置 dao 接口的位置,它有两种方式
第一种:使用 mapper 标签配置 class 属性
第二种:使用 package 标签,直接指定 dao 接口所在的包
-->
<package name="com.itheima.dao"/>
<!--
  <mapper class="com.itheima.dao.IUserDao"></mapper>
-->
</mappers>

使用注解实现复杂关系映射开发

实现复杂关系映射之前我们可以在映射文件中通过配置<resultMap>来实现,

在使用注解开发时我们需要借助@Results 注解,@Result 注解,@One 注解,@Many 注解。

复杂关系映射的注解说明 :

@Results 注解

  代替的是标签<resultMap> 

  该注解中可以使用单个@Result 注解,也可以使用@Result 集合

  @Results({@Result(),@Result()})或@Results(@Result())

@Result 注解

  代替了 <id>标签和<result>标签 

  @Result 中 属性介绍:

    id 是否是主键字段

    column 数据库的列名

    property 需要装配的属性名

    one  需要使用的@One 注解(@Result(one=@One)()))

    many  需要使用的@Many 注解(@Result(many=@many)())

@One 注解(一对一) 

  代替了<assocation>标签,是多表查询的关键,在注解中用来指定子查询返回单一对象

  @One 注解属性介绍:

    select  指定用来多表查询的 sqlmapper

    fetchType 会覆盖全局的配置参数 lazyLoadingEnabled

  使用格式:  @Result(column=" ",property="",one=@One(select=""))

@Many 注解(多对一)   

  代替了<Collection>标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。 

  注意:

    聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType (一般为 ArrayList)但是注解中可以不定义

  使用格式:

    @Result(property="",column="",many=@Many(select=""))

 使用注解实现一对一复杂关系映射及延迟加载 

public interface IAccountDao {
/**
* 查询所有账户,采用延迟加载的方式查询账户的所属用户
* @return
*/
  @Select("select * from account")
  @Results(id="accountMap",
        value= {
              @Result(id=true,column="id",property="id"),
              @Result(column="uid",property="uid"),
              @Result(column="money",property="money"),
              @Result(column="uid" property="user",
                    one=@One(
                        select="com.itheima.dao.IUserDao.findById",
                        fetchType=FetchType.LAZY
                         )
                   )
  })
  List
<Account> findAll();
}

使用注解实现一对多复杂关系映射:

需求:

  查询用户信息时,也要查询他的账户列表。使用注解方式实现。

public interface IUserDao {    

/**
* 查询所有用户
* @return */
  @Select("select * from user")
  @Results(id="userMap",
      value= {
        @Result(id=true,column="id",property="userId"),
        @Result(column="username",property="userName"),
        @Result(column="sex",property="userSex"),
        @Result(column="address",property="userAddress"),
        @Result(column="birthday",property="userBirthday"),
        @Result(column="id",property="accounts",
              many=@Many( select="com.itheima.dao.IAccountDao.findByUid", fetchType=FetchType.LAZY ) )
  })
  List<User> findAll();
}

@Many:
相当于<collection>的配置

select 属性:代表将要执行的 sql 语句

fetchType 属性:代表加载方式,一般如果要延迟加载都设置为 LAZY 的值

mybatis 基于注解的二级缓存

在 SqlMapConfig 中开启二级缓存支持

<!-- 配置二级缓存 --> 
<settings>
  <!-- 开启二级缓存的支持 -->
  <setting name="cacheEnabled" value="true"/>
</settings>

在持久层接口中使用注解配置二级缓存

@CacheNamespace(blocking=true)//mybatis 基于注解方式实现配置二级缓存
public interface IUserDao {}

Mybatis注解开发的更多相关文章

  1. Mybatis注解开发模糊查询

    Mybatis注解开发模糊查询 一般在使用mybatis时都是采用xml文件保存sql语句 这篇文章讲一下在使用mybatis的注解开发时,如何进行模糊查询 模糊查询语句写法(在@Select注解中) ...

  2. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-2.使用Mybatis注解开发视频列表增删改查

    笔记 2.使用Mybatis注解开发视频列表增删改查     讲解:使用Mybatis3.x注解方式 增删改查实操, 控制台打印sql语句              1.控制台打印sql语句      ...

  3. Spring Boot 使用Mybatis注解开发增删改查

    使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...

  4. Mybatis注解开发多表一对一,一对多

    Mybatis注解开发多表一对一,一对多 一对一 示例:帐户和用户的对应关系为,多个帐户对应一个用户,在实际开发中,查询一个帐户并同时查询该账户所属的用户信息,即立即加载且在mybatis中表现为一对 ...

  5. 【MyBatis】MyBatis 注解开发

    MyBatis 注解开发 文章源码 环境搭建 Mybatis 也可以使用注解开发方式,这样就可以减少编写 Mapper 映射文件. 常用注解说明: @Insert 实现新增 @Update 实现更新 ...

  6. Mybatis高级:Mybatis注解开发单表操作,Mybatis注解开发多表操作,构建sql语句,综合案例学生管理系统使用接口注解方式优化

    知识点梳理 课堂讲义 一.Mybatis注解开发单表操作 *** 1.1 MyBatis的常用注解 之前我们在Mapper映射文件中编写的sql语句已经各种配置,其实是比较麻烦的 而这几年来注解开发越 ...

  7. 【java框架】MyBatis(7)--MyBatis注解开发

    1.MyBatis注解开发 1.1.Lombok的基本使用 Lombok是SpringBoot2.1.X版本与IDEA官方支持的一个插件,它是为简化POJO类中繁杂重复代码:geter/setter/ ...

  8. Mybatis注解开发(一对一)

    其他代码访问:Mybatis注解开发基础操作 1.添加OrderMapper接口 public interface OrderMapper { // @Select("select *,o. ...

  9. MyBatis 注解开发+逆向(Generator)

    注解开发 最初设计时,MyBatis 是一个 XML 驱动的框架.配置信息是基于 XML 的,而且映射语句也是定义在 XML 中的.随着技术的更新发展,对于开发效率要求也原来越高,特别是一些小型项目; ...

随机推荐

  1. MFC学习(四) 消息机制

    1 消息机制的要点: 消息队列:先进先出 消息循环:通过循环while,不断的从消息队列中取得队首消息,并分发消息. 消息处理:根据不同的消息类型做不同的处理 事件:事件响应函数 2 消息机制 _tW ...

  2. SQL Server2005中文版x64安装29506错误解决办法

    在使用SQL Server 2005简体中版安装时,使用X86(32位操作系统下)安装没有出现任何问题.可是在X64(64位操作系统下)安装过程没有出现问题,可是安装完成后却没有Microsoft S ...

  3. web deploy 部署网站

    一.服务端配置 1. 确保在服务器端(我目前是win server 2012 R2)安装管理服务 安装后服务器会重启, 2)安装webdeploy http://www.iis.net/downloa ...

  4. Informatica PowerCenter下载地址

    https://edelivery.oracle.com/EPD/Download/get_form?egroup_aru_number=12854075

  5. Lambda表达式中使用正则表达式

    某语句如果不用正则表达式: string[] names = { "Tom", "Dick", "Harry", "Mary&qu ...

  6. Django项目部署-01

    1. 安装Python 下载链接:https://www.python.org/getit/ 我这边下载的是3.6.5的版本的执行版本,安装过程中选择自动安装pip 2.安装django pip in ...

  7. Hadoop Serialization -- hadoop序列化详解 (2)

    回顾: 回顾序列化,其实原书的结构很清晰,我截图给出书中的章节结构: 序列化最主要的,最底层的是实现writable接口,wiritable规定读和写的游戏规则 (void write(DataOut ...

  8. day58-activiti 13-搭建web项目环境

    Eclipse的项目的build目录不可被删除,删除了也会被自动创建. 到项目的输出路径才看得到编译好的Java类.Eclipse的视图下是看不见的,因为类路径下的这个目录build不想让你操作,它给 ...

  9. php扩展开发环境搭建

    首先要安装编译php时要的几个扩展库 (1)libxml2,若无php安装一些解析xml的扩展时会提示xml2-config not found sudo apt-get install libxml ...

  10. dataTable写入数据库(大数据写入)

    例1: connectionStr,链接字符串dataTableName, 数据库中对应表名sourceDataTable DataTable 要写入数据库的DataTable字段要和表一致 publ ...