Employemapper.xml文件中,注意的是返回的类型resultType也是实体类对象的
<!-- public List
<Employe
> getEmpsByLastNameLike
(String lastName
); --
>
<select id
="getEmpsByLastNameLike" resultType
="com.atguigu.mybatis.hello.Employe">
select * from tbl_employe where last_name like
</select
>
接口EmployeMapper
package com.atguigu.mybatis.dao
;
import java.util.LinkedList
;
import java.util.List
;
import java.util.Map
;
import org.apache.ibatis.annotations.Param
;
import com.atguigu.mybatis.hello.Employe
;
public interface EmployeMapper
{
public List
<Employe
> getEmpsByLastNameLike
(String lastName
);
}
测试文件
@Test
public void test3
() throws Exception
{
SqlSession opSession
=SqlSessionFactoryji
();
try
{
EmployeMapper employeMapper
=opSession.getMapper
(EmployeMapper.class
);
List
<Employe
> likEmployes
=employeMapper.getEmpsByLastNameLike
("%t%");
for (Employe employe
: likEmployes
) {
System.out.println
(employe
);
}
} finally
{
opSession.close
();
}
}
实现效果,
select记录封装map
EmployMapper.java
package com.atguigu.mybatis.dao
;
import java.util.LinkedList
;
import java.util.List
;
import java.util.Map
;
import org.apache.ibatis.annotations.MapKey
;
import org.apache.ibatis.annotations.Param
;
import com.atguigu.mybatis.hello.Employe
;
public interface EmployeMapper
{
//多条记录封装一个Map
<Integer,Employe
>:键是这条记录的主键,指是记录封装后的javaBean
//告诉mybatis封装这个map的时候用哪个属性作为map的key
@MapKey
("id")
public Map
<Integer, Employe
> getEmpByLastNameLikeReturnMap
(String lastName
);
}
EmployeMapper.xml配置文件
<!-- public Map
<Integer, Employe
> getEmpByLastNameLikeReturnMap
(String lastName
); --
>
<select id
="getEmpByLastNameLikeReturnMap" resultType
="com.atguigu.mybatis.hello.Employe">
select * from tbl_employe where last_name like
</select
>
`测试类:
@Test
public void test3
() throws Exception
{
SqlSession opSession
=SqlSessionFactoryji
();
try
{
EmployeMapper employeMapper
=opSession.getMapper
(EmployeMapper.class
);
// Map
<String, Object
> eMaPMap
=employeMapper.getEmpByIdReturnMap
(1
);
// System.out.println
(eMaPMap
);
Map
<Integer, Employe
> empMap
= employeMapper.getEmpByLastNameLikeReturnMap
("%t%");
System.out.println
(empMap
);
} finally
{
opSession.close
();
}
}
运行效果,多个记录封装在map中
resultMap自定义结果映射规则,不需要驼峰的设置
EmployeMapper.java
package com.atguigu.mybatis.dao
;
import com.atguigu.mybatis.hello.Employe
;
public interface EmployeMapperPlus
{
public Employe getEmployebyid
(Integer id
);
}
EmployeMapperPlus.xml
<?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
="com.atguigu.mybatis.dao.EmployeMapperPlus">
<!--自定义某个javaBean的封装规则
type:自定义规则的Java类型
id:唯一id方便引用
--
>
<resultMap type
="com.atguigu.mybatis.hello.Employe" id
="myEmp">
<!--指定主键列的封装规则
id定义主键底层有优化
column:指定哪一列
property:指定对应的javaBean属性
--
>
<id column
="id" property
="id"></id
>
<!--定义普通封装规则 --
>
<result column
="last_name" property
="lastName"/
>
<!--其他不指定的列会自动封装,但是我们写resultMap就最好把全部的映射规则都写上 --
>
<result column
="gender" property
="gender"/
>
<result column
="email" property
="email"></result
>
</resultMap
>
<select id
="getEmployebyid" resultMap
="myEmp">
select * from tbl_employe where id
=
</select
>
</mapper
>
测试类
@Test
public void test5
() throws Exception
{
SqlSession opSession
=SqlSessionFactoryji
();
try
{
EmployeMapperPlus employMapperPlus
=opSession.getMapper
(EmployeMapperPlus.class
);
Employe employe
= employMapperPlus.getEmployebyid
(1
);
System.out.println
(employe
);
} finally
{
opSession.close
();
}
}
##########注意将配置文件EmployMapperPlus.xml 记得引入中mybatis-config.xml
级联dept表,按需查询
<?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
="com.atguigu.mybatis.dao.EmployeMapperPlus">
<!--自定义某个javaBean的封装规则
type:自定义规则的Java类型
id:唯一id方便引用
--
>
<resultMap type
="com.atguigu.mybatis.hello.Employe" id
="myEmp">
<!--指定主键列的封装规则
id定义主键底层有优化
column:指定哪一列
property:指定对应的javaBean属性
--
>
<id column
="id" property
="id"></id
>
<!--定义普通封装规则 --
>
<result column
="last_name" property
="lastName"/
>
<!--其他不指定的列会自动封装,但是我们写resultMap就最好把全部的映射规则都写上 --
>
<result column
="gender" property
="gender"/
>
<result column
="email" property
="email"></result
>
</resultMap
>
<!--
<select id
="getEmployebyid" resultMap
="myEmp">
select * from tbl_employe where id
=
</select
> --
>
<!-- 联合查询,级联属性封装结果集 --
>
<resultMap type
="com.atguigu.mybatis.hello.Employe" id
="MyDifEmp">
<id column
="id" property
="id"/
>
<result column
="last_name" property
="lastName"/
>
<result column
="gender" property
="gender"/
>
<result column
="email" property
="email"/
>
<result column
="id" property
="dept.id"></result
>
<result column
="dept_name" property
="dept.deptName"></result
>
</resultMap
>
<!--使用association定义关联的单个对象的封装规则 --
>
<resultMap type
="com.atguigu.mybatis.hello.Employe" id
="MyDifEmp2">
<id column
="id" property
="id"/
>
<result column
="last_name" property
="lastName"/
>
<result column
="gender" property
="gender"/
>
<result column
="email" property
="email"/
>
<!--association可以指定联合的javaBean对象
property
="dept":指定哪个属性是联合的对象
javaType:指定这个属性对象的类型
[不能省略
] --
>
<association property
="dept" javaType
="com.atguigu.mybatis.hello.Dept">
<id column
="id" property
="id"/
>
<result column
="dept_name" property
="deptName"/
>
</association
>
</resultMap
>
<!--
场景一:
查询Employe的同时查询员工对应的部门
Employe
==Department
一个员工有与之对应的部门
id last_name gender d_id did dept_name
--
>
<select id
="getEmpandDept" resultMap
="MyDifEmp2">
SELECT
e.id,
e.gender,
e.last_name,
e.email,
d.id,
d.dept_name
FROM
tbl_employe e,
tbl_dept d
WHERE
e.dept_id
=d.id and
e.id
=
</select
>
<!--使用association进行分布查询:
1.先按照员工id查询员工信息
2.根据查询员工信息中的d_id去部门表查出部门信息
3.部门设置到员工中; --
>
<resultMap type
="com.atguigu.mybatis.hello.Employe" id
="myEmpBystep">
<id column
="id" property
="id"></id
>
<result column
="last_name" property
="lastName"/
>
<result column
="gender" property
="gender"/
>
<result column
="email" property
="email"/
>
<!-- association定义关联对象的封装规则
select:表明当前属性是调用select指定的方法查出结果
流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property--
>
<association property
="dept" select
="com.atguigu.mybatis.dao.DeptMapper.getDeptbyId" column
="dept_id">
</association
>
</resultMap
>
<select id
="getEmployebyid" resultMap
="myEmpBystep">
select * from tbl_employe where id
=
</select
>
<!--可以使用延迟加载(懒加载);(按需加载)
Employ
===dept,
我们每次查询Employe对象的时候,都将一起查询出来。
部门信息在我们使用的时候再去查询;
实现的方法是:分段查询的基础上加上两个配置
<setting name
="lazyLoadingEnabled" value
="true"/
> 关联的这个值使用的时候延迟加载,默认为false
<setting name
="aggressiveLazyLoading" value
="false" /
> 开启每一个属性全部加载出来,禁用的话可以按需加载
--
>
</mapper
>
测试类
@Test
public void test6
() throws Exception
{
SqlSession opSession
=SqlSessionFactoryji
();
try
{
EmployeMapperPlus employeMapperPlus
=opSession.getMapper
(EmployeMapperPlus.class
);
Employe employe
=employeMapperPlus.getEmployebyid
(1
);
System.out.println
(employe
);
System.out.println
(employe.getDept
());
} finally
{
opSession.close
();
}
}