复制进MySQL
数据库中运行即可
DROP TABLE IF EXISTS `t_emp`;
CREATE TABLE `t_emp` (
`emp_id` int(11) NOT NULL AUTO_INCREMENT,
`emp_name` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`gender` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`dept_id` int(11) DEFAULT NULL,
PRIMARY KEY (`emp_id`) USING BTREE,
INDEX `my`(`dept_id`) USING BTREE,
CONSTRAINT `my` FOREIGN KEY (`dept_id`) REFERENCES `t_dept` (`dept_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of t_emp
-- ----------------------------
INSERT INTO `t_emp` VALUES (1, '张三', 18, '男', 1);
INSERT INTO `t_emp` VALUES (2, '李四', 19, '男', 2);
INSERT INTO `t_emp` VALUES (3, '王五', 18, '男', 3);
INSERT INTO `t_emp` VALUES (4, '赵六', 18, '男', 1);
SET FOREIGN_KEY_CHECKS = 1;
DROP TABLE IF EXISTS `t_dept`;
CREATE TABLE `t_dept` (
`dept_id` int(11) NOT NULL,
`dept_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of t_dept
-- ----------------------------
INSERT INTO `t_dept` VALUES (1, 'A');
INSERT INTO `t_dept` VALUES (2, 'B');
INSERT INTO `t_dept` VALUES (3, 'C');
SET FOREIGN_KEY_CHECKS = 1;
为查询的字段设置别名,和属性名保持一致
<select id="getEmpByIdOne" resultType="Emp">
select emp_id empId , emp_name empName , age , gender from t_emp where emp_id = #{id}
select>
当字段符合MySQL的要求使用_,而属性符合java的要求使用驼峰
此时可以在MyBatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰
emp_id ==> empId , emp_name ==> empName;
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<select id="getEmpByIdOne" resultType="Emp">
select * from t_emp where emp_id = #{id}
select>
resultMap: 设置自定义的映射关系
id:唯一标识
type:处理映射关系的实体的类型
id标签:设置主键的
column:设置映射关系中的字段名,必须是sql查询出的某一个字段
property:设置映射关系中的属性的属性名,必须是处理的实体类类型中的属性名
<resultMap id="empResultMap" type="Emp">
<id column="emp_id" property="empId">id>
<result column="emp_name" property="empName">result>
<result column="gender" property="gender">result>
<result column="age" property="age">result>
resultMap>
<select id="getEmpById" resultMap="empResultMap">
select * from t_emp where emp_id = #{id}
select>
<resultMap id="getEmpAndDeptResultOne" type="Emp">
<id column="emp_id" property="empId">id>
<result column="emp_name" property="empName">result>
<result column="gender" property="gender">result>
<result column="age" property="age">result>
<result column="dept_id" property="dept.deptId">result>
<result column="dept_name" property="dept.deptName">result>
resultMap>
association:处理多对一(或一对一)的映射关系(处理实体类类型的属性)
property:设置需要处理映射关系的属性的属性名
javaType:设置要处理的属性的类型
<resultMap id="getEmpAndDeptResult" type="Emp">
<id column="emp_id" property="empId">id>
<result column="emp_name" property="empName">result>
<result column="gender" property="gender">result>
<result column="age" property="age">result>
<association property="dept" javaType="Dept">
<result column="dept_id" property="deptId">result>
<result column="dept_name" property="deptName">result>
association>
resultMap>
<select id="getEmpAndDeptByEmpId" resultMap="getEmpAndDeptResult">
select t_emp.* , t_dept.* from t_emp left join t_dept on t_dept.dept_id=t_emp.dept_id where t_emp.emp_id=#{empId}
select>
property:设置需要映射关系的属性的属性名
select:设置分步查询的sql的唯一标识
column:将查询出的某个字段作为分步查询的sql的条件
fetchType:开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载
fetchType=“eager(立即加载)| lazy(延迟加载)”
<resultMap id="empAndDeptStepResultMap" type="Emp">
<id column="emp_id" property="empId">id>
<result column="emp_name" property="empName">result>
<result column="age" property="age">result>
<result column="gender" property="gender">result>
<association property="dept" fetchType="eager"
select="com.ch.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="dept_id">
association>
resultMap>
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptStepResultMap">
select * from t_emp where emp_id = #{empId}
select>
getEmpAndDeptByStepOne
查询出来的dept_id作为getEmpAndDeptByStepTwo
的查询条件
<select id="getEmpAndDeptByStepTwo" resultType="dept">
select * from t_dept where dept_id = #{deptId}
select>
分步查询的优点:可以实现延迟加载,此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql
ofType: 设置集合类型的属性中存储的数据的类型
property: 设置需要映射关系的属性的属性名
<resultMap id="deptAndEmpResultMap" type="Dept">
<id column="dept_id" property="deptId">id>
<result column="dept_name" property="deptName">result>
<collection property="emps" ofType="Emp">
<id column="emp_id" property="empId">id>
<result column="emp_name" property="empName">result>
<result column="age" property="age">result>
<result column="gender" property="gender">result>
collection>
resultMap>
<select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
select t_dept.*,t_emp.* from t_dept left join t_emp on t_dept.dept_id = t_emp.dept_id where t_dept.dept_id=#{deptId}
select>
property:设置需要映射关系的属性的属性名
select:设置分步查询的sql的唯一标识
column:将查询出的某个字段作为分步查询的sql的条件
fetchType:开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载
fetchType=“eager(立即加载)| lazy(延迟加载)”
<resultMap id="empAndDeptByStepOneMap" type="dept">
<id column="dept_id" property="deptId">id>
<result column="dept_name" property="deptName">result>
<association property="emps"
select="com.ch.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
column="dept_id">
association>
resultMap>
<select id="getDeptAndEmpByStepOne" resultMap="empAndDeptByStepOneMap">
select * from t_dept where dept_id= #{deptId}
select>
getDeptAndEmpByStepOne
查询出来的dept_id作为getDeptAndEmpByStepTwo
的查询条件
<select id="getDeptAndEmpByStepTwo" resultType="emp">
select * from t_emp where dept_id = #{deptId}
select>