and t.alarm_time<![CDATA[<]]>=to_date(#{endTime},'yyyy-mm-dd hh24:mi:ss')
3:使用if判断时,空值可以这样判断
<if test="endTime !=null and endTime !='' ">
4:传递多个参数据 (一种是用map ,一种是用bean(略))
<!--
使用HashMap传递多个参数
parameterType 可以是别名或完全限定名 ,map->java.util.Map,这两个都是可以的
-->
<select id="selectBlogByMap" parameterType="map" resultType="Blog">
SELECT t.ID, t.title, t.content
FROM blog t
WHERE t.title = #{h_title}
AND t.content =#{h_content}
</select>
<!-- 使用JavaBean传递多个参数 -->
<select id="selectBlogByBean" parameterType="Blog" resultType="Blog">
SELECT t.ID, t.title, t.content
FROM blog t
WHERE t.title = #{title} <!--名字要与bean中的一致,大小写也要一致-->
AND t.content =#{content}
</select>
/**
* 通过Map传递多个参数
*/
@Test
public void testSelectByMap() {
SqlSession session = sqlSessionFactory.openSession();
Map<String, Object> param=new HashMap<String, Object>();
param.put("h_title", "oracle");
param.put("h_content", "使用序列!");
Blog blog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByMap",param);
session.close();
System.out.println("blog title:"+blog.getTitle());
}
/**
* 通过JavaBean传递多个参数
*/
@Test
public void testSelectByBean() {
SqlSession session = sqlSessionFactory.openSession();
Blog blog=new Blog();
blog.setTitle("oracle");
blog.setContent("使用序列!");
Blog newBlog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByBean",blog);
session.close();
System.out.println("new Blog ID:"+newBlog.getId());
}
使用注解
Mapper
public List<BigCustomerSumBandwidth> getBigCustomerSumByBigCustomerId(@Param("id") String id,
@Param("startTime") Date startTime);
xml
<select id="getBigCustomerSumByBigCustomerId"
resultType="com.chinacache.entity.BigCustomerSumBandwidth">
select * from big_customer_sumbandwidth where
big_customer_id=#{id} and day>=#{startTime,jdbcType=DATE}
</select>
5:使用缓存(二级缓存)
在spring的配置文件中加入mybatis的配置文件
7:There is no getter for property named '**' in 'class java.lang.String (参数用_parameter代替,sql中用${_parameter } )
<select id="getBigCustomerLimitByBu" resultType="com.chinacache.entity.BigCustomer"
parameterType="java.lang.String">
<if test="_parameter == 'SOC' ">
<![CDATA[
select t.* from (select * from big_user_config
where rownum<=15 order by order_band) t where rownum<=15
]]>
</if>
<if test="_parameter != 'SOC'">
<![CDATA[
select t.* from (select * from big_user_config
where
bu=#{_parameter} and rownum<=15 order by order_band) t where rownum<=15
]]>
</if>
</select>
8:MyBatis中出现Mapped Statements collection does not contain value异常解决方案
<select id="getCarAndHwCount" parameterType="com.autoyolConsole.vo.CarSearchCondition"
resultType="int">
SELECT COUNT(c.id) FROM car c JOIN member m ON c.mem_no=m.reg_no
<include refid="CHOSEN_CARS_WHERE_CONDITION" />
</select>
12:Document root element "mapper", must match DOCTYPE root "configuration"
mybatis.xml和mapper.xml都用了一个doctype
改回来即可
mybatis.xml:
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
mapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">