One of the latest MyBatis feature is the ability to use Annotations or XML to do One-to-One or One-to-Many queries. Let’s start
with an example, as usual im using PostgreSQL, Netbeans 6.9 and MyBatis 3.0.2.

First is a simple database with 2 different tables,

CREATE DATABASE test
CREATE TABLE master
(
nama CHARACTER VARYING(30) NOT NULL,
usia SMALLINT,
CONSTRAINT idnama PRIMARY KEY (nama)
)
CREATE TABLE contoh
(
id INTEGER NOT NULL,
nama CHARACTER VARYING(30),
alamat CHARACTER VARYING(50),
CONSTRAINT id PRIMARY KEY (id)
)
ALTER TABLE
contoh ADD CONSTRAINT master FOREIGN KEY (nama) REFERENCES master (nama)
ON DELETE CASCADE
ON UPDATE CASCADE insert into master (nama, usia) values ('pepe', 17);
insert into master (nama, usia) values ('bubu', 19); insert into contoh (id, nama, alamat) values (1, 'bubu', 'Tangerang');
insert into contoh (id, nama, alamat) values (2, 'pepe', 'Jakarta');
insert into contoh (id, nama, alamat) values (3, 'bubu', 'Singapore');
insert into contoh (id, nama, alamat) values (4, 'pepe', 'Kuburan');

My java bean, as my java object representation of my database tables,

package com.edw.bean;

import java.util.List;

public class Master {

    private String nama;
private Short usia;
private List<Contoh> contohs; // other setters and getters @Override
public String toString() {
return "Master{" + "nama=" + nama + " usia=" + usia + " contohs=" + contohs + '}';
}
}
package com.edw.bean;

public class Contoh {

    private Integer id;
private String nama;
private String alamat;
private Master master; // other setters and getters @Override
public String toString() {
return "Contoh{" + "id=" + id + " nama=" + nama + " alamat=" + alamat + " master=" + master + '}';
}
}

My XML files for table Contoh and Master queries, please take a look at association tags and collection tags. The association
element deals with a has-one type
relationship. While collection deals with a has-lots-of type
relationship.

<?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.edw.mapper.MasterMapper" >
<!-- result maps -->
<resultMap id="ResultMap" type="com.edw.bean.Master" >
<id column="nama" property="nama" />
<result column="usia" property="usia" />
<!-- collections of Contoh -->
<collection property="contohs" ofType="com.edw.bean.Contoh"
column="nama" select="selectContohFromMaster" />
</resultMap> <!-- one to many select -->
<select id="selectUsingXML" resultMap="ResultMap" parameterType="java.lang.String" >
SELECT
master.nama,
master.usia
FROM
test.master
WHERE master.nama = #{nama}
</select> <select id="selectContohFromMaster"
parameterType="java.lang.String"
resultType="com.edw.bean.Contoh">
SELECT
id,
nama,
alamat
FROM
test.contoh
WHERE
nama = #{nama}
</select>
</mapper>
<?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.edw.mapper.ContohMapper" >
<!-- result maps -->
<resultMap id="BaseResultMap" type="com.edw.bean.Contoh" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="nama" property="nama" jdbcType="VARCHAR" />
<result column="alamat" property="alamat" jdbcType="VARCHAR" /> <!-- one to one -->
<association property="master" column="nama" javaType="com.edw.bean.Master"
select="selectMasterFromContoh"/>
</resultMap> <!-- one to one select -->
<select id="selectUsingXML" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
SELECT
contoh.id,
contoh.nama,
contoh.alamat
FROM
test.contoh
WHERE
id = #{id,jdbcType=INTEGER}
</select> <select id="selectMasterFromContoh"
parameterType="java.lang.String"
resultType="com.edw.bean.Master">
SELECT
master.nama,
master.usia
FROM
test.master
WHERE
nama = #{nama}
</select>
</mapper>

This is my main xml configuration to handle my database connections,

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/test"/>
<property name="username" value="postgres"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/edw/xml/ContohMapper.xml" />
<mapper resource="com/edw/xml/MasterMapper.xml" />
</mappers>
</configuration>

and a java class to load my XML files

package com.edw.config;

