SSM笔记-查询

1、selsect标签属性:
①id:唯一标识符,一般用方法名
②resultType:返回值类型,如果返回集合元素,则类型写集合里面的元素的类型,不可跟resultMap一起用
③parameterType:可不写,mybatis会根据TypeHandler判断

2、内容包括:
①返回List数据
②返回Map数据 (单条数据)
③返回Map数据 (多条数据)
④resultMap查询
⑤resultMap级联查询
⑥使用association的resultMap级联查询
⑦使用association的resultMap级联分步查询
⑧懒加载
⑨discriminator鉴别器

3、返回List类型的数据
步骤:
①创建Mapper接口,创建List<Bean>类型的接口方法
②Mapper xml中配置对应的<select>标签
③调用接口方法,获得结果

4、返回单条Map类型的数据
步骤:
①创建Mapper接口,创建Map<String,Object>类型的接口方法
②Mapper xml中配置对应的<select>标签
③调用接口方法,获得结果

5、返回多条Map类型的数据
步骤:
①创建Mapper接口,创建Map<type,Bean>类型的接口方法(其中type代表的是作为map的key的数据的类型)
②Mapper xml中配置对应的<select>标签
③在Mapper接口对应的接口方法上,使用@MapKey(“XX”)修饰方法(其中XX代表需要使用查询结果的哪个字段作为map的key)
④调用接口方法,获得结果

6、通过resultMap来自定义映射结果
步骤:跟用resultType一样,只不过在Mapper xml里面需要用resultMap标签来定义resultMap的映射规则,然后在<select>标签里面使用resultMap属性调用对应resultMap映射规则
注意:
1)resultType和resultMap不能同时使用
2)<resultMap>标签中属性:
①要使用<id>标签制定主键
②使用<result>标签制定其他字段
3)<resultMap>中的<id><result>标签中column代表数据库字段名,property表示Bean中的属性名

7、resultMap级联查询
步骤:跟用resultMap一样,只不过对于需要级联的参数名,需要写为级联的类对象名.级联的属性名(如detail.detailId)
注意:
<resultMap>标签中的所有子标签的column都是sql的参数属性名,property都是bean类中的属性名
②级联操作的对象的<result>标签中,column属性写的是sql获取的值的属性名,property属性写的是bean名+bena中属性名

8、使用association的resultMap级联查询
步骤:跟用resultMap一样,只不过<resultMap>中使用<association>标签来设置需要被级联的参数,这样就不需要像resultMap级联查询的时候那样,用级联的类对象名.级联的属性名来设置属性名,这样就更加清晰了
注意:
<association>标签中的property属性的值是bean类中的级联类的对象属性名,javaType是级联类的全类名(javaType必须要写)
<association>标签中的<id>标签的column注意不要跟<resultMap>中的<id>的column重名

9、使用association的resultMap级联分步查询
步骤:
①创建相关bean类和mapper接口类和mapper配置文件
②在mybatis配置文件中注册mapper配置文件
③分别在mapper接口类定义方法,分别在mapper配置文件中用<select>标签写好查询各自表的sql
④在需要级联别的类的类的mapper配置文件中的<resultMap>使用<association>标签
⑤调用需要级联别的类的类的方法
注意:
1)为级联类创建Mapper接口和Mapper配置文件的时候,文件名要对应
2)要记得在mybatis配置文件的mapping标签中注册新的mapper配置文件
3)分步查询的时候,select语句只需要写当前mapper对应的bean的查询语句即可
4)<association>的属性的值:
①property :属性值为bean中的级联类的对象的属性名
②select :写需要级联的类的方法的全类名加方法名
③column :写需要把值传给级联类mapper文件的属性的属性名

10、懒加载:
①使用<association>时,如果需要进行懒加载(不需要的时候不查询被关联的类的数据),则需要在mybatis配置文件的<settings>标签中使用lazyLoadingEnable(true)和aggressiveLazyLoading(false)
②mybatis配置文件中<settings>要在<properties>下面,<settings>后面再写其他标签,要注意顺序
③对集合类型的数据进行懒加载时,<association>标签应该换为<collection>

