接着上一篇的进阶教程,继续学习Mybatis的一些常用用法。以便我们更好使用Mybatis。
知识点汇总
数据表一对一association
现实中,有时候我们的模型数据,需要结合其它模型,也就是数据库表与表之间的关联。比如说商品表,与优惠券表,假如一个商品对应一个优惠券。
1、数据表

商品表信息

优惠券表信息

商品表与优惠券表内容
2、Mybatis程序
2.1 新建Good.java模型,Coupon.java模型,GoodMapper接口,CouponMapper接口,再新建GoodMapper与CouponMapper的xml文件,最终目录结构接着上次如下。

项目目录结构
部分文件内容:
public class Good {
int id;
String good_name;
String coupon_id;
Coupon mCoupon;
List<Coupon> mCouponList;
public Good() { //构造器
}
public class Coupon {
int id;
String coupon_url;
public Coupon() {
}
注意,我们在Resource目录下新建的是me\aihe\dao目录
mkdir -pv src\main\resources\ me\aihe\dao
两种方法
方法一
3.1 使用分步查询
// GoodMapper接口文件新建方法
Good selectGood(@Param("id") Integer id);
// GoodMapper.xml文件
<resultMap id="goodMap" type="Good">
<id column="id" property="id" />
<result property="good_name" column="good_name" />
<result property="coupon_id" column="coupon_id" />
<association property="mCoupon" column="coupon_id" select="me.aihe.dao.CouponMapper.selectCoupon" />
</resultMap>
<select id="selectGood" resultMap="goodMap">
SELECT * FROM Good WHERE id = #{id}
</select>
// CouponMapper接口文件新建方法
Coupon selectCoupon(@Param("id") Integer id);
// CouponMapper.xml文件配置
<resultMap id="couponMap" type="Coupon">
<id property="id" column="id" />
<result column="coupon_url" property="coupon_url" />
</resultMap>
<select id="selectCoupon" resultMap="couponMap" >
SELECT * FROM Coupon WHERE id = #{id}
</select>
// 测试程序
public void test7(){
InputStream inputStream = null;
SqlSession sqlSession = null;
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory mSqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = mSqlSessionFactory.openSession();
GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
Good good = goodMapper.selectGood(1);
System.out.println(good);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
3.2 、最后的运行结果:

测试结果
方法二
4.1 使用嵌套结果集的方式处理。配置如下
//GoodMapper的接口方法
Good selectGood2(@Param("id") Integer id);
// GoodMapper.xml
<resultMap id="goodMap2" type="Good">
<id column="id" property="id" />
<result column="good_name" property="good_name" />
<result property="coupon_id" column="coupon_id" />
<association property="mCoupon" javaType="Coupon">
<id property="id" column="id" />
<result property="coupon_url" column="coupon_url" />
</association>
</resultMap>
<select id="selectGood2" resultMap="goodMap2">
SELECT * from Good g, Coupon c where g.coupon_id=c.id and g.id=#{id}
</select>
//测试程序
@Test
public void test8(){
InputStream inputStream = null;
SqlSession sqlSession = null;
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory mSqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = mSqlSessionFactory.openSession();
GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
Good good = goodMapper.selectGood2(1);
System.out.println(good);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
4.2 运行结果

测试结果
5、小结,我们也可以设置懒加载的方式,在真正用到模型的时候才去数据库查询数据,这样可以更加节省数据库的性能。
在mybatis-config的xml

lazyLoading配置属性
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="true" />
一对多查询
修改Coupon表为如下,多个 优惠券可能对应一个商品

优惠券表结构与内容
//GoodMapper接口方法
Good getGoodAndCouponMap(@Param("id") Integer id);
//GoodMapper.xml文件
<resultMap id="GoodCouponMap" type="Good">
<id property="id" column="id" />
<collection property="mCouponList" column="id" ofType="Coupon" select="me.aihe.dao.CouponMapper.getCouponFromGood"/>
</resultMap>
<select id="getGoodAndCouponMap" resultMap="GoodCouponMap">
SELECT * FROM Good WHERE id = #{id}
</select>
//CouponMapper.xml文件
<select id="getCouponFromGood" resultType="me.aihe.dao.Coupon">
SELECT * FROM Coupon WHERE g_id = #{gid}
</select>
//测试程序
@Test
public void test9(){
InputStream inputStream = null;
SqlSession sqlSession = null;
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory mSqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = mSqlSessionFactory.openSession();
GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
Good good = goodMapper.getGoodAndCouponMap(1);
System.out.println(good);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
运行结果

测试结果
这里使用分布查询较为方便,嵌套结果集查询,可自行测试
总结
本文主要演示了Mybatis对数据库表与表之间有关联的时候的操作。