m'ybatis 一对一 一对多 配置详解
javabean:
package com.me.model; import java.io.Serializable;
import java.util.Date;
import java.util.List; public class User implements Serializable { /**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
//一對一 放入對象
private Morder morder;
//一對多 放入對象集合
private List<Home> homeList; public List<Home> getHomeList() {
return homeList;
}
public void setHomeList(List<Home> homeList) {
this.homeList = homeList;
}
public Morder getMorder() {
return morder;
}
public void setMorder(Morder morder) {
this.morder = morder;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public int getId() {
return id;
}
public void setId(int 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
+ ", morder=" + morder + ", homeList=" + homeList + "]";
} }
package com.me.model; public class Morder { private int orderId;
private String orderName;
private String orderMessage;
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public String getOrderMessage() {
return orderMessage;
}
public void setOrderMessage(String orderMessage) {
this.orderMessage = orderMessage;
} }
package com.me.model; public class Home { private int homeId;
private String homeName;
public int getHomeId() {
return homeId;
}
public void setHomeId(int homeId) {
this.homeId = homeId;
}
public String getHomeName() {
return homeName;
}
public void setHomeName(String homeName) {
this.homeName = homeName;
} }
mapper.xml 代码
<!-- collection :collection属性的值有三个分别是list、array、map三种, 分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array
item : 表示在迭代过程中每一个元素的别名 index :表示在迭代过程中每次迭代到的位置(下标) open :前缀 close :后缀 separator
:分隔符,表示迭代时每个元素之间以什么分隔 -->
<delete id="deleteSome">
delete from user where id in
<foreach collection="list" item="id" index="index" open="("
close=")" separator=",">
#{id}
</foreach>
</delete> <!-- 关联查询 -->
<!-- 關聯查詢 1對1 -->
<select id="selectGL" resultMap="userRsultMap">
select * from user u,morder m
WHERE u.oid=m.order_id
</select>
<resultMap type="com.me.model.User" id="userRsultMap">
<id property="id" column="id" />
<result column="username" property="username" />
<result column="birthday" property="birthday" />
<result column="sex" property="sex" />
<result column="address" property="address" /> <association property="morder" javaType="com.me.model.Morder">
<id column="order_id" property="orderId" />
<result column="order_name" property="orderName" />
<result column="order_message" property="orderMessage" />
</association>
</resultMap>
<!-- 關聯查詢 1對多 -->
<select id="selectGL2" resultMap="userRsultMap2">
select * from user u,home h where u.hid=h.home_id;
</select>
<resultMap type="com.me.model.User" id="userRsultMap2">
<id property="id" column="id" />
<result column="username" property="username" />
<result column="birthday" property="birthday" />
<result column="sex" property="sex" />
<result column="address" property="address" /> <collection property="homeList" ofType="com.me.model.Home">
<id property="homeId" column="home_id" />
<result property="homeName" column="home_name" />
</collection>
</resultMap>
图文解释:
测试:
//關聯查詢 1 to 多
@Test
public void selectGL2(){
try {
inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入MyBatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// 通过工厂得到SqlSession
sqlSession = sqlSessionFactory.openSession();
List<User> list = sqlSession.selectList("test.selectGL2");
for (User u : list) {
System.err.println(u.getHomeList().get(0).getHomeName());
} } catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
sqlSession.close();
}
}
结果:
22:47:17.005 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.215 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection
22:47:17.420 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 518522822.
22:47:17.420 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.421 [main] DEBUG test.selectGL2 - ==> Preparing: select * from user u,home h where u.hid=h.home_id;
22:47:17.444 [main] DEBUG test.selectGL2 - ==> Parameters:
22:47:17.461 [main] DEBUG test.selectGL2 - <== Total: 4
sasadasd
22:47:17.462 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.462 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.463 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 518522822 to pool.
更多可以参考:https://www.cnblogs.com/xdp-gacl/p/4264440.html
m'ybatis 一对一 一对多 配置详解的更多相关文章
- ubuntu apache2配置详解(含虚拟主机配置方法)
ubuntu apache2配置详解(含虚拟主机配置方法) 在Windows下,Apache的配置文件通常只有一个,就是httpd.conf.但我在Ubuntu Linux上用apt-get inst ...
- 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解
注意:这里不会讲到nginx流媒体模块如何安装的问题,只研究rtmp,hls直播和录制相关的nginx服务器配置文件的详细用法和说明.可以对照这些命令详解配置nginx -rtmp服务 一.nginx ...
- Log4j配置详解(转)
一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...
- logback 常用配置详解<appender>
logback 常用配置详解 <appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的 ...
- [转]阿里巴巴数据库连接池 druid配置详解
一.背景 java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色 ...
- libCURL开源库在VS2010环境下编译安装,配置详解
libCURL开源库在VS2010环境下编译安装,配置详解 转自:http://my.oschina.net/u/1420791/blog/198247 http://blog.csdn.net/su ...
- logback配置详解3<filter>
logback 常用配置详解(三) <filter> <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之一.返回DENY ...
- logback配置详解2<appender>
logback 常用配置详解(二) <appender> <appender>: <appender>是<configuration>的子节点,是负责写 ...
- log4j.properties配置详解
1.Loggers Loggers组件在此系统中被分为五个级别:DEBUG.INFO.WARN.ERROR和FATAL.这五个级别是有顺序的,DEBUG < INFO < WARN < ...
随机推荐
- Java复习10.Servlet编程
Java复习10. Servlet编程知识 20131008 前言: 之前在大三下的时候,学习了一个月的JSP和Servlet知识,但是没有什么项目经验,把JSP Web开发学习实录看了前面几张,后面 ...
- centos 7 nginx 远程无法访问的原因
1.购买的阿里云需要添加安全组 80端口就可以用了 2.检查防火墙是否关闭 systemctl stop firewalld 这点很重要 我就是查了一天才发现centos7 防火墙开着 难怪几把搞 如 ...
- poj1273&&hdu1532
题解: 双倍经验 dinic模板题 然后ff过不了... 代码: #include<cstdio> #include<cstring> #include<algorith ...
- json 解析double类型多出几位小数,利用NSDecimalNumber修复 , 服务器返回的数据,用数据类型接收(int,NSNumber)?
后台返回float类型数据8.2,实际用AFN的json解析后得到8.1999999999,该怎么处理呢? 开发中处理处理价格金额问题, 后台经常返回float.double类型, 打印或转成NSS ...
- 浅谈title属性与alt属性
XHTML是CSS布局的基础,webjx.com一直强调XHTML知识的学习,重视语义和文档的结构.title 和alt 属性,给我最直观的感受就是,可以提高文档的适应性,并合理提高关键词密度.在XH ...
- Linux sed命令操作 删除文件每一行的前k个字符、在文本的行尾或行首添加字符
删除文件每一行的前k个字符 $ cat tmp.txt # 删除每行第一个字符 $ sed 's/.//' tmp.txt # 删除每行前两个字符 $ sed 's/..//' tmp.txt # 删 ...
- android代码常识
查看当前android代码版本号:build/core/version_defaults.mk---->查找platform_version android源码在线阅读网址 http://and ...
- android Camera模块分析
Android Camera Module Architecture and Bottom layer communication mechanism ----------- ...
- Android 编程 AMapLocationClientOption 类中的 setNeedAddress 方法用处 (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)
最近在用高德地图来写Android App, 其中有一些 方法是不太理解的,这里写一下 对 高德地图 com.amap.api.location.AMapLocationClientOption ...
- python pass关键字神奇吗
参考文献:http://blog.sina.com.cn/s/blog_76e94d210100vz3e.html 1.空语句 do nothing2.保证格式完整3.保证语义完整 好吧!它什么也没干 ...