前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MyBatis-Select查询

MyBatis-Select查询

作者头像
Java开发者之家
发布2021-06-17 16:54:18
3790
发布2021-06-17 16:54:18
举报
文章被收录于专栏:Java开发者之家Java开发者之家

# MyBatis-Select

# select

代码语言:javascript
复制
1. public List<Employee> getEmpsByLastNameLike(String lastName);

//返回一条记录的map:key就是列名;值就是对应的值
2. public Map<String, Object> getEmpByIdReturnMap(Integer id);

@MapKey("id")
3. public Map<Integer, Employee> getEmpMapByLastNameLikeReturnMap(String lastName);

4. public Employee getEmpBydId(Integer id);

5. public Employee getEmpAndDept(Integer id);

6. 分步查询
	public Employee getEmpByIdStep(Integer id);
	public Department getDeptById(Integer id);

7. 高级分步
	public List<Employee> getEmpsByDeptId(Integer deptId);
	public Department getDeptByIdStep(Integer id);
代码语言:javascript
复制
<!-- public List<Employee> getEmpsByLastNameLike(String lastName); -->
<!-- resultType:如果返回的是一个集合,要写集合中元素的类型 -->
1.	<select id="getEmpsByLastNameLike" resultType="com.finen.mybatis.bean.Employee">
		select * from tbl_employee where last_name like #{lastName}
	</select>
	
	<!-- public Map<String, Object> getEmpByIdReturnMap(Integer id); -->
2.	<select id="getEmpByIdReturnMap" resultType="map">
		select * from tbl_employee where id=#{id}
	</select>
	
	<!-- public Map<Integer, Employee> getEmpMapByLastNameLikeReturnMap(String lastName); -->
3.	<select id="getEmpMapByLastNameLikeReturnMap" resultType="com.finen.mybatis.bean.Employee">
		select * from tbl_employee where last_name like #{lastName}
	</select>

4.<!-- 自定义某个javaBean的封装规则
		type:自定义规则的Java类型
		id:唯一id,方便引用
	 -->
	<resultMap type="com.finen.mybatis.bean.Employee" id="MyEmp">
		<!--指定主键列的封装规则
		id:定义主键有底层优化
		column:指定那一列
		property:指定对应的javaBean属性
		 -->
		<id column="id" property="id"/>
		<result column="last_name" property="lastName"/>
		<!-- 其他不指定的列会自动封装;我们只要写resultMap就把全部的映射规则都写上-->
		<result column="email" property="email"/>
		<result column="gender" property="gender"/>
	</resultMap>
	<!-- 
		resultMap:可以指定自定义结果集映射规则
		resultType:必须已经存在
	 -->
	<!-- public Employee getEmpBydId(Integer id); -->
	<select id="getEmpBydId"  resultMap="MyEmp">
		select * from tbl_employee where id=#{id}
	</select>

5.	<!--
	场景一:
		查询Employee的同时要查询员工对应的部门
		Employee ===>Department
		 一个员工有与之对应的部门信息
	 -->
	 <!-- public Employee getEmpAndDept(Integer id); -->
	 <!--
	 	联合查询:级联属性封装结果集 
	  -->
	  
	 <resultMap type="com.finen.mybatis.bean.Employee" id="MyDifEmp">
		<id column="id" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="gender" property="gender"/>
		<result column="did" property="dept.id"/>
		<result column="dept_name" property="dept.departmentName"/>
	</resultMap>
	 
	 <resultMap type="com.finen.mybatis.bean.Employee" id="MyEmp2">
	 	<id column="id" property="id"/>
	 	<result column="last_name" property="lastName"/>
	 	<result column="gender" property="gender"/>
	 	<!-- 可以指定联合的对象
	 			property:指定那个属性是联合的对象
	 			javaType:指定这个属性对象的类型【不能省略】
	 	 -->
	 	
	 	<association property="dept" javaType="com.finen.mybatis.bean.Department">
	 		<id column="did" property="id"/>
	 		<result column="dept_name" property="departmentName"/>
	 	</association>
	 </resultMap>
	 
	 <select id="getEmpAndDept" resultMap="MyEmp2">
		SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
		d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
		WHERE e.d_id=d.id AND e.id=#{id}
	</select>