11、discriminator鉴别器
作用:鉴别器在于确定使用那个ResultMap来映射SQL查询语句,即通过某些条件判断resultMap使用哪个mapper来进行映射
用法:
1)在mapper映射文件中配置多个不同的映射方法
2)在需要使用discriminator鉴别器的映射文件的<resultMap>标签中使用<discriminator>
3)discriminator属性:
①javaType:需要使用discriminator鉴别的参数在bean类中的属性的类型
②column:需要使用discriminator鉴别的参数在bean类中的属性名
<case>:<discriminator>会根据这个标签来加载不同的mapper映射文件的方法,其中value属性是判断的条件值,resultMap属性是需要加载的mapper文件所对应的接口类的方法全类名

13、mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<properties resource="dbConfig.properties"></properties>
	
	<settings>
		<!-- 打开延迟加载的开关 -->
		<setting name="lazyLoadingEnabled" value="true" />
		<!-- 将积极加载改为消息加载即按需加载 -->
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>
	
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driver}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="InfoMapper.xml" />
		<mapper resource="DetailMapper.xml" />
	</mappers>
</configuration>

14、InfoMapper.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.demo.ssmtest.InfoMapper">

	<!--
	id				:唯一标识符,一般用方法名
	resultType		:返回值类型,如果返回集合元素,则类型写集合里面的元素的类型,不可跟resultMap一起用
	parameterType	:可不写,mybatis会根据TypeHandler判断
	-->
	
	<!-- 返回List数据 -->
	<select id="getAllReturnList"  resultType="com.demo.ssmtest.Info">
		select * from info
	</select>
	<!-- 返回Map数据 (单条数据)-->
	<select id="getByIdReturnMap"  resultType="map">
		select * from info where id=#{id}
	</select>
	<!-- 返回Map数据 (多条数据)-->
	<select id="getAllReturnMap"  resultType="com.demo.ssmtest.Info">
		select * from info
	</select>
	
	<!-- 测试resultMap-->
	<select id="getAllResultMap"  resultMap="rsMap">
		select * from info
	</select>
	<resultMap type="com.demo.ssmtest.Info" id="rsMap">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="password" property="password"/>
		<result column="description" property="description"/>
	</resultMap>
	
	<!-- 测试resultMap级联查询-->
	<!--
	注意:
	1、<resultMap>标签中的所有子标签的column都是sql的参数属性名,property都是bean类中的属性名
	2、级联操作的对象的<result>标签中,column属性写的是sql获取的值的属性名,property属性写的是bean名+bena中属性名
	-->
	<select id="getByIdInfoAndDetail"  resultMap="rsInfoDetail">
		select i.id,i.name,i.password,i.description,d.id as detailId,d.name as detailName from info i,detail d where i.detailId = d.id and i.id=#{id}
	</select>
	<resultMap type="com.demo.ssmtest.Info" id="rsInfoDetail">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="password" property="password"/>
		<result column="description" property="description"/>
		<result column="detailId" property="detail.id"/>
		<result column="detailName" property="detail.name"/>
	</resultMap>
	
	<!-- 使用association测试resultMap级联查询-->
	<!--
	注意:
	1、<association>标签中的property属性的值是bean类中的级联类的对象属性名,javaType是级联类的全类名(javaType必须要写)
	2、<association>标签中的<id>标签的column注意不要跟<resultMap>中的<id>的column重名
	-->
	<!--
	
	-->
	<select id="getByIdInfoAndDetail2"  resultMap="rsInfoDetailassociation">
		select i.id,i.name,i.password,i.description,d.id as detailId,d.name as detailName from info i,detail d where i.detailId = d.id and i.id=#{id}
	</select>
	<resultMap type="com.demo.ssmtest.Info" id="rsInfoDetailassociation">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="password" property="password"/>
		<result column="description" property="description"/>
		<association property="detail" javaType="com.demo.ssmtest.Detail">
			<id column="detailId" property="id"/>
			<result column="detailName" property="name"/>
		</association>
	</resultMap>
	
	
	<!-- 使用association测试resultMap级联分步查询-->
	<!--
	步骤:
	①创建相关bean类和mapper接口类和mapper配置文件
	②在mybatis配置文件中注册mapper配置文件
	③分别在mapper接口类定义方法,分别在mapper配置文件中用<select>标签写好查询各自表的sql
	④在需要级联别的类的类的mapper配置文件中的<resultMap>使用<association>标签
	⑤调用需要级联别的类的类的方法
	注意:
	1、为级联类创建Mapper接口和Mapper配置文件的时候,文件名要对应
	2、要记得在mybatis配置文件的mapping标签中注册新的mapper配置文件
	3、分步查询的时候,select语句只需要写当前mapper对应的bean的查询语句即可
	4、<association>的属性的值:
		①property	:属性值为bean中的级联类的对象的属性名
		②select		:写需要级联的类的方法的全类名加方法名
		③column		:写需要把值传给级联类mapper文件的属性的属性名
	-->