import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisSqlSessionFactory { protected static final SqlSessionFactory FACTORY; static {
try {
Reader reader = Resources.getResourceAsReader("com/edw/xml/Configuration.xml");
FACTORY = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e){
throw new RuntimeException("Fatal Error. Cause: " + e, e);
}
} public static SqlSessionFactory getSqlSessionFactory() {
return FACTORY;
}
}

These are my mapper interfaces, i put my annotation queries here. Please take a note at @Many and @One annotations.
MyBatis use @One to map a single property value of a complex type, while @Many for mapping a collection property of a complex types.

package com.edw.mapper;

import com.edw.bean.Contoh;
import com.edw.bean.Master;
import java.util.List; import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select; public interface MasterMapper { Master selectUsingXML(String nama); /*
* one to many Select.
*/
@Select("SELECT master.nama, master.usia FROM test.master WHERE master.nama = #{nama}")
@Results(value = {
@Result(property="nama", column="nama"),
@Result(property="usia", column="usia"),
@Result(property="contohs", javaType=List.class, column="nama",
many=@Many(select="getContohs"))
})
Master selectUsingAnnotations(String nama); @Select("SELECT contoh.id, contoh.nama, contoh.alamat FROM test.contoh WHERE contoh.nama = #{nama}")
List<Contoh> getContohs(String nama);
}
package com.edw.mapper;

import com.edw.bean.Contoh;
import com.edw.bean.Master;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select; public interface ContohMapper { Contoh selectUsingXML(Integer nama); /*
* one to one Select.
*/
@Select("SELECT contoh.id, contoh.nama, contoh.alamat FROM test.contoh WHERE contoh.id = #{id}")
@Results(value = {
@Result(property = "nama", column = "nama"),
@Result(property = "alamat", column = "alamat"),
@Result(property = "master", column = "nama", one=@One(select = "getMaster"))
})
Contoh selectUsingAnnotations(Integer id); @Select("SELECT master.nama, master.usia FROM test.master WHERE master.nama = #{nama}")
Master getMaster(String nama);
}

Here is my main java class

package com.edw.main;

import com.edw.bean.Contoh;
import com.edw.bean.Master;
import com.edw.config.MyBatisSqlSessionFactory;
import com.edw.mapper.ContohMapper;
import com.edw.mapper.MasterMapper;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger; public class Main { private static Logger logger = Logger.getLogger(Main.class); public Main() {
} private void execute() throws Exception{
SqlSession session = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();
try {
MasterMapper masterMapper = session.getMapper(MasterMapper.class); // using XML queries ------------------------------
Master master = masterMapper.selectUsingXML("pepe");
logger.debug(master); List<Contoh> contohs = master.getContohs();
for (Contoh contoh : contohs) {
logger.debug(contoh);
} // using annotation queries ------------------------------
master = masterMapper.selectUsingAnnotations("pepe");
logger.debug(master); List<Contoh> contohs2 = master.getContohs();
for (Contoh contoh : contohs2) {
logger.debug(contoh);
} // using XML queries ------------------------------
ContohMapper contohMapper = session.getMapper(ContohMapper.class);
Contoh contoh = contohMapper.selectUsingXML(1);
logger.debug(contoh.getMaster()); // using annotation queries ------------------------------
contoh = contohMapper.selectUsingAnnotations(1);
logger.debug(contoh); session.commit();
} finally {
session.close();
}
} public static void main(String[] args) throws Exception {
try {
Main main = new Main();
main.execute();
} catch (Exception exception) {
logger.error(exception.getMessage(), exception);
}
}
}

And this is what happen on my java console, im using log4j to do all the loggings.

