#千鋒逆戰(zhàn)# Mybatis多表聯(lián)合

Mybatis多表聯(lián)合查找

  1. 加入依賴(lài)

    • 加入mybatis核心依賴(lài),junit測(cè)試,mysql依賴(lài)

    • <dependencies>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
          </dependency>
      
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>5.1.47</version>
          </dependency>
      
          <dependency>
              <groupId>org.mybatis</groupId>
              <artifactId>mybatis</artifactId>
              <version>3.4.6</version>
          </dependency>
      
  2. XML配置

    • resources目錄下新建Mybatis.xml文件

    • 導(dǎo)入db.properties資源文件,如下

      driver=com.mysql.jdbc.Driver
      url=jdbc:mysql://localhost:3307/shop_mybatis
      username=root
      password=970809
      
    • 設(shè)置settings,加入自動(dòng)日志

    • 設(shè)置typeAliases,設(shè)置別名包,此包下的引用可直接寫(xiě)類(lèi)名,忽略大小寫(xiě)

    • 設(shè)置連接池屬性POOLED

    • 在mappers標(biāo)簽下設(shè)置對(duì)應(yīng)的映射文件路徑

    • <?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="db.properties"/>
      
          <!--添加日志功能,STDOUT_LOGGING不需要添加第三方j(luò)ar包-->
          <settings>
              <setting name="logImpl" value="STDOUT_LOGGING"/>
          </settings>
          <typeAliases>
              <package name="pojo"/>
          </typeAliases>
      
          <environments default="development">
              <environment id="development">
                  <transactionManager type="JDBC"/>
                  <dataSource type="POOLED">
                      <property name="driver" value="${driver}"/>
                      <property name="url" value="${url}"/>
                      <property name="username" value="${username}"/>
                      <property name="password" value="${password}"/>
                  </dataSource>
              </environment>
          </environments>
      
          
          <mappers>
              <mapper resource="com/mapper/OrderMapper.xml"/>
              <mapper resource="com/mapper/UserMapper.xml"/>
              <mapper resource="com/mapper/DetailMapper.xml"/>
              <mapper resource="com/mapper/ProductMapper.xml"/>
              <mapper resource="com/mapper/TypeMapper.xml"/>
          </mappers>
      </configuration>
      
  3. OrderMapper.xml配置例舉

    • namespace和id組合唯一,可自定義

    • 使用select標(biāo)簽進(jìn)行查詢(xún)操作,其他操作分別為update,insert,delete

    • 使用resultMap進(jìn)行聯(lián)合查詢(xún)

    • resultMap標(biāo)簽下 如果對(duì)應(yīng)pojo內(nèi)的屬性名(property)和數(shù)據(jù)庫(kù)表中的列名相同,可省略不寫(xiě)result標(biāo)簽

    • 使用association標(biāo)簽進(jìn)行對(duì)一的表結(jié)構(gòu)聯(lián)合查詢(xún),使用Collection標(biāo)簽進(jìn)行對(duì)多的表結(jié)構(gòu)聯(lián)合查詢(xún)

    • <?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="pojo.OrderMapper">
          <select id="getOrderByOid" resultMap="orderMap">
              select * from orders where oid = #{oid}
        </select>
          
          <resultMap id="orderMap" type="Order">
      <!--        <id property="oid" column="oid"/>-->
      <!--        <result column="price" property="price"/>-->
      <!--        <result column="addr" property="addr"/>-->
      <!--        <result column="payType" property="payType"/>-->
      
              <!--對(duì)一,select中填寫(xiě)對(duì)應(yīng)的映射文件的namespace.id-->
              <association property="user" column="uid" select="pojo.UserMapper.getUserByUid"/>
      
             <!--對(duì)多-->
              <collection property="details" column="did" select="pojo.DetailMapper.getDetailByDid" ofType="pojo.Detail"/>
          </resultMap>
      </mapper>
      
  4. UserMapper.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="pojo.UserMapper">
          <select id="getUserByUid" resultType="User">
          select * from users where uid = #{uid}
        </select>
      
      </mapper>
      
  5. DetailMapper.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="pojo.DetailMapper">
          <select id="getDetailByDid" resultMap="detailMap">
              select * from details where did = #{did}
        </select>
      
      
          <resultMap id="detailMap" type="Detail">
      <!--        <id property="did" column="did"/>-->
      <!--        <result column="count" property="count"/>-->
      
              <association property="product" column="pid" select="pojo.ProductMapper.getProductByPid"/>
          </resultMap>
      
      </mapper>
      
  6. ProductMapper.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="pojo.ProductMapper">
          <select id="getProductByPid" resultMap="productMap">
          select * from products where pid = #{pid}
        </select>
      
          <resultMap id="productMap" type="Product">
      
      <!--        <id property="pid" column="pid"/>-->
      <!--        <result property="name" column="name"/>-->
      <!--        <result property="price" column="price"/>-->
      <!--        <result property="img" column="img"/>-->
      
              <association property="type" column="tid" select="pojo.TypeMapper.getTypeByTid"/>
          </resultMap>
      
      </mapper>
      
  7. TypeMapper.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="pojo.TypeMapper">
          <select id="getTypeByTid" resultType="Type">
          select * from types where tid = #{tid}
        </select>
      
      </mapper>
      
  8. 測(cè)試類(lèi)

    • 使用junit的@Before注解,setUp在所有方法運(yùn)行前運(yùn)行

    • 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 org.junit.After;
      import org.junit.Before;
      import org.junit.Test;
      import pojo.Order;
      
      import java.io.IOException;
      
      public class TestOrders {
          private SqlSessionFactory sf = null;
          private SqlSession session = null;
      
          @Before
          public void setUp() {
              try {
                  sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("Mybatis.xml"));
                  session = sf.openSession();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      
          @After
          public void tearDown() {
              if (session != null) {
                  session.close();
                  session = null;
              }
          }
      
          @Test
          public void testGetOrderByOid() {
              Order order = session.selectOne("pojo.OrderMapper.getOrderByOid",1);
              System.out.println(order);
          }
      }
      
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在學(xué)習(xí)MyBatis3的過(guò)程中,文檔上面一直在強(qiáng)調(diào)一個(gè)id的東西!在做這個(gè)實(shí)驗(yàn)的時(shí)候,也因?yàn)闆](méi)有理解清楚id含義而...
    殺小賊閱讀 1,027評(píng)論 0 6
  • 一、配置maven的pom.xml加載jar包 為了后續(xù)開(kāi)發(fā)的方便,將SSM框架所有需要的jar包一并加載進(jìn)來(lái) p...
    docki閱讀 2,402評(píng)論 1 23
  • 1. 簡(jiǎn)介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲(chǔ)過(guò)程以及高級(jí)映射的優(yōu)秀的...
    笨鳥(niǎo)慢飛閱讀 6,220評(píng)論 0 4
  • 一、結(jié)果映射是什么? ResultMap 1.使用場(chǎng)景一 當(dāng)數(shù)據(jù)庫(kù)的列名和實(shí)體類(lèi)的屬性名不一致時(shí),無(wú)法直接通過(guò)re...
    huishao閱讀 1,084評(píng)論 3 3
  • 1、Mybatis支持普通SQL查詢(xún)、存儲(chǔ)一級(jí)高級(jí)映射的優(yōu)秀持久層框架 2、Mybatis可以使用簡(jiǎn)單的XML或注...
    JHMichael閱讀 387評(píng)論 0 1

友情鏈接更多精彩內(nèi)容