<!--
	懒加载:
	1、使用<association>时,如果需要进行懒加载(不需要的时候不查询被关联的类的数据),则需要在mybatis配置文件的<settings>标签中使用lazyLoadingEnable(true)和aggressiveLazyLoading(false)
	2、mybatis配置文件中<settings>要在<properties>下面,<settings>后面再写其他标签,要注意顺序
	3、对集合类型的数据进行懒加载时,<association>标签应该换为<collection>
-->	
	<select id="getByIdStep"  resultMap="rsInfoDetailassociationStep">
		select * from info where id=#{id}
	</select>
	<resultMap type="com.demo.ssmtest.Info" id="rsInfoDetailassociationStep">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="password" property="password"/>
		<result column="description" property="description"/>
		<result column="detailId" property="detailId"/>
		<association property="detail" select="com.demo.ssmtest.DetailMapper.getDetailById" column="detailId">
		</association>
	</resultMap>
	
	<!-- discriminator鉴别器 -->
	<select id="getByIddiscriminator"  resultMap="rsDiscriminator">
		select * from info where id=#{id}
	</select>
	<resultMap type="com.demo.ssmtest.Info" id="rsDiscriminator">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="password" property="password"/>
		<result column="description" property="description"/>
		<result column="detailId" property="detailId"/>
		<association property="detail" select="com.demo.ssmtest.DetailMapper.getDetailById" column="detailId"></association>
		<discriminator javaType="int" column="id">
			<case value="1" resultMap="com.demo.ssmtest.DetailMapper.getDetailById"></case>
			<case value="2" resultMap="com.demo.ssmtest.DetailMapper.getDetailById2"></case>
		</discriminator>
	</resultMap>
</mapper>

15、DetailMapper.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.demo.ssmtest.DetailMapper">

	<select id="getDetailById"  resultType="com.demo.ssmtest.Detail">
		select * from detail where id=#{id}
	</select>
	
	<select id="getDetailById2"  resultType="com.demo.ssmtest.Detail">
		select * from detail where id=#{id}
	</select>
	
</mapper>

16、dbConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=

17、Detail.java

package com.demo.ssmtest;

public class Detail {

	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Detail [id=" + id + ", name=" + name + "]";
	}
}

18、DetailMapper.java

package com.demo.ssmtest;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.MapKey;

public interface DetailMapper {

	public List<Info> getDetailById(Integer id);
	
	public List<Info> getDetailById2(Integer id);
	
}

19、Info.java

package com.demo.ssmtest;


public class Info {

	Integer id;
	String name;
	String password;
	String description;
	Integer detailId;
	Detail detail;
	
	public Info() {
		super();
	}

	public Info(Integer id, String name, String password, String description, Integer detailId, Detail detail) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
		this.description = description;
		this.detailId = detailId;
		this.detail = detail;
	}

	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 getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getDetailId() {
		return detailId;
	}

	public void setDetailId(Integer detailId) {
		this.detailId = detailId;
	}

	public Detail getDetail() {
		return detail;
	}

	public void setDetail(Detail detail) {
		this.detail = detail;
	}

	@Override
	public String toString() {
		return "Info [id=" + id + ", name=" + name + ", password=" + password + ", description=" + description
				+ ", detailId=" + detailId + ", detail=" + detail + "]";
	}
}

