以前一直使用Hibernate,基本上没用过Mybatis,工作中需要做映射关系,简单的了解下Mybatis的映射。
两者相差不多都支持一对一,一对多,多对多,本章简单介绍一对一的使用以及注意点。
建表语句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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', '跨国集团');
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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:
1
2
3
4
5
6
7
8
9
10
|
package mapper;
import java.util.List;
import model.Bloc;
public interface BlocMapper {
public List< Bloc > getBlocList(String name);
}
|
1
2
3
4
5
|
package mapper;
public interface CompanyMapper {
}
|
model:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? 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 >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< 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= 查询方法的参数
运行结果
1
|
companyName =
|
形式二:关联查询
映射配置实体测试类一样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? 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值
运行结果为
1
|
companyName = 宏伟集团
|
显然运行结果不是我们想要的结果
以上简单的Demo希望能帮助初学Mybatis童鞋!!
这篇基于Mybaits映射的一点心得(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/zhuxiansheng/p/7794292.html