基于Mybatis映射的⼀点⼼得(分享)以前⼀直使⽤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 batis;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.ssion.SqlSessionFactory;
import org.apache.ibatis.ssion.SqlSessionFactoryBuilder;
心字怎么写好看public class SqlSessionHelper {
public static SqlSessionFactory getSessionFactory() throws IOException{
SqlSessionFactory ssionFactory = null;
Reader reader = ResourceAsReader("l");土荆皮的功效与作用
try{
ssionFactory = new SqlSessionFactoryBuilder().build(reader);;
}catch(Exception ex){
ex.printStackTrace();
}
return ssionFactory;
}
苏维埃联邦
}
Test.java:
package batis;
import java.util.List;
import org.apache.ibatis.ssion.SqlSession;
import mapper.BlocMapper;
import model.Bloc;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try{
SqlSession sqlSession = SessionFactory().openSession(); BlocMapper blocMapper = Mapper(BlocMapper.class);
List<Bloc> blocs = BlocList("1");
for (Bloc bloc : blocs) {
System.out.println("companyName = "Company().getName());
}
}catch(Exception ex){
System.out.Message());
}
}
}
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 tId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void tName(String name) {
this.name = name;
}
public String getIntro() {
return intro;
}
public void tIntro(String intro) {
this.intro = intro;
}
public Company getCompany() {
return company;
}
public void tCompany(Company company) {
}
}
package model;
public class Company {
private Integer id;
private String name;
private Integer intro;
public Integer getId() {
return id;
}
public void tId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void tName(String name) {
this.name = name;
}
public Integer getIntro() {
return intro;
}
环保小区public void tIntro(Integer intro) {
this.intro = intro;
}
}
映射配置如下:(如果是⼀堆多的话“brandObject”改成实体List属性association 改成collection )<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.CompanyMapper">
<resultMap id="BaResultMap" 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>
<lect id = "getCompanyInfo" parameterType="Integer" resultMap="BaResultMap">
lect * from company where id = #{id}
</lect>
</mapper>
<mapper namespace="mapper.BlocMapper">
<resultMap id="BaResultMap" 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" lect="CompanyInfo">
</association>
</resultMap>
<lect id="getBlocList" parameterType="String" resultMap="BaResultMap">
lect * from bloc where name = #{name}
</lect>
</mapper>
column:表中的字段 property:实体当中的字段名 lect:引⼊的另⼀个l的getCompanyInfo⽅法
这样当查询⽤的映射时检测到有lect就会执⾏你引⼊的另⼀个Mapper的查询⽅法,查询条件是comp
any_id= 查询⽅法的参数
运⾏结果
companyName =
形式⼆:关联查询
映射配置实体测试类⼀样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.BlocMapper">
<resultMap id="BaResultMap" 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.BaResultMap">
</association>
<!-- <association column="company_id" property="company" lect="CompanyInfo">
</association> -->
</resultMap>
<lect id="getBlocList" parameterType="String" resultMap="BaResultMap">
<!-- lect * from bloc where name = #{name} -->
<!-- 查询会有赋值紊乱问题 -->
lect * from bloc b left join company c pany_id = c.id where b.name = #{name}
</lect>
</mapper>
column:表中的字段 property:实体当中的字段名 resultMap :引⼊另⼀个Mapper的映射
值得注意的是:因为是嵌套映射,所以形式⼆在两个实体字段名⼀样的情况下会引发字段赋值的紊乱,例如两个实体都有name 当第⼀个实体name有值,第⼆个实体name没有值的时候,查询出来的结果是两个实体name都有值,且都是⼀样的为第⼀个实体的name值
运⾏结果为
companyName = 宏伟集团
显然运⾏结果不是我们想要的结果
以上简单的Demo希望能帮助初学Mybatis童鞋!!
这篇基于Mybatis映射的⼀点⼼得(分享)就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。