20、InfoMapper.java

package com.demo.ssmtest;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.MapKey;

public interface InfoMapper {

	public List<Info> getAllReturnList();
	
	public Map<String,Object> getByIdReturnMap(Integer id);
	
	@MapKey("id")
	public Map<Integer,Info> getAllReturnMap();
	
	public List<Info> getAllResultMap();
	
	public List<Info> getByIdInfoAndDetail(Integer id);
	
	public List<Info> getByIdInfoAndDetail2(Integer id);
	
	public List<Info> getByIdStep(Integer id);
	
	public List<Info> getByIddiscriminator(Integer id);
}

21、TestMain.java

package com.demo.ssmtest;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class TestMain {
	
	
	public static void main(String[] args) throws Exception {
		test();
	}

	public static void test() throws Exception {

		String resource = "mybatis.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession openSession = sqlSessionFactory.openSession();
		try {
			InfoMapper queryMapper = openSession.getMapper(InfoMapper.class);
			
			//返回List类型的数据
			//步骤:
			//①创建Mapper接口,创建List<Bean>类型的接口方法
			//②Mapper xml中配置对应的<select>标签
			//③调用接口方法,获得结果
			List<Info>infoList = queryMapper.getAllReturnList();
			for(Info in: infoList){
				System.out.println("1--"+in.toString());
			}
			
			//返回单条Map类型的数据
			//步骤:
			//①创建Mapper接口,创建Map<String,Object>类型的接口方法
			//②Mapper xml中配置对应的<select>标签
			//③调用接口方法,获得结果
			Map<String,Object>info1Map = queryMapper.getByIdReturnMap(1);
			System.out.println("2--"+info1Map.toString());
			
			//返回多条Map类型的数据
			//步骤:
			//①创建Mapper接口,创建Map<type,Bean>类型的接口方法(其中type代表的是作为map的key的数据的类型)
			//②Mapper xml中配置对应的<select>标签
			//③在Mapper接口对应的接口方法上,使用@MapKey("XX")修饰方法(其中XX代表需要使用查询结果的哪个字段作为map的key)
			//④调用接口方法,获得结果
			Map<Integer,Info>infoAllMap = queryMapper.getAllReturnMap();
			System.out.println("3--"+infoAllMap.toString());
			
			//测试通过resultMap来自定义映射结果
			//步骤:跟用resultType一样,只不过在Mapper xml里面需要用resultMap标签来定义resultMap的映射规则,然后在<select>标签里面使用resultMap属性调用对应resultMap映射规则
			//注意:
			//1、resultType和resultMap不能同时使用
			//2、<resultMap>标签中属性:
			//		①要使用<id>标签制定主键
			//		②使用<result>标签制定其他字段
			//3、<resultMap>中的<id>和<result>标签中column代表数据库字段名,property表示Bean中的属性名
			List<Info>infoRSMapList = queryMapper.getAllResultMap();
			for(Info in: infoRSMapList){
				System.out.println("4--"+in.toString());
			}
			
			
			List<Info>infoAndDetailList = queryMapper.getByIdInfoAndDetail(1);
			for(Info in: infoAndDetailList){
				System.out.println("5--"+in.toString());
			}
			
			List<Info>infoAndDetailList2 = queryMapper.getByIdInfoAndDetail2(1);
			for(Info in: infoAndDetailList2){
				System.out.println("6--"+in.toString());
			}
			
			List<Info>infoStep = queryMapper.getByIdStep(1);
			for(Info in: infoStep){
				System.out.println("7--"+in.toString());
			}
			
			List<Info>infoDiscriminator= queryMapper.getByIddiscriminator(1);
			for(Info in: infoDiscriminator){
				System.out.println("8--"+in.toString());
			}
		} finally {
			openSession.close();
		}

	}
}

22、项目目录
项目目录

23、demo
https://download.csdn.net/download/qq_22778717/10718678

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值