mybatis学习:mybatis注解开发一对多
实体类User:
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
//一对多关系映射:一个用户对应多个账户
private List<Account> accounts;
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
dao层:
IUserDao:
@Select("select * from user")
@Results(id = "userMap",value = {
@Result(id = true,column = "id",property = "id"),
@Result(column = "username",property = "username"),
@Result(column = "address",property = "address"),
@Result(column = "sex",property = "sex"),
@Result(column = "birthday",property = "birthday"),
@Result(property = "accounts",column = "id",many = @Many(select = "cn.flypig666.dao.IAccountDao.findAccountByUid",
fetchType = FetchType.EAGER))
})
List<User> findAll();
IAccountDao:
/**
* 根据用户id查询账户信息
* @param userId
* @return
*/
@Select("select * from account where uid = #{uid}")
List<Account> findAccountByUid(Integer userId);
}
mybatis学习:mybatis注解开发一对多的更多相关文章
- mybatis学习——使用注解开发
前言: 一个语句既可以通过 XML 定义,也可以通过注解定义.不过,由于 Java 注解的一些限制以及某些 MyBatis 映射的复杂性,要使用大多数高级映射(比如:嵌套联合映射),仍然需要使用 XM ...
- Mybatis学习笔记-注解开发
面向接口编程 根本原因:[解耦],[可拓展],[更高规范性] 接口类型: abstract class interface 使用注解开发 简单语句可用注解开发(直接查询,列名与属性名相同) 本质:反射 ...
- MyBatis框架之注解开发
MyBatis注解开发 @Insert注解注解属性value:写入SQL语句 @Options注解实现添加新数据的主键封装注解属性useGeneratedKeys:使用生成的主键,配置为truekey ...
- MyBatis:使用注解开发
面向接口编程 大家之前都学过面向对象编程,也学习过接口,但在真正的开发中,很多时候我们会选择面向接口编程 根本原因 : 解耦 , 可拓展 , 提高复用 , 分层开发中 , 上层不用管具体的实现 , 大 ...
- 【转】Mybatis学习---MyBatis知识、原始Dao开发和mapper代理开发
[原文]https://www.toutiao.com/i6594610137560777223/ 一.什么是MyBatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及 ...
- 8、MyBatis之使用注解开发
9.使用注解开发 mybatis最初配置信息是基于 XML ,映射语句(SQL)也是定义在 XML 中的.而到MyBatis 3提供了新的基于注解的配置.不幸的是,Java 注解的的表达力和灵活性十分 ...
- MyBatis学习--mybatis开发dao的方法
简介 使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法. 主要概念介绍: MyBatis中进行Dao开发时候有几个重要的类,它们是SqlSessionFac ...
- Mybatis学习---Mybatis分页插件 - PageHelper
1. Mybatis分页插件 - PageHelper说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,S ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_7 Mybatis注解开发一对多的查询配置
一对多的配置,一个用户对应多个账户 需要在Accout里面增加根据用户的uid查询的方法 在user里面指定子一对多的查询配置 换行显示 测试 把这里注销掉.测试延迟加载,代码注释掉后,延迟加载就没有 ...
随机推荐
- 7_1.springboot2.x启动配置原理_1.创建SpringApplication对象
环境准备 springboot2.1.9.idea2019. pom.xml 解析 几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContex ...
- 靖烜小哥哥之mybatis总结
MyBatis是一个半自动映射的框架.“半自动”是相对于Hibernate全表映射而言的,MyBatis需要手动匹配提供POJO.SQL和映射关系,而Hibernate只需提供POJO和映射关系即可. ...
- selenium基础(元素定位)
selenium的帮助文档: https://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions 目前支 ...
- 18-3-bind
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue 计算属性实现过滤关键词
效果 html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...
- Python全栈开发:冒泡排序
#!/usr/bin/env python # -*- coding;utf-8 -*- """ 第一次对比:找到最大值,放到最后 对比是两两对比,对比的两个数组合共有l ...
- input光标使用caret-color改变颜色
本文转载自:https://www.zhangxinxu.com/wordpress/2018/01/css-caret-color-first-line/ CSS caret-color属性可以改变 ...
- 非旋Treap及其可持久化
平衡树这种东西,我只会splay.splay比较好理解,并且好打,操作方便. 我以前学过SBT,但并不是很理解,所以就忘了怎么打了. 许多用平衡树的问题其实可以用线段树来解决,我们真正打平衡树的时候一 ...
- 廖雪峰Java12maven基础-1maven入门-3构建流程
maven是一个Java项目管理和构建工具: 标准化项目结构 标准化构建流程(编译.打包.发布) 依赖管理 Maven的构建流程 clean 删除所有编译生成的文件 compile 编译源码.编译测试 ...
- Redis学习目录
目录 持续更新... Redis简介 Redis安装及基本配置 Redis持久化 Redis开发及管理实战 Redis高可用及集群 Redis多API开发