mybatis一对多xml配置

1.一的配置

<resultMap id="accListResultMap" type="com.hex.dop.pms.model.PMAccount">
<result column="PK_ID" property="pkId" jdbcType="VARCHAR"/>
<result column="ACCOUNT" property="account" jdbcType="VARCHAR"/>
<result column="ACCOUNTBANK" property="accountBank" jdbcType="VARCHAR"/>
<result column="ACCOUNTBANKNAME" property="accountBankName" jdbcType="VARCHAR"/>

...
<result column="APPLYDATE" property="applyDate" jdbcType="TIMESTAMP"/>
<result column="STATUS" property="status" jdbcType="VARCHAR"/>
<collection property="expresss" column="PK_ID" select="getExpress"/>
</resultMap>

2.多的配置

<resultMap id="expressListResultMap" type="com.hex.dop.pms.model.Express">
<result column="ID" property="id" jdbcType="VARCHAR"></result>
<result column="SENDER" property="sender" jdbcType="VARCHAR"></result>
<result column="CONSIGNEE" property="consignee" jdbcType="VARCHAR"></result>
<result column="REMARK" property="remark" jdbcType="VARCHAR"></result>
<result column="UPONTIME" property="uponTime" jdbcType="TIMESTAMP"></result>
<result column="EXPRESSNUMBER" property="expressNumber" jdbcType="VARCHAR"></result>
<result column="EXPRESSCOMPANY" property="expressCompany" jdbcType="VARCHAR"></result>
</resultMap>

3.上面的collection标签保证了PK_ID相关联

<!--根据id查询快递信息-->
<select id="getExpress" parameterType="java.util.Map" resultMap="expressListResultMap">
select E.ID,E.SENDER,E.CONSIGNEE,E.REMARK,E.UPONTIME,E.EXPRESSNUMBER,E.EXPRESSCOMPANY
from SYW_ACPMS_EXPRESS E
where E.PK_ID=#{pkId}
</select>

4.PMAccount.java

private String pkId;    //唯一编号

private List<Express> expresss = null;//每一个账户对应有多个快递

public List<Express> getExpresss() {
if(expresss==null){//如果为空,返回一个空的集合
return new ArrayList<Express>();
}
return expresss;
}

public void setExpresss(List<Express> expresss) {
this.expresss = expresss;
}

Express.java

private   String pkId;//账户主键,建立逻辑上的连接关系

5.control层:

/*
* 根据id查出快递信息返回
* */
@RequestMapping(value ="/express")
@ResponseBody
public Map getAccExpress(@RequestBody PMAccount acc){
Map map = new HashMap();
try{
acc = this.accountManagerService.getAcc(acc.getPkId());//将account传入,查询快递信息
List list = acc.getExpresss();//获得快递集合
map.put("data",list);//map返回的是数组,所以必须在前台使用encode都的对象[‘data‘]才行
}catch(Exception e){
e.printStackTrace();
}
return map;
}

service层:

public PMAccount getAcc(String id){
return this.pmAccountMapper.getAcc(id);
}

dao层:

public PMAccount getAcc(String id){
Map map = new HashMap<String,String>();
map.put("pkId", id);//这里是将前台传入的值放入map中
PMAccount acc = (PMAccount)sqlSessionTemplate.selectList("com.hex.dop.pms.dao.acc.PMAccountMapper.getAcc", map).get(0);
return acc;
}

control层直到调用dao层后,返回出查询的账户对象,然后账户对象中获得快递对象(然后传递pkid,自动调用collection标签中select中的语句,都是collection标签的作用),这样便获得查询出的快递对象,当然还需要acc.getExpresss()从账户对象中取出来。

时间: 2024-08-18 12:09:09

mybatis一对多xml配置的相关文章

Spring 整合hibernate和mybatis的 applicationContext.xml的配置

Spring整合hibernate的applicationContext.xml配置 1.注解方式的实现 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x

MyBatis 源码分析——配置信息

MyBatis框架的启动前期需要加载相关的XML配置信息.从官网上我们可以了解到他具有十几个节点.其中笔者认为比较重要的节点是settings节点.properties节点.environments节点. transactionManager节点.至于相关的作用到官网看过一篇都会明白. MyBatis框架的XML配置到底有几个节点,这个必须去查看一下相关的DTD文件才行.DTA文件就在包名为org.apache.ibatis.builder.xml的下面.由于显示的问题笔者只复制出部分来. <?

MyBatis一对多和多对多xml配置

MyBatis一对多和多对多xml配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ktcx.

Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器

关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务器所提供的SQL语句的巨大威力.与此同时,MyBaits消除了书写大量冗余代码的痛苦,它使使用SQL更容易. 在代码里直接嵌套SQL语句是很差的编码实践,并且维护起来困难.MyBaits使用了映射器配置文件或注解来配置SQL语句.在本章中,我们会看到具体怎样使用映射器配置文件来配置映射SQL语句.

笔记:MyBatis XML配置详解

MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 properties 属性 settings 设置 typeAliases 类型命名 typeHandlers 类型处理器 objectFactory 对象工厂 plugins 插件 environments 环境 environment 环境变量 transactionManager 事务管理器 dataSource

mybatis使用注解替代xml配置,动态生成Sql

mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectProvider来动态生成sql. 典型的使用场景 1. 无参数@SelectProvide方法在Mapper接口方法上和@SelectProvide指定类方法上,均无参数:UserMapper.java: 1     @SelectProvider(type = SqlProvider.class, 

MyBatis之xml配置配置文件(一)(xml配置文件)

一.properties标签 <properties resource="jdbc.properties"></properties> 使用properties标签来引入外部文件. jdbc.properties: jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///mybatis jdbc.username=root jdbc.password=123 使用外部文件的值(使用${}来引用): <

Mybatis最入门---代码自动生成(generatorConfig.xml配置)

第一种方式:通过Main方法执行配置文件. ------------------------------------------------------------------------------------------------------------------------------------- 1.创建本文我们将使用的工程Mybatis13,工程结构图如下:[重点文件我们给出,其他配置文件请读者参考前文工程] 2.修改jdbc.properties文件,具体内容如下: jdbc.

使用 XML 配置 MyBatis

构建 SqlSessionFactory 最常见的方式是基于 XML 配置(的构造方式).下面的 mybatis-config.xml 展示了一个 典型的 MyBatis 配置文件的样子: XML Code <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "