您好,登錄后才能下訂單哦!
以前一直使用Hibernate,基本上沒用過Mybatis,工作中需要做映射關系,簡單的了解下Mybatis的映射。
兩者相差不多都支持一對一,一對多,多對多,本章簡單介紹一對一的使用以及注意點。
建表語句:
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', '宏偉集團', '1', '跨國集團');
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”改成實體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:實體當中的字段名 select:引入的另一個xxxMapper.xml的getCompanyInfo方法
這樣當查詢用的映射時檢測到有select就會執行你引入的另一個Mapper的查詢方法,查詢條件是company_id= 查詢方法的參數
運行結果
companyName =
形式二:關聯查詢
映射配置實體測試類一樣:
<?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" /> <!-- 查詢會有賦值紊亂問題 --> <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} --> <!-- 查詢會有賦值紊亂問題 --> select * from bloc b left join company c on b.company_id = c.id where b.name = #{name} </select> </mapper>
column:表中的字段 property:實體當中的字段名 resultMap :引入另一個Mapper的映射
值得注意的是:因為是嵌套映射,所以形式二在兩個實體字段名一樣的情況下會引發字段賦值的紊亂,例如兩個實體都有name 當第一個實體name有值,第二個實體name沒有值的時候,查詢出來的結果是兩個實體name都有值,且都是一樣的為第一個實體的name值
運行結果為
companyName = 宏偉集團
顯然運行結果不是我們想要的結果
以上簡單的Demo希望能幫助初學Mybatis童鞋!!
這篇基于Mybaits映射的一點心得(分享)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。