Mybatis_day4_Mybatis的注解开发
- 这几年来注解开发越来越流行,Mybatis 也可以使用注解开发方式,这样我们就可以减少编写 Mapper 映射文件。
mybatis 的常用注解说明
@Insert
:实现新增@Update
:实现更新@Delete
:实现删除@Select
:实现查询@Result
:实现结果集封装@Results
:可以与@Result 一起使用,封装多个结果集@ResultMap
:实现引用@Results 定义的封装@One
:实现一对一结果集封装@Many
:实现一对多结果集封装@SelectProvider
: 实现动态 SQL 映射@CacheNamespace
:实现注解二级缓存的使用
使用 Mybatis 注解实现基本 CRUD
- 编写实体类
public class User implements Serializable {private Integer userId;
private String userName;
private Date userBirthday;
private String userSex;
private String userAddress;public Integer getUserId() {return userId;
}
public void setUserId(Integer userId) {this.userId = userId;
}
.....
- 注意:此处User的属性名和数据库表的列名不一致。
- 使用注解方式开发持久层接口
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();/**
* 根据 id 查询一个用户
* @param userId
* @return
*/
@Select("select * from user where id = #{uid} ")
@ResultMap("userMap")
User findById(Integer userId);/**
* 保存操作
* @param user
* @return
*/
@Insert("insert into user(username,sex,birthday,address)values(#{username},#{sex},#{birthday},#{address})")
int saveUser(User user);/**
* 更新操作
* @param user
* @return
*/
@Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id =#{id} ")
int updateUser(User user);/**
* 删除用户
* @param userId
* @return
*/
@Delete("delete from user where id = #{uid} ")
int deleteUser(Integer userId);/**
* 查询使用聚合函数
* @return
*/
@Select("select count(*) from user ")
int findTotal();/**
* 模糊查询
* @param name
* @return
*/
@Select("select * from user where username like #{username} ")
List<User> findByName(String name);
}
- 编写 SqlMapConfig 配置文件
<!-- 配置映射信息 -->
<mappers><!-- 配置 dao 接口的位置,它有两种方式
第一种:使用 mapper 标签配置 class 属性
第二种:使用 package 标签,直接指定 dao 接口所在的包
--><package name="cn.myp666.dao"/>
</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="cn.myp666.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")
})
List<User> findAll();/**
* 根据 id 查询一个用户
* @param userId
* @return
*/
@Select("select * from user where id = #{uid} ")
@ResultMap("userMap")
User findById(Integer userId);
}
使用注解实现一对多复杂关系映射
- 编写用户的持久层接口并使用注解配置
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="cn.myp666.dao.IAccountDao.findByUid",fetchType=FetchType.LAZY))})List<User> findAll();
}
@Many:
相当于<collection>
的配置- select 属性:代表将要执行的 sql 语句
- fetchType 属性:代表加载方式,一般如果要延迟加载都设置为
LAZY
的值
- 编写账户的持久层接口并使用注解配置
public interface IAccountDao {/**
* 根据用户 id 查询用户下的所有账户
* * @param userId
* @return
*/
@Select("select * from account where uid = #{uid} ")
List<Account> findByUid(Integer userId);
}
mybatis 基于注解的二级缓存
- 在 SqlMapConfig 中开启二级缓存支持
<!-- 配置二级缓存 -->
<settings>
<!-- 开启二级缓存的支持 --><setting name="cacheEnabled" value="true"/>
</settings>
- 在持久层接口中使用注解配置二级缓存
@CacheNamespace(blocking=true)//mybatis 基于注解方式实现配置二级缓存
public interface IUserDao {}
Mybatis_day4_Mybatis的注解开发相关推荐
- mybatis使用注解开发
mybatis使用注解开发 面向接口编程 在之前我们是通过面向对象编程,但是在真正开发的时候我们会选择面向接口编程. 根本原因 : 解耦 , 可拓展 , 提高复用 , 分层开发中 , 上层不用管具体的 ...
- 使用注解开发SpringMVC详细配置教程
目录 1.使用注解开发SpringMVC 1.新建一个普通的maven项目,添加web支持 2.在pom.xml中导入相关依赖 3.配置web.xml 4.编写SpringMVC配置文件 1. 自动扫 ...
- 【Spring Web MVC】Spring Web MVC 注解开发环境搭建
为什么80%的码农都做不了架构师?>>> 1.创建maven项目 创建一个名为:springwebmvc-first的maven项目 2.添加依赖包 要使用springWebM ...
- AspectJ的注解开发AOP:异常抛出通知的学习
AspectJ的注解开发AOP:异常抛出通知的学习 参考文章: (1)AspectJ的注解开发AOP:异常抛出通知的学习 (2)https://www.cnblogs.com/xiaolaha/p/1 ...
- 【SSM框架系列】Spring IoC(控制反转) DI(依赖注入)注解开发
Spring注解开发 Spring是轻代码重配置的框架,配置比较繁重,会影响开发效率.这个时候可以通过注解开发,注解代替xml配置文件可以简化配置,提高开发效率. Spring原始注解 注解分为原始注 ...
- 07-XML 文件注解开发
目录 注解 一.@Configuration @Import 二.@ComponentScan 三.@bean 1.Config完整代码 2.测试类 不论是 xml 开发或者注解开发都有一个问题是,我 ...
- 011_AOP注解开发
一. Spring的基于ApsectJ的注解的AOP开发 1. 编写目标类并配置 2. 编写切面类并配置 3. 在配置文件中打开注解的AOP开发 4. 在切面类上使用注解@Aspect 5. 前置通知 ...
- SpringMVC学习03之使用注解开发SpringMVC
复习 Spring MVC的特点: 轻量级,简单易学 高效 , 基于请求响应的MVC框架 与Spring兼容性好,无缝结合 约定优于配置 功能强大:RESTful.数据验证.格式化.本地化.主题等 简 ...
- springmvc学习笔记(10)-springmvc注解开发之商品改动功能
springmvc学习笔记(10)-springmvc注解开发之商品改动功能 springmvc学习笔记(10)-springmvc注解开发之商品改动功能 标签: springmvc springmv ...
最新文章
- mysql 电商项目(一)
- Android开发之工厂模式初探
- 8255控制四个双色灯C语言,汇编语言实现通过8255A和4个开关控制实现8个LED灯和8个7位数码管显示指定数字全亮、全灭、从左至右、从右至左跑马灯式点亮...
- TemplateComponent.setContainer Component runAsOwner getStable ID and xml view creation
- [AlwaysOn Availability Groups] 健康模型 Part 2 ——扩展
- quartz mysql 初始化_quartz scheduler 从数据库初始化
- enum 有什么好处_林卡尔|先买地板后装修的三大好处
- 雷电3菊链功能_同轴科技推出5款USB-C全功能数据线,清一色内置同轴线缆
- 网页中加载flash的方法
- 如何编写内联if语句用于打印?
- 不显示负频率的部分,坐标轴转换为频率,幅值量纲还原改善频谱图以及功率谱和对数功率谱
- c语言RePutDate用法,C语言 栈的使用
- 算法笔记_面试题_11.正则表达式匹配
- 安装Powerdesigner16.5
- 5G关键技术之NFV
- HDU 5857 Median(水~)
- R 绘制带有数字标签的多分类柱状图
- Linux:syscall: entry_SYSCALL_64_after_hwframe
- 那些年我们没能bypass的xss filter[from wooyun]
- 1251:丛林中的路
热门文章
- Qt QDataTime QString 两个类的使用
- hexo博客出现“Cannot GET/xxxx”的错误
- 基于.NET Socket API 通信的综合应用
- Git pull 强制覆盖本地文件
- python中print后面加逗号
- [HDOJ]1018. Big Number
- 最详细的git( Github和Gitee )入门使用(上传与克隆)
- JS:ES6-4 简化对象与箭头函数
- LeetCode(1021)——删除最外层的括号(JavaScript)
- 计算机网络学习笔记(8. 报文交换与分组交换②)