溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于Mybaits映射的一點(diǎn)心得(分享)

發(fā)布時(shí)間:2020-09-01 18:22:07 來(lái)源:腳本之家 閱讀:114 作者:紅尾巴的豬 欄目:編程語(yǔ)言

以前一直使用Hibernate,基本上沒(méi)用過(guò)Mybatis,工作中需要做映射關(guān)系,簡(jiǎn)單的了解下Mybatis的映射。

兩者相差不多都支持一對(duì)一,一對(duì)多,多對(duì)多,本章簡(jiǎn)單介紹一對(duì)一的使用以及注意點(diǎn)。

建表語(yǔ)句:

CREATE TABLE `bloc` ( 
 
 `id` int(11) NOT NULL auto_increment, 
 
 `name` varchar(255) collate utf8_bin default NULL, 
 
 `company_id` int(11) default NULL, 
 
 `intro` varchar(255) collate utf8_bin default NULL, 
 
 PRIMARY KEY (`id`) 
 
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 
 
 
 
INSERT INTO`bloc` (`id`, `name`, `company_id`, `intro`) VALUES ('1', '宏偉集團(tuán)', '1', '跨國(guó)集團(tuán)');
CREATE TABLE `company` ( 
 
 `id` int(11) NOT NULL, 
 
 `name` varchar(255) collate utf8_bin default NULL, 
 
 `intro` varchar(255) collate utf8_bin default NULL, 
 
 PRIMARY KEY (`id`) 
 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 
 
INSERT INTO company (`id`, `name`, `intro`) VALUES ('1', '', NULL);

形式一:子查詢

JAVA代碼:SqlSessionHelper.java

package com.demo.mybatis; 
 
import java.io.IOException; 
import java.io.Reader; 
 
import org.apache.ibatis.io.Resources; 
import org.apache.ibatis.session.SqlSessionFactory; 
import org.apache.ibatis.session.SqlSessionFactoryBuilder; 
 
public class SqlSessionHelper { 
  
 public static SqlSessionFactory getSessionFactory() throws IOException{ 
  SqlSessionFactory sessionFactory = null; 
  Reader reader = Resources.getResourceAsReader("configuration.xml"); 
  try{ 
   sessionFactory = new SqlSessionFactoryBuilder().build(reader);; 
  }catch(Exception ex){ 
   ex.printStackTrace(); 
  } 
  return sessionFactory; 
 } 
}

Test.java:

package com.demo.mybatis; 
 
import java.util.List; 
 
import org.apache.ibatis.session.SqlSession; 
 
import mapper.BlocMapper; 
import model.Bloc; 
 
public class Test { 
 
 /** 
  * @param args 
  */
 public static void main(String[] args) { 
   
  try{ 
   SqlSession sqlSession = SqlSessionHelper.getSessionFactory().openSession(); 
   BlocMapper blocMapper = sqlSession.getMapper(BlocMapper.class); 
   List<Bloc> blocs = blocMapper.getBlocList("1"); 
   for (Bloc bloc : blocs) { 
    System.out.println("companyName = "bloc.getCompany().getName()); 
   } 
  }catch(Exception ex){ 
   System.out.println(ex.getMessage()); 
  } 
 
 } 
 
}

mapper:

package mapper; 
 
import java.util.List; 
 
import model.Bloc; 
 
public interface BlocMapper { 
 
 public List<Bloc> getBlocList(String name); 
} 
package mapper; 
 
public interface CompanyMapper { 
 
} 

model:

package model; 
 
public class Bloc { 
 
 private Integer id; 
  
 private String name; 
  
 private String intro; 
  
 private Company company; 
 
 public Integer getId() { 
  return id; 
 } 
 
 public void setId(Integer id) { 
  this.id = id; 
 } 
 
 public String getName() { 
  return name; 
 } 
 
 public void setName(String name) { 
  this.name = name; 
 } 
 
 public String getIntro() { 
  return intro; 
 } 
 
 public void setIntro(String intro) { 
  this.intro = intro; 
 } 
 
 public Company getCompany() { 
  return company; 
 } 
 
 public void setCompany(Company company) { 
  this.company = company; 
 } 
  
  
}
package model; 
 
public class Company { 
 
 private Integer id; 
  
 private String name; 
  
 private Integer intro; 
 
 public Integer getId() { 
  return id; 
 } 
 
 public void setId(Integer id) { 
  this.id = id; 
 } 
 
 public String getName() { 
  return name; 
 } 
 
 public void setName(String name) { 
  this.name = name; 
 } 
 
 public Integer getIntro() { 
  return intro; 
 } 
 
 public void setIntro(Integer intro) { 
  this.intro = intro; 
 } 
  
  
}

映射配置如下:(如果是一堆多的話“brandObject”改成實(shí)體List屬性association 改成collection )

<?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="mapper.CompanyMapper"> 
  
 <resultMap id="BaseResultMap" type="model.Company" > 
  <id column="id" property="id" jdbcType="INTEGER" /> 
  <result column="name" property="name" jdbcType="VARCHAR" /> 
  <result column="intro" property="intro" jdbcType="VARCHAR" /> 
 </resultMap> 
 <select id = "getCompanyInfo" parameterType="Integer" resultMap="BaseResultMap"> 
  select * from company where id = #{id} 
 </select> 
</mapper> 
<mapper namespace="mapper.BlocMapper">
 <resultMap id="BaseResultMap" type="model.Bloc" >
  <id column="id" property="id" jdbcType="INTEGER" />
  <result column="name" property="name" jdbcType="VARCHAR" />
  <result column="intro" property="intro" jdbcType="VARCHAR" />
  <association column="company_id" property="company" select="mapper.CompanyMapper.getCompanyInfo">
  </association>
 </resultMap>
 
 <select id="getBlocList" parameterType="String" resultMap="BaseResultMap">
  select * from bloc where name = #{name}
 </select>
</mapper>

column:表中的字段 property:實(shí)體當(dāng)中的字段名 select:引入的另一個(gè)xxxMapper.xml的getCompanyInfo方法

這樣當(dāng)查詢用的映射時(shí)檢測(cè)到有select就會(huì)執(zhí)行你引入的另一個(gè)Mapper的查詢方法,查詢條件是company_id= 查詢方法的參數(shù)

運(yùn)行結(jié)果

companyName = 

形式二:關(guān)聯(lián)查詢

映射配置實(shí)體測(cè)試類一樣:

<?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="mapper.BlocMapper"> 
 <resultMap id="BaseResultMap" type="model.Bloc" > 
  <id column="id" property="id" jdbcType="INTEGER" /> 
  <result column="name" property="name" jdbcType="VARCHAR" /> 
  <result column="intro" property="intro" jdbcType="VARCHAR" /> 
  <!-- 查詢會(huì)有賦值紊亂問(wèn)題 --> 
  <association column="company_id" property="company" resultMap = "mapper.CompanyMapper.BaseResultMap"> 
  </association> 
  <!-- <association column="company_id" property="company" select="mapper.CompanyMapper.getCompanyInfo"> 
  </association> --> 
 </resultMap> 
  
 <select id="getBlocList" parameterType="String" resultMap="BaseResultMap"> 
  <!-- select * from bloc where name = #{name} --> 
  <!-- 查詢會(huì)有賦值紊亂問(wèn)題 --> 
  select * from bloc b left join company c on b.company_id = c.id where b.name = #{name} 
 </select> 
</mapper>

column:表中的字段 property:實(shí)體當(dāng)中的字段名 resultMap :引入另一個(gè)Mapper的映射

值得注意的是:因?yàn)槭乔短子成洌孕问蕉趦蓚€(gè)實(shí)體字段名一樣的情況下會(huì)引發(fā)字段賦值的紊亂,例如兩個(gè)實(shí)體都有name 當(dāng)?shù)谝粋€(gè)實(shí)體name有值,第二個(gè)實(shí)體name沒(méi)有值的時(shí)候,查詢出來(lái)的結(jié)果是兩個(gè)實(shí)體name都有值,且都是一樣的為第一個(gè)實(shí)體的name值

運(yùn)行結(jié)果為

companyName = 宏偉集團(tuán) 

顯然運(yùn)行結(jié)果不是我們想要的結(jié)果

以上簡(jiǎn)單的Demo希望能幫助初學(xué)Mybatis童鞋??!

這篇基于Mybaits映射的一點(diǎn)心得(分享)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI