为了账号安全,请及时绑定邮箱和手机立即绑定

Mybatis进阶教程(二)

标签:
大数据

接着上一篇的进阶教程,继续学习Mybatis的一些常用用法。以便我们更好使用Mybatis。

知识点汇总

数据表一对一association

现实中,有时候我们的模型数据,需要结合其它模型,也就是数据库表与表之间的关联。比如说商品表,与优惠券表,假如一个商品对应一个优惠券。
1、数据表


1000

商品表信息

1000

优惠券表信息

666

商品表与优惠券表内容

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

584

项目目录结构

部分文件内容:

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 、最后的运行结果:

1000

测试结果

方法二

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 运行结果

1000

测试结果

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

1000

lazyLoading配置属性

        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="aggressiveLazyLoading" value="true" />

一对多查询

修改Coupon表为如下,多个 优惠券可能对应一个商品

1000

优惠券表结构与内容

//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();
            }
        }
    }

运行结果

1000

测试结果

这里使用分布查询较为方便,嵌套结果集查询,可自行测试

总结

本文主要演示了Mybatis对数据库表与表之间有关联的时候的操作。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
1.1万
获赞与收藏
1544

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消