Mybatis学习笔记02

特殊返回参数

//查询全部
String statement = "select stuno,stuname from student";
//根据年龄查询学生
String statement ="select stuno,stuname from student where stuage = #{stuage}";
//根据姓名和年龄查询学生
String statement ="select stuno,stuname from student where stuname = #{stuname} and stuage =#{stuage}";

标签会自动处理第一个标签中的and,但不会处理之后中的and

用来解析传入的集合
查询学号为1,2,55的学生信息
ids = {1,2,55};
select stuno,stuname,from student where stuno in(1,2,55)
可以迭代的类型: 数组、集合、属性

属性:
SQL:  
	主sql + open + item + separator + close
select * from student 		//主sql
<where>
	<if test="stuNos!=null and stuNos.size>0">
		<foreach collection="stuNos" open=" and stuno in(" close=")" item="stuNo" separator=",">
			${stuNo}
		</foreach>
	</if>
</where>
数组
简单类型的数组:  无论编写代码时,传递的是什么参数名,在mapper.xml中,必须用array来代替数组
集合:
无论编写代码时,传递的是什么参数名,在mapper.xml中,必须用list来代替数组
对象数组:
Student[] students = {student0,student1,student2}  每个studentx中包含一个学号的属性
注意: parameterType="Object[]"   item="student"   ${student.stuNo}

SQL片段:

将相似的代码提炼出来。  类似于Java中的函数
提取相似代码:
	<sql id="objectArrayStuNos"> 
		代码
	</sql>
使用:
	<include refid="objectArrayStuNos"></include>
如果sql片段和引用不在同一个文件中, refid=文件所在位置+id

关联查询:

一对一:
业务扩展类:
	核心: 用resultType 指定类的属性,包含多表查询的所有字段 (继承一个 再写上另一个)
resultMap:
	1.通过属性成员,建立两个类的关系
	2.写映射文件
		<select id="queryStudentByNoWithCardId2" parameterType="int" resultMap="student_card_map">
			select s.*,c.* from student s inner join studentcard c on s.cardid=c.cardid
			where s.stuno = #{stuNo}
		</select>
		<resultMap type="student" id="student_card_map">
			<!-- 学生的信息 -->
			<id property="stuNo" column="stuNo"/>
			<result property="stuName" column="stuName"/>
			<result property="stuAge" column="stuAge"/>
			<!-- 一对一时,对象成员用association 映射    javaType 指定该属性的类型 -->
			<association property="card" javaType="StudentCard">
				<id property="cardId" column="cardId"/>
				<result property="cardInfo" column="cardInfo"/>
			</association>
		</resultMap>
一对多: (多对一) (多对多)
表:  student  studentclass    关联:classid
类:  Student  StudentClass    关联:StudentClass的一个属性成员(List<Student> students)

日志:

Log4j:
1.下载一个 Log4j.jar
2.开启日志  
	在conf.xml中配置
	<settings>
		<setting name="logImpl" value="LOG4J"/>
	</settings>
	如果不指定,Mybatis就会根据以下顺序,寻找日志
	SLF4J --> Apache Commons Logging -->Log4j 2 --> Log4j -->JDK Logging
3.编写配置日志输出文件
	log4j.properties
		log4j.rootLogger=DUBUG,stdout
		log4j.appender.stdout=org.apache.log4j.ConsoleAppender
		log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
		log4j.appender.stdout.layout.ConversionPattern= %5p [%t] -%m%n
	日志级别:DEBUG<INFO<WARN<ERROR
	如果设置为INFO  则只显示info及以上的级别 (开发debug  运行时info及以上 )

可以通过日志信息,详细的阅读mybatis执行情况(观察实际执行的sql,以及sql中的参数,和返回结果)

延迟加载:

先配置conf.xml
	<settings>
		<!-- 开启延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>		
		<!-- 关闭立即加载 -->
		<setting name="aggressivelazyLoading" value="false"/>
	</settings>

在查询时 如果不使用延迟加载(既立即加载)查询时,会将 一(班级) 和 多(班级中的学生) 都查询一遍。
如果暂时需要查询 一 ,先不查询 多, 在需要的时候才查询 多 这时,就可以使用延迟加载
通过debug可以发现,
如果程序只需要学生,则只想数据库发送了查询学生的SQL,如果需要学生证,再第二次发送查询学生证的SQL。

延迟加载的步骤:
1.开启,  conf.xml中配置settings
2.配置mapper.xml,两个mapper.xml
	先配置查询班级的mapper.xml,同时关联到查询学生的select,按需要进行查询
	<select id="queryClassAndStudentsLazyload" resultMap="class_student_lazyload_map">
		select * from studentclass 
	</select>
	<resultMap type="StudentClass" id="class_student_lazyload_map">
		<!--  类和表的一一对应关系 -->
		<id property="classId" column="classId"/>
		<result property="className" column="className"/>
		
		<collection property="students" ofType="Student" select="org.mybatis.mapper.StudentMapper.qureyStudetsByClassId" column="classid">
		</collection>
	</resultMap>

查询缓存:

一级缓存: 同一个SQLSession对象。

Mybatis默认开启一级缓存,如果用同样的SQLSession对象查询相同的数据,则只会在第一次查询时,向数据库查询。
并将结果放入SQLSession中(作为缓存),后续再次查询该同样的对象时,则直接从缓存中获取。(不用再次访问数据库)

二级缓存:

Mybatis自带的二级缓存: 序列化 (钝化 硬盘文件保存) 默认关闭
同一个namespace(全类名)生成的mapper对象
只要产生的xxxMapper对象,来自于同一个namespace,则这些对象可以共享二级缓存。
开启:
1.在conf.xml中配置			<setting name="cacheEnabled" value="true"/>
2.在具体的mapper.xml中,声明此namespace产生的mapper开启二级缓存    <cache/>
3.准备缓存的对象,必须实现序列化接口
(如果开启缓存的namespace="xxx.xxx.Student"  需要将Student对象序列化  Student的级联属性,父类 实现一个序列化接口)
禁用: select 标签中  useCache="false"
清理: 
	1.commit();  和清理一级缓存的方法相同。但不能时查询自身的commit   (执行增删改时,会清理掉缓存,能够避免产生脏数据)
	2.在select标签中 增加属性  flushCache="true"
三方提供的二级缓存:
ehcache 、memcache、自定义二级缓存   都必须实现Cache接口
ehcache二级缓存:
1.导入jar
	Ehcache-core.jar
	mybatis-Encache.jar
	slf4j-api.jar
2.编写ehcache配置文件 Ehcache.xml 
3.开启EhCache二级缓存   在xxxmapper.xml中,<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>

逆向工程:

表、类、接口、mapper.xml四者密切相关,  因此,当知道一个的时候,
其他三个因该可以自动生成
一般情况下都是用 表-->其他三个
实现步骤:
1.三个jar包
	mybatis-generator-core.jar
	mybatis.jar
	ojdbc.jar
2.逆向工程的配置文件  generator.xml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值