当前位置: 移动技术网 > IT编程>开发语言>Java > 基于Mybaits映射的一点心得(分享)

基于Mybaits映射的一点心得(分享)

2019年07月19日  | 移动技术网IT编程  | 我要评论

以前一直使用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映射的一点心得(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网