Mybatis关联映射

关联映射查询:

如果我们没有开启Full全部映射,那么自己的数据也获取不到,只能一个一个对应字段,所以需要获取全部数据情况下,我们采用的步骤如下:
1.在大配置文件中开启映射
2.在小配置文件中加入映射resultMap,关联一的一方或者多的一方,其他只需要手动映射不一致字段即可
如果不需要全部获取,不用开启FULL,建议手动映射

关联映射分类:

	**1.一对一:*查询新闻时,需要将分类数据一起查询出来**
		在关联查询时,没有获取到关联实体对象的数据,解决方案为:
			关联一的一方的对象,采用resultMap,如果说association里面没有一个一个映射字段的情况下,那么想要拿到数据,必须在大配置文件中去开启映射 <
			setting name="autoMappingBehavior" value="FULL"/>
			如果不开启,那么只能一个一个字段手动映射
				
			实体之间对应关系:
			News实体,包含一的一方(Topic对象)

	  接口:
		//查询所有的新闻数据,包含新闻类别信息
		public List<News> getAllNews() throws Exception;
		小配置文件:
			<!--一对一关联-->
			<resultMap id="newsMap" type="News">
				<!--自动关联的情况下,只要保证实体属性和数据表字段一致即可-->

				<!--关联映射一的一方  property实体中关联一的一方的属性-->
				<association property="topic" javaType="Topic">
				<!--配置关联的属性,写实体与表不一致的字段-->
							
				</association>
	    	</resultMap>
		
		测试类:
				// 一对一的查询
				@Test
					public void getAllNews() throws Exception {
						//获取SqlSession对象
						SqlSession sqlSession = MybatisUtil.getSqlSession();
						INewsMapper mapper = sqlSession.getMapper(INewsMapper.class);

						List<News> newsList = mapper.getAllNews();
						for(News news:newsList){
							System.out.println(news.toString());
						}
						//关闭资源
						MybatisUtil.closeSqlSession(sqlSession);
					}
					
			
		
	**2.一对多:单条SQL**
		查询国内新闻分类下的所有新闻数据,解决方案:在新闻分类的实体当中植入新闻集合
		
		接口中新建方法
			//查询国内新闻数据
			public Topic getTopic(Integer tid) throws  Exception;
		
		小配置文件
			<resultMap id="topicMap" type="Topic">
				<!--植入多的一方 property多的一方的属性名称  ofType关联集合的泛型-->
				<collection property="newsList" ofType="News">

				</collection>
			</resultMap>
			
			<select id="getTopic" resultMap="topicMap">
				select * from news,topic where news.ntid=topic.tid and topic.tid=#{tid}
			</select>
		
		测试类:	
			/**
			 * 一对多的查询
			 * @throws Exception
			 */
			@Test
			public void getTopic() throws Exception {
				//获取SqlSession对象
				SqlSession sqlSession = MybatisUtil.getSqlSession();
				ITopicMapper mapper = sqlSession.getMapper(ITopicMapper.class);

				Topic topic = mapper.getTopic(1);
				System.out.println(topic.toString());
				//关闭资源
				MybatisUtil.closeSqlSession(sqlSession);
			}
		
	
	**3.一对多,多条SQL**
		查询国内新闻分类下的所有新闻数据,解决方案:在新闻分类的实体当中植入新闻集合
		
		接口中新建方法
			//查询国内新闻数据
			public Topic getTopic(Integer tid) throws  Exception;
		
		小配置文件
			<resultMap id="topicMap" type="Topic">
				<!--关联多的一方   select代表执行这个查询   column获取第一次查询的列-->
				<collection property="newsList" ofType="News" select="getNewsByTopicId" column="tid"></collection>
			</resultMap>

			<select id="getTopic" resultMap="topicMap">
				select * from topic where tid=#{tid}
			</select>
			
			<select id="getNewsByTopicId" resultType="News">
				select * from news where ntid=#{tid}
			</select>
		
		测试类:	
			/**
			 * 一对多的查询
			 * @throws Exception
			 */
			@Test
			public void getTopic() throws Exception {
				//获取SqlSession对象
				SqlSession sqlSession = MybatisUtil.getSqlSession();
				ITopicMapper mapper = sqlSession.getMapper(ITopicMapper.class);

				Topic topic = mapper.getTopic(1);
				System.out.println(topic.toString());
				//关闭资源
				MybatisUtil.closeSqlSession(sqlSession);
			}

	**4.多对一**
		查询所有的新闻,包含新闻的分类
		单条SQL:
		mapper接口层:
			//查询所有新闻数据
			public List<News> getAllNews() throws Exception;
		
		小配置文件:	
			<resultMap id="newsMap" type="News">
				自动关联的情况下,只要保证实体属性和数据表字段一致即可

				关联映射一的一方  property实体中关联一的一方的属性
				<association property="topic" javaType="Topic">
					&lt;!&ndash;配置关联的属性,写实体与表不一致的字段&ndash;&gt;

				</association>
			</resultMap>
			<select id="getAllNews" resultMap="newsMap">
				select * from news,topic where news.ntid=topic.tid
			</select>
		
		测试类:	
			/**
			 * 多对一
			 * @throws Exception
			 */
			@Test
			public void getAllNews() throws Exception {
				//获取SqlSession对象
				SqlSession sqlSession = MybatisUtil.getSqlSession();

				INewsMapper mapper = sqlSession.getMapper(INewsMapper.class);
				List<News> newsList = mapper.getAllNews();
				for(News news:newsList){
					System.out.println(news.toString());
				}
				//关闭资源
				MybatisUtil.closeSqlSession(sqlSession);
			}
	**多条SQL:**
		mapper接口层:
			//查询所有新闻数据
			public List<News> getAllNews() throws Exception;
		
		小配置文件:	
			<resultMap id="newsMap" type="News">
				<!--植入一的一方的对象-->
				<association property="topic" javaType="Topic" select="getTopicByNtid" column="ntid"></association>
			</resultMap>

			<select id="getAllNews" resultMap="newsMap">
				select * from news
			</select>
			<select id="getTopicByNtid" resultType="Topic">
				select * from topic where tid=#{ntid}
			</select>
		
		测试类:	
			/**
			 * 多对一
			 * @throws Exception
			 */
			@Test
			public void getAllNews() throws Exception {
				//获取SqlSession对象
				SqlSession sqlSession = MybatisUtil.getSqlSession();

				INewsMapper mapper = sqlSession.getMapper(INewsMapper.class);
				List<News> newsList = mapper.getAllNews();
				for(News news:newsList){
					System.out.println(news.toString());
				}
				//关闭资源
				MybatisUtil.closeSqlSession(sqlSession);
			}
	**多对多:一对多成立,反之多对一也成立**
	一名教员可以教授多名学员,多名教员可以教授一名学员
	
	Student 
		List<Teacher>
	Teacher	
		List<Student>
		
	解决方案:在实体当中互相植入多的一方的对象(如果利用到了第三章表,返回的结果是多条记录,那就不能采用单个对象接收数据,要采用集合)
		mapper层接口
			//查询微冷的雨教授的学员
			public List<Teacher> getTeacherById(Integer tid) throws Exception;
		
		mapper小配置文件
			<resultMap id="teacherMap" type="Teacher">
				<!--植入多的一方的对象-->
				<collection property="stuList" ofType="Student">

				</collection>
			</resultMap>
			<select id="getTeacherById" resultMap="teacherMap">
				select * from teacher,student,t_s_relation where teacher.tid=t_s_relation.tid and student.stuid=t_s_relation.stuid
				and teacher.tid=#{tid} and t_s_relation.tid=#{tid}
			</select>
			
		
		测试类:	
			@Test
			public void getTeacherById() throws Exception {
				//获取SqlSession对象
				SqlSession sqlSession = MybatisUtil.getSqlSession();

				ITeacherMapper mapper = sqlSession.getMapper(ITeacherMapper.class);
				List<Teacher> teacherList = mapper.getTeacherById(2);
				for (Teacher teacher:teacherList){
					System.out.println(teacher.toString());
				}

				//关闭资源
				MybatisUtil.closeSqlSession(sqlSession);
			}
	**自关联:在自身内部关联自身集合**
	实体类:
		public class City {
			private Integer cid;    //地区编号
			private String cname;   //地区名称
			private Integer parentid;    //上一级分类ID
			//自关联,就是在内部注入本身类型的集合
			private List<City> cityList;
		}
	mapper层接口	
		//北京市下面的所有城区
		public City getCity(Integer cid) throws Exception;
	
	小配置文件	
		<resultMap id="cityMap" type="City">
			<id property="cid" column="cid"/>
			<collection property="cityList" ofType="City" select="getChildCity" column="cid">

			</collection>
		</resultMap>
		<!--自关联怎么样去查询-->
		<select id="getCity" resultMap="cityMap">
			select * from city where cid=#{cid}
		</select>
		<select id="getChildCity" resultMap="cityMap">
			select * from city where parentid=#{cid}
		</select>
	
	测试类:
		@Test
		public void getCity() throws Exception {
			//获取SqlSession对象
			SqlSession sqlSession = MybatisUtil.getSqlSession();

			ICityMapper mapper = sqlSession.getMapper(ICityMapper.class);
			City city = mapper.getCity(1);
			System.out.println(city.toString());

			//关闭资源
			MybatisUtil.closeSqlSession(sqlSession);
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值