本文将介绍怎样使用 MyBatis 实现对象一对一映射,下面通过一个具体实例来进行介绍。
(1)我们先看看实例涉及的数据表,该实例共涉及了 user(用户表) 和 user_contact(用户联系方式表) 表。假设一个用户只能拥有一个联系方式,则有如下数据:
其中,user.user_id 等于 user_contact.user_id 字段。下面将查询用户联系方式,然后也将用户信息查询出来,只不过用户信息是作为联系方式中的成员变量出现。
(2)根据上面数据表创建 user 和 user_contact 表映射的 JavaBean 实体。代码如下:
a、ORMContact.java
package com.hxstrive.mybatis.orm.demo2; public class ORMContact { private Integer id; private Integer userId; private String usage; private String number; private ORMUser user; // 忽略 getter 和 setter @Override public String toString() { return "ORMContact{" + "id=" + id + ", userId=" + userId + ", usage='" + usage + '\'' + ", number='" + number + '\'' + ", user=" + user + '}'; } }
b、ORMUser.java
package com.hxstrive.mybatis.orm.demo2; import java.util.Date; import java.util.List; public class ORMUser { private Integer userId; private String name; private String sex; private Integer age; private Double salary; private Date borthday; private byte[] face; // 忽略 getter 和 setter @Override public String toString() { return "ORMUser{" + "userId=" + userId + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + ", salary=" + salary + ", borthday=" + borthday + '}'; } }
(3)MyBatis 配置文件 mybatis-cfg.xml 文件代码如下:
<?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> <properties resource="database.properties"/> <environments default="MySqlDatabase" > <environment id="MySqlDatabase" > <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/hxstrive/mybatis/orm/demo2/ORMMapper.xml"/> </mappers> </configuration>
(4)定义 Mapper 接口文件 ORMMapper.java,文件内容如下:
package com.hxstrive.mybatis.orm.demo2; import java.util.List; public interface ORMMapper { /** * 查询所有的联系方式信息 */ List<ORMContact> joinSelect02(); }
(5)定义 Mapper XML 文件 ORMMapper.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.hxstrive.mybatis.orm.demo2.ORMMapper"> <!-- 对象一对一的关联映射,ORMContact对象中有ORMUser对象 --> <resultMap id="ormContact" type="com.hxstrive.mybatis.orm.demo2.ORMContact"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="user_id" jdbcType="INTEGER" property="userId" /> <result column="usage" jdbcType="VARCHAR" property="usage" /> <result column="number" jdbcType="VARCHAR" property="number" /> <!-- 关联映射 --> <association column="n_userid" property="user" javaType="com.hxstrive.mybatis.orm.demo2.ORMUser"> <id column="user_id" jdbcType="INTEGER" property="userId" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="sex" jdbcType="VARCHAR" property="sex" /> <result column="age" jdbcType="INTEGER" property="age" /> <result column="salary" jdbcType="DOUBLE" property="salary" /> <result column="borthday" jdbcType="DATE" property="borthday" /> </association> </resultMap> <select id="joinSelect02" resultMap="ormContact"> select a.`user_id`, a.`name`, a.`sex`, a.`age`, a.`salary`, a.`borthday`, b.`id`, b.`user_id`, b.`usage`, b.`number` from `user` a join `user_contact` b on a.`user_id`=b.`user_id` </select> </mapper>
(6)客户端代码如下:
package com.hxstrive.mybatis.orm.demo2; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; import java.util.List; public class OrmDemo { public static void main(String[] args) throws Exception { String cfgName = "com/hxstrive/mybatis/orm/demo2/mybatis-cfg.xml"; InputStream input = Resources.getResourceAsStream(cfgName); SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlFactory = factoryBuilder.build(input); SqlSession sqlSession = sqlFactory.openSession(true); ORMMapper ormMapper = sqlSession.getMapper(ORMMapper.class); List<ORMContact> contactList = ormMapper.joinSelect02(); for ( ORMContact contact : contactList ) { System.out.println(contact); } } }
运行程序,输出如下结果:
ORMContact{id=1, userId=1, usage='手机号码', number='15787782291', user=ORMUser{userId=1, name='Helen', sex='男', age=28, salary=7800.0, borthday=Wed Jun 04 00:00:00 CST 1902}} ORMContact{id=2, userId=2, usage='家庭号码', number='028-82234543', user=ORMUser{userId=2, name='张小凡', sex='男', age=30, salary=8670.0, borthday=Wed Aug 22 00:00:00 CDT 1990}} ORMContact{id=3, userId=3, usage='工作号码', number='028-78675599', user=ORMUser{userId=3, name='叶星云', sex='女', age=31, salary=6890.0, borthday=Mon Feb 27 00:00:00 CST 1989}}