6. 分步查询
	<!-- public Employee getEmpByIdStep(Integer id); -->
	 <!--
	 	使用association进行分步查询
	 	1.先根据员工id查询员工信息
	 	2.根据查询员工信息的中d_id值去查询部门信息
	 	3.部门设置到员工中
	  -->
	 <resultMap type="com.finen.mybatis.bean.Employee" id="MyEmpByStep">
	 	<id column="id" property="id"/>
	 	<result column="last_name" property="lastName"/>
	 	<result column="email" property="email"/>
	 	<result column="gender" property="gender"/>
	 	<!-- 定义关联对象的封装规则
	 			select: 表名当前属性是调用select指定的方法查出的结果
	 			column:指定将那一列的传给这个方法
	 			
	 			流程:使用select指定的方法(传入column指定的参数的值查出对象),并封装给property指定的属性
	 	 -->
	 	<association property="dept" 
	 	select="com.finen.mybatis.dao.DepartmentMapper.getDeptById"
	 	column="d_id">
	 	</association>
	 </resultMap>
	 
	 <select id="getEmpByIdStep" resultMap="MyEmpByStep">
	 	select * from tbl_employee where id=#{id}
	 </select>

	<!-- public Department getDeptById(Integer id); -->
	<select id="getDeptById" resultType="com.finen.mybatis.bean.Department">
		select id, dept_name departmentName from tbl_dept where id=#{id}
	</select>

7. 高级分步
	<!-- 方法1:嵌套结果集的方式 -->
	<!--
		collection: 嵌套结果集的方式 ,定义关联的集合类型相关规则
	 -->
	<!-- public Department getDeptByIdPlus(Integer id); -->
	<resultMap type="com.finen.mybatis.bean.Department" id="MyDept">
		<id column="did" property="id"/>
		<result column="dept_name" property="departmentName"/>
		<!-- collection定义集合类型的封装规则
			 ofType:指定集合里面的元素类型
		 -->
		<collection property="emps" ofType="com.finen.mybatis.bean.Employee">
			<id column="eid" property="id"/>
			<result column="last_name" property="lastName"/>
			<result column="email" property="email"/>
			<result column="gender" property="gender"/>
		</collection>
	</resultMap>
	<select id="getDeptByIdPlus" resultMap="MyDept">
		SELECT d.id did,d.dept_name dept_name,
				e.id eid,e.last_name last_name,e.email email,e.gender gender
		FROM tbl_dept d
		LEFT JOIN tbl_employee e
		ON d.id=e.d_id
		WHERE d.id=#{id}
	</select>

	<!-- 方法二:分步查询 -->
	<!--
		可以使用 延迟加载
		Employee ==> Dept
			每次查询Employee对象的时候,都将一起查询出来
			部门信息在我们使用的时候再去查询;
			分段查询的基础之上加上两个配置
	 -->
	<!-- ===================association======================= -->
	<!--
		场景二:
			查询部门的时候将部门对应的所有员工信息也查询出来
	 -->
	
	<!-- public List<Employee> getEmpsByDeptId(Integer deptId); -->
	<select id="getEmpsByDeptId" resultType="com.finen.mybatis.bean.Employee">
		select * from tbl_employee where d_id=#{deptId}
	</select>

	<!-- 方法2:分步查询 -->
	<!-- public Department getDeptByIdStep(Integer id); -->
	<resultMap type="com.finen.mybatis.bean.Department" id="MyDeptStep">
		<id column="id" property="id"/>
		<result column="dept_name" property="departmentName"/>
		<collection property="emps" 
			select="com.finen.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
			column="id">
			</collection>
	</resultMap>
	
	<select id="getDeptByIdStep" resultMap="MyDeptStep">
		select id, dept_name departmentName from tbl_dept where id=#{id} 
	</select>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # MyBatis-Select
    • # select
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档