DEBUG java.sql.Connection:27 - ooo Connection Opened
DEBUG java.sql.PreparedStatement:27 - ==> Executing: SELECT master.nama, master.usia FROM test.master WHERE master.nama = ?
DEBUG java.sql.PreparedStatement:27 - ==> Parameters: pepe(String)
DEBUG java.sql.ResultSet:27 - <== Columns: nama, usia
DEBUG java.sql.ResultSet:27 - <== Row: pepe, 17
DEBUG java.sql.PreparedStatement:27 - ==> Executing: SELECT id, nama, alamat FROM test.contoh WHERE nama = ?
DEBUG java.sql.PreparedStatement:27 - ==> Parameters: pepe(String)
DEBUG java.sql.ResultSet:27 - <== Columns: id, nama, alamat
DEBUG java.sql.ResultSet:27 - <== Row: 2, pepe, Jakarta
DEBUG java.sql.ResultSet:27 - <== Row: 4, pepe, Kuburan
DEBUG com.edw.main.Main:32 - Master{nama=pepe usia=17 contohs=[Contoh{id=2 nama=pepe alamat=Jakarta master=null}, Contoh{id=4 nama=pepe alamat=Kuburan master=null}]}
DEBUG com.edw.main.Main:36 - Contoh{id=2 nama=pepe alamat=Jakarta master=null}
DEBUG com.edw.main.Main:36 - Contoh{id=4 nama=pepe alamat=Kuburan master=null}
DEBUG java.sql.PreparedStatement:27 - ==> Executing: SELECT master.nama, master.usia FROM test.master WHERE master.nama = ?
DEBUG java.sql.PreparedStatement:27 - ==> Parameters: pepe(String)
DEBUG java.sql.ResultSet:27 - <== Columns: nama, usia
DEBUG java.sql.ResultSet:27 - <== Row: pepe, 17
DEBUG java.sql.PreparedStatement:27 - ==> Executing: SELECT contoh.id, contoh.nama, contoh.alamat FROM test.contoh WHERE contoh.nama = ?
DEBUG java.sql.PreparedStatement:27 - ==> Parameters: pepe(String)
DEBUG java.sql.ResultSet:27 - <== Columns: id, nama, alamat
DEBUG java.sql.ResultSet:27 - <== Row: 2, pepe, Jakarta
DEBUG java.sql.ResultSet:27 - <== Row: 4, pepe, Kuburan
DEBUG com.edw.main.Main:41 - Master{nama=pepe usia=17 contohs=[Contoh{id=2 nama=pepe alamat=Jakarta master=null}, Contoh{id=4 nama=pepe alamat=Kuburan master=null}]}
DEBUG com.edw.main.Main:45 - Contoh{id=2 nama=pepe alamat=Jakarta master=null}
DEBUG com.edw.main.Main:45 - Contoh{id=4 nama=pepe alamat=Kuburan master=null}
DEBUG java.sql.PreparedStatement:27 - ==> Executing: SELECT contoh.id, contoh.nama, contoh.alamat FROM test.contoh WHERE id = ?
DEBUG java.sql.PreparedStatement:27 - ==> Parameters: 1(Integer)
DEBUG java.sql.ResultSet:27 - <== Columns: id, nama, alamat
DEBUG java.sql.ResultSet:27 - <== Row: 1, bubu, Tangerang
DEBUG java.sql.PreparedStatement:27 - ==> Executing: SELECT master.nama, master.usia FROM test.master WHERE nama = ?
DEBUG java.sql.PreparedStatement:27 - ==> Parameters: bubu(String)
DEBUG java.sql.ResultSet:27 - <== Columns: nama, usia
DEBUG java.sql.ResultSet:27 - <== Row: bubu, 19
DEBUG com.edw.main.Main:51 - Master{nama=bubu usia=19 contohs=null}
DEBUG java.sql.PreparedStatement:27 - ==> Executing: SELECT contoh.id, contoh.nama, contoh.alamat FROM test.contoh WHERE contoh.id = ?
DEBUG java.sql.PreparedStatement:27 - ==> Parameters: 1(Integer)
DEBUG java.sql.ResultSet:27 - <== Columns: id, nama, alamat
DEBUG java.sql.ResultSet:27 - <== Row: 1, bubu, Tangerang
DEBUG java.sql.PreparedStatement:27 - ==> Executing: SELECT master.nama, master.usia FROM test.master WHERE master.nama = ?
DEBUG java.sql.PreparedStatement:27 - ==> Parameters: bubu(String)
DEBUG java.sql.ResultSet:27 - <== Columns: nama, usia
DEBUG java.sql.ResultSet:27 - <== Row: bubu, 19
DEBUG com.edw.main.Main:55 - Contoh{id=1 nama=bubu alamat=Tangerang master=Master{nama=bubu usia=19 contohs=null}}
DEBUG java.sql.Connection:27 - xxx Connection Closed

my
log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG,stdout # Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %c:%L - %m%n

