您的位置:首页 > 数据库

mybaties基础教程之四:SQL语句映射文件(2)

2014-03-27 12:48 555 查看

4.5parameters
上面很多地方已经用到了参数,比如查询、修改、删除的条件,插入,修改的数据等,MyBatis可以使用的基本数据类型和Java的复杂数据类型。

基本数据类型,String,int,date等。

但是使用基本数据类型,只能提供一个参数,所以需要使用Java实体类,或Map类型做参数类型。通过#{}可以直接得到其属性。

4.5.1基本类型参数

根据入学时间,检索学生列表:
<!-- 查询学生list,根据入学时间  -->
<select id="getStudentListByDate"  parameterType="Date" resultMap="studentResultMap">
	SELECT *
	  FROM STUDENT_TBL ST LEFT JOIN CLASS_TBL CT ON ST.CLASS_ID = CT.CLASS_ID
	 WHERE CT.CLASS_YEAR = #{classYear};	
</select>


List<StudentEntity> studentList = studentMapper.getStudentListByClassYear(StringUtil.parse("2007-9-1"));
for (StudentEntity entityTemp : studentList) {
	System.out.println(entityTemp.toString());
}


4.5.2实体类型参数
根据姓名和性别,检索学生列表。使用实体类做参数:
<!-- 查询学生list,like姓名、=性别,参数entity类型 -->
<select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">
	SELECT * from STUDENT_TBL ST
        WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
          AND ST.STUDENT_SEX = #{studentSex}
</select>


StudentEntity entity = new StudentEntity();
entity.setStudentName("李");
entity.setStudentSex("男");
List<StudentEntity> studentList = studentMapper.getStudentListWhereEntity(entity);
for (StudentEntity entityTemp : studentList) {
	System.out.println(entityTemp.toString());
}


4.5.3Map参数
根据姓名和性别,检索学生列表。使用Map做参数:
<!-- 查询学生list,=性别,参数map类型 -->
<select id="getStudentListWhereMap" parameterType="Map" resultMap="studentResultMap">
	SELECT * from STUDENT_TBL ST
	 WHERE ST.STUDENT_SEX = #{sex}
          AND ST.STUDENT_SEX = #{sex}
</select>


Map<String, String> map = new HashMap<String, String>();
map.put("sex", "女");
map.put("name", "李");
List<StudentEntity> studentList = studentMapper.getStudentListWhereMap(map);
for (StudentEntity entityTemp : studentList) {
	System.out.println(entityTemp.toString());
}


4.5.4多参数的实现

如果想传入多个参数,则需要在接口的参数上添加@Param注解。给出一个实例:

接口写法:
public List<StudentEntity> getStudentListWhereParam(@Param(value = "name") String name, @Param(value = "sex") String sex, @Param(value = "birthday") Date birthdar, @Param(value = "classEntity") ClassEntity classEntity);

SQL写法:
<!-- 查询学生list,like姓名、=性别、=生日、=班级,多参数方式 -->
<select id="getStudentListWhereParam" resultMap="studentResultMap">
	SELECT * from STUDENT_TBL ST
	<where>
		<if test="name!=null and name!='' ">
			ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{name}),'%')
		</if>
		<if test="sex!= null and sex!= '' ">
			AND ST.STUDENT_SEX = #{sex}
		</if>
		<if test="birthday!=null">
			AND ST.STUDENT_BIRTHDAY = #{birthday}
		</if>
		<if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">
			AND ST.CLASS_ID = #{classEntity.classID}
		</if>
	</where>
</select>


进行查询:
List<StudentEntity> studentList = studentMapper.getStudentListWhereParam("", "",StringUtil.parse("1985-05-28"), classMapper.getClassByID("20000002"));
for (StudentEntity entityTemp : studentList) {
	System.out.println(entityTemp.toString());
}

4.5.5字符串代入法

默认的情况下,使用#{}语法会促使MyBatis 生成PreparedStatement 属性并且使用PreparedStatement 的参数(=?)来安全的设置值。尽量这些是快捷安全,也是经常使用的。但有时候你可能想直接未更改的字符串代入到SQL 语句中。比如说,对于ORDER BY,你可能会这样使用:ORDER BY ${columnName}但MyBatis 不会修改和规避掉这个字符串。

注意:这样地接收和应用一个用户输入到未更改的语句中,是非常不安全的。这会让用户能植入破坏代码,所以,要么要求字段不要允许客户输入,要么你直接来检测他的合法性 。

4.6 cache缓存

MyBatis 包含一个强在的、可配置、可定制的缓存机制。MyBatis 3 的缓存实现有了许多改进,既强劲也更容易配置。默认的情况,缓存是没有开启,除了会话缓存以外,它可以提高性能,且能解决全局依赖。开启二级缓存,你只需要在SQL 映射文件中加入简单的一行:<cache/>

这句简单的语句的作用如下:
1. 所有在映射文件里的select 语句都将被缓存。

2. 所有在映射文件里insert,update 和delete 语句会清空缓存。

3. 缓存使用“最近很少使用”算法来回收

4. 缓存不会被设定的时间所清空。

5. 每个缓存可以存储1024 个列表或对象的引用(不管查询出来的结果是什么)。

6. 缓存将作为“读/写”缓存,意味着获取的对象不是共享的且对调用者是安全的。不会有其它的调用

7. 者或线程潜在修改。


例如,创建一个FIFO 缓存让60 秒就清空一次,存储512 个对象结果或列表引用,并且返回的结果是只读。因为在不用的线程里的两个调用者修改它们可能会导致引用冲突。
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true">
</cache>


还可以在不同的命名空间里共享同一个缓存配置或者实例。在这种情况下,你就可以使用cache-ref 来引用另外一个缓存。
<cache-ref namespace="com.liming.manager.data.StudentMapper"/>


Cache 语句属性配置细节:
属性说明取值默认值
eviction缓存策略:

LRU - 最近最少使用法:移出最近较长周期内都没有被使用的对象。

FIFI- 先进先出:移出队列里较早的对象

SOFT - 软引用:基于软引用规则,使用垃圾回收机制来移出对象

WEAK - 弱引用:基于弱引用规则,使用垃圾回收机制来强制性地移出对象
LRU

FIFI

SOFT

WEAK
LRU
flushInterval代表一个合理的毫秒总计时间。默认是不设置,因此使用无间隔清空即只能调用语句来清空。正整数不设置

size缓存的对象的大小正整数1024
readOnly只读缓存将对所有调用者返回同一个实例。因此都不能被修改,这可以极大的提高性能。可写的缓存将通过序列

化来返回一个缓存对象的拷贝。这会比较慢,但是比较安全。所以默认值是false。

true|falsefalse

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: