springboot jpa | mybaits

一.jpa:

1.jpa可以使用jpaRepository,@query的查询, 当然如果方法命名规范,可以不写sql代码

2.jpa可也使用EntityManager,通过@PersistenceContext 注入 private EntityManager entityManager,使用this.entityManager.createQuery(sql)查询

参见:https://www.cnblogs.com/cnblog-long/p/7238338.html

3.https://www.youtube.com/watch?v=AkUMJxh407A

4.jpa的动态查询,参见https://blog.csdn.net/tianyaleixiaowu/article/details/72876732

二.mybatis:

1.多对1和1对1映射:

1.1 急迫抓取的1对1或多对1

<resultMap id="BaseResultMap" type="com.test3.model.Article" >

  <id column="id" property="id"/>
  <result column="type" property="type"/>
  <result column="title" property="title"/>
  <result column="author" property="author"/>
  <result column="content" property="content"/>
  <result column="create_time" property="createTime"/>

  <association property="articleType" javaType="com.test3.model.ArticleType" >
    <id column="type" property="id"/>
    <result column="type_name" property="typeName"/>
  </association>
</resultMap>

1.2懒加载的1对1或多对1
<resultMap id="lazyResultMap" type="com.test3.model.Article" >
  <id column="id" property="id"/>
  <result column="title" property="title"/>
  <result column="author" property="author"/>
  <result column="content" property="content"/>
  <result column="create_time" property="createTime"/>

  <association property="articleType" javaType="com.test3.model.ArticleType" select="com.test3.mapper.ArticleTypeMapper.getByIdJust" column="type" >
  </association>
</resultMap>

2.多对多和1对多映射:

我们系统不搞多对对,而使用1对多形成多对多。其实我的系统1对多和多对多都不会去使用,而是自己查询来封装

<resultMap type="com.test3.model.ArticleType" id="baseResultMap">
  <id column="articleTypeId" property="id"/>
  <result column="type_name" property="typeName"/>
<!-- 知道就可以了,不要在系统中这样使用集合映射 -->
  <collection property="articles" ofType="com.test3.model.Article">
    <id column="id" property="id"/>
    <result column="type" property="type"/>
    <result column="title" property="title"/>
    <result column="author" property="author"/>
    <result column="content" property="content"/>
    <result column="create_time" property="createTime"/>
  </collection>
</resultMap>

原文地址:https://www.cnblogs.com/yanjunwu/p/9200493.html