Beginning MyBatis 3 Part 2 : How to Handle One-to-Many and One-to-One Selects的更多相关文章

  1. MyBatis(3.2.3) - Configuring MyBatis using XML, typeHandlers

    As discussed in the previous chapter, MyBatis simplifies the persistent logic implementation by abst ...

  2. MyBatis学习总结(一)——ORM概要与MyBatis快速起步

    程序员应该将核心关注点放在业务上,而不应该将时间过多的浪费在CRUD中,多数的ORM框架都把增加.修改与删除做得非常不错了,然后数据库中查询无疑是使用频次最高.复杂度大.与性能密切相关的操作,我们希望 ...

  3. [翻译.每月一译.每日一段]Exploring Fonts with DirectWrite and Modern C++

    Windows with C++ Exploring Fonts with DirectWrite and Modern C++ Kenny Kerr DirectWrite is an incred ...

  4. js实现省市区联动

    先来看看效果图吧,嘻嘻~~~~~~~~~~~~~~~~~~~· 代码在下面: 示例一: html: <!DOCTYPE html> <html> <head> &l ...

  5. 我为什么放弃使用MyBatis3的Mapper注解

    最近在使用MyBatis3做项目.在使用注解实现Mapper的时候遇到了比较奇葩的问题:在实现数据的batch insert的时候总是报错.好不容易可以正常插入了,但是又不能返回自增的主键id到实体b ...

  6. 我为什么放弃使用mybatis3的mapper注解了

    原文链接 最近在使用MyBatis3做项目.在使用注解实现Mapper的时候遇到了比较奇葩的问题:在实现数据的batch insert的时候总是报错.好不容易可以正常插入了,但是又不能返回自增的主键i ...

  7. select2插件改造之设置自定义选项 源码

    改造特性: 适应业务需要,选项里面包含“其他”其它”,可以点击填写并设置自定义选项 效果图: 具体代码不做阐述,如有类似需求,请私信.主要源码: /* Copyright 2012 Igor Vayn ...

  8. Building microservices with ASP.NET Core (without MVC)(转)

    There are several reasons why it makes sense to build super-lightweight HTTP services (or, despite a ...

  9. android surfaView surfaHolder video 播放

    主文件 package cn.com.sxp;import android.app.Activity;import android.media.AudioManager;import android. ...

随机推荐

  1. spring的常用配置

    bean.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  2. Python学习之一

    运行Python的步骤 1 加载内存 2 词法分析  3 语法分析 4编译字节码=>机器码  5运行   11:03:49 2016-01-11

  3. poj 3286 统计0的个数

    #include <iostream> using namespace std; long long p; ]; long long solve(long long n){ ; ;i< ...

  4. QT 强止杀进程

    bool KillProcess(QString ProcessName){  bool result = false; QString str1; HANDLE hSnapShot = Create ...

  5. 安装ubuntu时的注意事项----个人小总结

    今天重装了一次ubuntu,以前是别人帮我装的,而这次是我自己照着网上教程装的. 这个教程还是挺不错的,我就是照着这个装成功的 http://jingyan.baidu.com/article/60c ...

  6. 权限管理之基于ACL的实现:自定义JSTL函数实现即时认证

    实现即时认证(即只有拥有相应的权限,才能做相应的操作) 经常用在,在JSP页面上,调用JSTL自定义函数做判断,显示相应的菜单或者功能按钮,比如只有管理员登陆时才显示“删除”按钮,从而完成权限的即时认 ...

  7. .Net之路(十三)数据库导出到EXCEL

    .NET中导出到Office文档(word,excel)有我理解的两种方法.一种是将导出的文件存放在server某个目录以下,利用response输出到浏览器地址栏,直接打开:还有直接利用javasc ...

  8. 一入python深似海--浅拷贝与深拷贝

    python中有一个模块copy,deepcopy函数用于深拷贝,copy函数用于浅拷贝. 要理解浅拷贝,必须先弄清楚python中的引用. 引用 Python中一切都是对象,变量中存放的是对象的引用 ...

  9. switch语句:适用于一个条件有多个分支的情况---分支语句

    例1: 客服选择功能,然后按按键 Console.WriteLine("查花费请按1,查余额请按2,查流量请按3,办理业务请按4,宽带请按5,人工服务请按6,集团业务请按7"); ...

  10. iOS 最新UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...